Java 剑指offer 1-20题

  • 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
public class Solution {
    public boolean Find(int target, int [][] array) {
        int n=array.length;
        int nn = array[0].length;
        int i=n-1,j=0;
        if(n==0){
            return false;
        }
        while(i>=0 && j <nn){
            System.out.println(array[i][j]);
            if(array[i][j]>target){
                i--;
            }
            else if(array[i][j]<target){
                j++;
            }
            else {
                return true;
            }
        }
        return false;
    }
}
  • 请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
public class Solution {
    public String replaceSpace(StringBuffer str) {
        int index =str.indexOf(" ");
        if(str.length()==0){
            return "";
        }
        while(index!=-1){
            str.replace(index,index+1,"%20");
            index =str.indexOf(" ");
        }
        String str1 = str.toString();
        return str1;
    }
}
  • 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
import java.util.Stack;

public class Solution {
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();
    
    public void push(int node) {
        stack1.push(node);
    }
    
    public int pop() {
        if(stack2.empty()){
        while(!stack1.empty()){
            stack2.push(stack1.pop());
        }
        }
        return stack2.pop();
    }
}
  • 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
    
    public class Solution {
         private TreeNode reConstructBinaryTree(int [] pre,int startPre,int endPre,int [] in,int startIn,int endIn) {
            if(startPre>endPre||startIn>endIn)
                return null;
            TreeNode root=new TreeNode(pre[startPre]);
            for(int i=startIn;i<=endIn;i++)
                if(in[i]==pre[startPre]){
                    root.left=reConstructBinaryTree(pre,startPre+1,startPre+i-startIn,in,startIn,i-1);
                    root.right=reConstructBinaryTree(pre,i-startIn+startPre+1,endPre,in,i+1,endIn);
                          break;
                }
                     
            return root;
        }
        public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
            TreeNode root=reConstructBinaryTree(pre,0,pre.length-1,in,0,in.length-1);
            return root;
        }
    }

    把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。

  • import java.util.ArrayList;
    public class Solution {
        public int minNumberInRotateArray(int [] array) {
            int low = 0 ;//0//2//3
            int high = array.length - 1;//5//4
            while(low < high){
                  int mid = (high + low)/2;
                  if(array[mid]>array[low]){
                      low = mid;
                  }else if(array[mid] == array[low]){
                      low+=1;
                  }else{
                      high =mid;
                  }
    
            }
            return array[low];
        }
    }

    一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。

  • 递归

  • public class Solution {
        public int JumpFloor(int target) {
            if (target == 1) {
                return 1;
            } else if (target ==2) {
                return 2;
            } else {
                return  JumpFloor(target-1)+JumpFloor(target-2);
            }
        }
    }

    一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。

  • 2(n-1)用移位

  • public class Solution {
        public int JumpFloorII(int target) {
            if(target==1){
                return 1;
            }
            if(target==2){
                return 2;
            }
            return 2<<target-2;
        }
    }

     

  • 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?

  • public class Solution {
        public int RectCover(int target) {
            if(target==0) return 0;
            if(target == 1) return 1;
            if(target == 2) return 2;
            return RectCover(target-1)+RectCover(target-2);
        }
    }

    输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。

  • public class Solution {
        public int NumberOf1(int n) {
            int count = 0;
            while(n!=0){
                count++;
                n = n& (n-1);
            }
            return count;
        }
    }

    最简单的思路则是Integer.toBinaryString(n),这个自己领悟,但是这个回答不大好。

  • 给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。

  • public class Solution {
        public double Power(double base, int exponent) {
            if(base==0) return 0; 
            if(exponent==1) return base;
            if(exponent==0) return 1;
            double  result=1;
            for(int i=0;i<Math.abs(exponent);i++){
                result*=base;
            }
            if(exponent<0){
                result=1/result;
            }
            return result;            
      }
    } 

    输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。

  • public class Solution {
        public void reOrderArray(int [] array) {
            int count = 0;
            for(int i=0;i<array.length;i++){
                if(array[i]%2==1){
                    count++;
                    for(int j=i;j>=count;j--){
                        int temp = array[j];
                        array[j] = array[j-1];
                        array[j-1] = temp;
                    }
                }
            }
        }
    }

    输入一个链表,输出该链表中倒数第k个结点。

  • public class Solution {
        public ListNode FindKthToTail(ListNode head,int k) {
            if(head == null || k ==0) return null;
            ListNode a = head;
            ListNode b = head;
            for (int i = 0; i < k-1; i++) {
                if(b.next!=null) b = b.next;
                else return null;
            }
            while (b.next!=null){
                a = a.next;
                b = b.next;
            }
            return a;
        }
    }

    定义两个节点同时指向头节点,让其中一个向前移动k个距离,然后两个节点一起向前。

  • 输入一个链表,反转链表后,输出新链表的表头。

  • public class Solution {
        public ListNode ReverseList(ListNode head) {
            if(head == null ) return null;
            ListNode a = null;
            ListNode b = null;
            while (head !=null){
                b=head.next;
                head.next=a;
                a=head;
                head=b;
            }
            return a;
        }
    }

    输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

  • public class Solution {
        public ListNode Merge(ListNode list1,ListNode list2) {
            if(list1 == null) return list2;
            if(list2 == null) return list1;
            ListNode list = null;
            ListNode list3 = null;
            while(list1!=null && list2!=null){
                if(list1.val >= list2.val){
                    if(list3==null) list3= list=list2;
                    else {
                        list.next = list2;
                        list = list.next;
                    }
                    list2=list2.next;
                }else{
                    if(list3==null) list3= list=list1;
                    else{
                        list.next = list1;
                        list = list.next;
                    }
                    list1=list1.next;
                }
    
            }
            if(list1 == null){
                while (list2!=null){
                    list.next = list2;
                    list2=list2.next;
                    list = list.next;
                }
            } else if(list2 == null){
                while (list1!=null){
                    list.next = list1;
                    list1=list1.next;
                    list = list.next;
                }
            }
            return list3;
        }
    }

    输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)

  • public class Solution {
        public boolean HasSubtree(TreeNode root1,TreeNode root2) {
            if(root2==null) return false;
            if(root1==null && root2!=null) return false;
            boolean flag = false;
            if(root1.val == root2.val){
                flag = isSubTree(root1,root2);
            }
            if(flag == false){
                flag=HasSubtree(root1.left,root2);
                if(flag == false) flag=HasSubtree(root1.right,root2);
            }
            return flag;
        }
        private boolean isSubTree(TreeNode root1, TreeNode root2) {
            if(root2==null) return true;
            if(root1==null && root2!=null) return false;
            if(root1.val == root2.val){
                return isSubTree(root1.left,root2.left) && isSubTree(root1.right,root2.right);
            }else return false;
        }
    }

    操作给定的二叉树,将其变换为源二叉树的镜像。

  • 递归

  • public class Solution {
        public void Mirror(TreeNode root) {
            TreeNode temp;
            if(root != null){
                temp = root.left;
                root.left = root.right;
                root.right = temp;
                if(root.left!= null) Mirror(root.left);
                if(root.right!= null) Mirror(root.right);
            }
        }
    }

    输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

  • import java.util.ArrayList;
    public class Solution {
        public ArrayList<Integer> printMatrix(int [][] matrix) {
           if(matrix == null) return null;
            ArrayList<Integer> a = new ArrayList<>();
            int n = matrix[0].length;//4
            int m = matrix.length;//5
            for (int k = 0; k < (Math.min(n,m)+1)/2; k++) {//k=1
                for (int i = k; i < n-k; i++) {
                    a.add(matrix[k][i]);
    
                }
                for (int i = 1+k; i <m-k ; i++) {
                    a.add(matrix[i][n-1-k]);
    
                }
                for (int i = n-2-k; i >=k&&(m-k-1!=k); i--) {
                    a.add(matrix[m-1-k][i]);
    
                }
                for (int i = m-2-k; i >k&&(n-k-1!=k) ; i--) {
                    a.add(matrix[i][k]);
                }
    
            }
            return a;
        }
    }

    定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。

  • import java.util.Stack;
    import java.util.Iterator;
    public class Solution {
    
        Stack<Integer> stack = new Stack<Integer>();
        public void push(int node) {
            stack.push(node);
        }
        
        public void pop() {
            stack.pop();
        }
        
        public int top() {
            return stack.peek();
        }
        
        public int min() {
            int min = stack.peek();
            int temp = 0;
            Iterator<Integer> it= stack.iterator();
            while(it.hasNext()){
                temp =it.next();
                if(temp < min){
                    min = temp;
                }
            }
            return min;
        }
    }

     

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值