其他分享
首页 > 其他分享> > 剑指offer1-7题

剑指offer1-7题

作者:互联网

/**

优化:因为数列是递增有序的,可以进行二分查找进行优化,但是本题可以不进行二分也可以过。因为大家有兴趣可以去查一查编程语言数组可以开多大。然后单个查找在这个范围内即使不优化也不会超时。有兴趣的可以自己写一写二分!复杂度O(logn)
*/

public class Shuzuchazhao {


       public boolean Find(int target,int [][] array){
           if (array.length==0||array[0].length==0)
               return false;
           for (int i=array.length-1;i>=0;i--){
               if (array[i][0]>target){
                   continue;
               }
               for (int j=0;j<array[0].length;j++){
                    if (array[i][j] == target){
                        return true;
                    }
               }
           }
           return false;
       }
}

/**

public class Tihuankongge {
       /** public static String replaceSpace(StringBuffer str){
            String team = str.toString();
            return team.replaceAll(" ","%20");
        }
        **/
        public static String  replaceSpace(StringBuffer str){
            StringBuffer str2=new StringBuffer();
             char demo=' ';
             for (int i=0;i<str.length();i++){
                 demo=str.charAt(i);
                 if (demo == ' '){
                     str2.append("%20");
                 }
                 else {
                     str2.append(demo);
                 }
             }
             return str2.toString();
        }
}

/**

public class Linkedlist {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {

        ArrayList<Integer>list=new ArrayList<Integer>();
        while (listNode!=null) {
            list.add(0, listNode.val);
            listNode=listNode.next;
        }
        return list;

    }
}

/**

*/

public class Binarytree {

      public TreeNode reConstructBinaryTree(int [] pre, int [] in){
            TreeNode node=new TreeNode(in[0]);
            int preleft=0,preright=pre.length-1;
            int inleft=0,inright=pre.length-1;
            return creat(pre,in,preleft,preright,inleft,inright);
      }
      private TreeNode creat(int[] pre,int[] in,int preleft,int preright,int inleft,int inright){
            if (preleft>preright||inleft>inright) return null;
            TreeNode node=new TreeNode(pre[preleft]);
            int mid=0;
            for (int i=inleft;i<=inright;i++){
                  if (pre[preleft]==in[i]){
                        mid=i;
                  }
            }
            node.left=creat(pre,in,preleft+1,preleft+(mid-inleft),inleft,mid-1);
            node.right=creat(pre,in,preleft+(mid-inleft)+1,preright,mid+1,inright);
            return node;
      }
}

/**

public class Stack {

    java.util.Stack<Integer> stack1=new java.util.Stack<>();
    java.util.Stack<Integer> stack2=new java.util.Stack<>();

    public void push(int node){
        while (!stack1.isEmpty()){
            stack2.push(stack1.pop());
        }
        stack2.push(node);
        while (!stack2.isEmpty()){
            stack1.push(stack2.pop());
        }
        stack2.clear();
    }
    public int pop(){
        return stack1.pop();
    }
}

/**

*/

public class Xuanzhuanshuzu {
        public int minNumberInRotateArray(int[] array){
            if (array.length==0)return 0;
            int min=array[0];
            for (int i=0;i<array.length;i++){
                if (array[i]<min){
                    min=array[i];
                    break;
                }
            }
            return min;
        }

        int minNumberInRotateArray(vector<int> rotateArray){
            if (rotateArray.empty()){
                return 0;

                int low=0;
                int high=rotateArray.size()-1;
                int mid=0;

                while (low<high){
                    if (rotateArray[low] < rotateArray[high])
                        return rotateArray[low];
                    mid=low+(high-low)/2;
                    if (rotateArray[mid] > rotateArray[low])
                        low=mid+1;
                    else if (rotateArray[mid] < rotateArray[high])
                        high=mid;
                    else low++;
                }
                return rotateArray[low];
            }
        }
}

/**

public class Feibona {

    public int Fibonacci(int n){
        if (n==1||n==0){
            return n;
        }else{
            return Fibonacci(n-1+Fibonacci(n-2));
        }
    }
    public int Fibonacci2(int n){
        int Fibo[]=new int[40];
        Fibo[0]=0;
        Fibo[1]=1;
        for (int i=2;i<=n;i++){
            Fibo[i]=Fibo[i-1]+Fibo[i-2];
        }
        return Fibo[n];
    }
}

标签:遍历,return,int,前序,offer1,数组,public
来源: https://blog.csdn.net/u011676417/article/details/104678852