剑指offer(二)java

来源:互联网 发布:兼职淘宝客服好不好做 编辑:程序博客网 时间:2024/05/21 11:24

1.输入一个链表,从尾到头打印链表每个节点的值。

  1. package test3;  
  2.   
  3. import java.util.ArrayList;  
  4.  class ListNode {  
  5.     int val;  
  6.     ListNode next = null;  
  7.   
  8.     ListNode(int val) {  
  9.         this.val = val;  
  10.     }  
  11. }  
  12. public class printFromTailtoHead {  
  13.       
  14.     public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {  
  15.         ArrayList<Integer> a = new ArrayList<Integer>();  
  16.         ListNode temp = listNode;  
  17.         while(temp != null){  
  18.             a.add(new Integer(temp.val));  
  19.             temp = temp.next;  
  20.         }  
  21.         Integer b ;  
  22.         for(int i=0; i<a.size()/2;i++){  
  23.             b = a.get(i);  
  24.             a.set(i, a.get(a.size()-i-1));  
  25.             a.set(a.size()-i-1,b);  
  26.         }  
  27.         return a;  
  28.     }  
  29.     public static void main(String args[]){  
  30.         ListNode a = new ListNode(1);  
  31.         a.next = new ListNode(2);  
  32.         ArrayList<Integer> b = printListFromTailToHead(a);  
  33.         System.out.println(b.get(0)+"----"+b.get(1));  
  34.     }  
  35.   
  36. }  
2.输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。

  1. public class Solution {  
  2.         public TreeNode reConstructBinaryTree(int [] pre,int [] in) {  
  3.              return reConstructBinaryTree0(pre, 0, pre.length, in, 0, in.length);  
  4.         }  
  5.       
  6.     public TreeNode reConstructBinaryTree0(int [] pre, int preStart, int preEnd, int [] in, int inStart, int inEnd) {  
  7.         if (preStart >= preEnd) { // way out  
  8.             return null;  
  9.         }  
  10.         int rootIndex = indexOf(in, inStart, inEnd, pre[preStart]);  
  11.         TreeNode root = new TreeNode(pre[preStart]);  
  12.         int leftPreStart = preStart + 1;  
  13.         int leftPreEnd = leftPreStart + (rootIndex - inStart);  
  14.         int leftInStart = inStart;  
  15.         int leftInEnd = rootIndex;  
  16.                 // 构建左子树  
  17.         root.left = reConstructBinaryTree0(pre, leftPreStart, leftPreEnd, in, leftInStart, leftInEnd);  
  18.           
  19.         int rightPreStart = leftPreEnd;  
  20.         int rightPreEnd = preEnd;  
  21.         int rightInStart = rootIndex + 1;  
  22.         int rightInEnd = inEnd;  
  23.                 // 构建右子树  
  24.         root.right = reConstructBinaryTree0(pre, rightPreStart, rightPreEnd, in, rightInStart, rightInEnd);  
  25.         return root;  
  26.     }  
  27.       
  28.         // 找下标  
  29.     public int indexOf(int[] array, int start, int end, int value) {  
  30.         for (int i = start; i < end; i++) {  
  31.             if (array[i] == value) {  
  32.                 return i;  
  33.             }  
  34.         }  
  35.         return -1;  
  36.     }  
  37. }  
3.用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

解法:有两个栈,栈1和栈2.当入栈的时候,我们将它全放进栈1中,当需要出栈的时候,我们将栈1出栈到栈2中,然后再将栈2依次出栈。出完栈之后,再把stack2中的数pop出push到stack1,接受下次的操作。所以入栈的时候,思路很简单,注意到要将int类型转为Integer类型,我们使用了new Integer(int);当需要出栈的时候,我们用API提供的方法while(stack1.isEmpty())来将所有栈1的元素压入栈2中,然后将栈2弹出就可以。这里又涉及到将Integer的类型转为int类型的方法Integer.intValue();

  1. import java.util.Stack;  
  2.   
  3. public class Solution {  
  4.     Stack<Integer> stack1 = new Stack<Integer>();  
  5.     Stack<Integer> stack2 = new Stack<Integer>();  
  6.       
  7.     public void push(int node) {  
  8.         stack1.push(new Integer(node));  
  9.   
  10.     }  
  11.   
  12.     public int pop() {  
  13.         int pop;  
  14.         while (!stack1.isEmpty()) {  
  15.             stack2.push(stack1.pop());  
  16.         }  
  17.         pop=stack2.pop().intValue();  
  18.         while(!stack2.isEmpty()){  
  19.             stack1.push(stack2.pop());  
  20.         }  
  21.         return pop;  
  22.     }  
  23. }  
4.把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。
输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。
例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。
NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。

  1. public static int minOfShiftedArray(int[] x){  
  2.         if(x==null||x.length==0){  
  3.             return -1;  
  4.         }  
  5.         int len=x.length;  
  6.         int low=0;  
  7.         int high=len-1;  
  8.         if(x[low]<x[high]){//if the array is not shifted actually,e.g. {1,2,3,4,5}  
  9.             return x[low];  
  10.         }  
  11.         int mid=0;  
  12.         while(low<high){  
  13.             mid=(low&high)+(low^high)/2;  
  14.             if(mid==low){//if there are only two elements left  
  15.                 return x[low]<=x[high]?x[low]:x[high];  
  16.             }  
  17.             if(x[mid]>=x[low]){  
  18.                 low=mid;  
  19.             }else{  
  20.                 high=mid;  
  21.             }  
  22.         }  
  23.         return x[mid];  
  24.     }  

0 0
原创粉丝点击