1:二分查找法
public class Test {
public static void main(String[] args) {
int []a ={1,2,5,7,8,12,14};
System.out.println(print(a, 8));
}
public static int print(int[] a,int num){
int start =0;
int end=a.length-1;
int mid=-1;
while(start<=end){
mid=(start+end)/2;
if(a[mid]==num){
return mid;
}else if (a[mid]<num) {
start=mid+1;
}else if (a[mid]>mid) {
end=mid-1;
}
}
return -1;
}
}
2:链表的算法实现
public class Test {
public static void main(String[] args) {
MassageNode massageNode=new MassageNode();
massageNode.add(1);
massageNode.add(2);
massageNode.add(3);
massageNode.add(4);
massageNode.add(5);
massageNode.add(6);
System.out.println(massageNode.delect(5));
massageNode.finAll();
}
}
class MassageNode{
private Node rootNode;
public void add(int date){
if(rootNode==null){
rootNode=new Node(date);
}else{
rootNode.add(date);
}
}
public boolean delect(int date){
if(rootNode!=null){
if (rootNode.getDate()==date) {
rootNode=rootNode.nextNode;
return true;
}else {
return rootNode.delect(date);
}
}
return false;
}
public void finAll(){
if(rootNode!=null){
System.out.println(rootNode.date);
rootNode.finAll();
}else{
return;
}
}
private class Node{
private int date;
private Node nextNode;
public Node() {
super();
// TODO Auto-generated constructor stub
}
public Node(int date) {
super();
this.date = date;
}
public int getDate() {
return date;
}
public void setDate(int date) {
this.date = date;
}
public void add(int date){
if(this.nextNode==null){
this.nextNode=new Node(date);
}else{
this.nextNode.add(date);
}
}
public boolean delect(int date){
if(this.nextNode!=null){
if (this.nextNode.getDate()==date) {
this.nextNode=this.nextNode.nextNode;
return true;
}else {
return this.nextNode.delect(date);
}
}
return false;
}
public void finAll(){
if(this.nextNode!=null){
System.out.println(this.nextNode.getDate());
this.nextNode.finAll();
}else{
return;
}
}
}
}