劍指offer(二)java
阿新 • • 發佈:2019-02-10
1.輸入一個連結串列,從尾到頭列印連結串列每個節點的值。
- package test3;
- import java.util.ArrayList;
- class ListNode {
- int val;
- ListNode next = null;
- ListNode(int val) {
- this.val = val;
- }
- }
- publicclass printFromTailtoHead {
-
publicstatic ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
- ArrayList<Integer> a = new ArrayList<Integer>();
- ListNode temp = listNode;
- while(temp != null){
- a.add(new Integer(temp.val));
- temp = temp.next;
- }
- Integer b ;
- for(int i=0; i<a.size()/2;i++){
-
b = a.get(i);
- a.set(i, a.get(a.size()-i-1));
- a.set(a.size()-i-1,b);
- }
- return a;
- }
- publicstaticvoid main(String args[]){
- ListNode a = new ListNode(1);
- a.next = new ListNode(2);
-
ArrayList<Integer> b = printListFromTailToHead(a);
- System.out.println(b.get(0)+"----"+b.get(1));
- }
- }
- public class Solution {
- public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
- return reConstructBinaryTree0(pre, 0, pre.length, in, 0, in.length);
- }
- public TreeNode reConstructBinaryTree0(int [] pre, int preStart, int preEnd, int [] in, int inStart, int inEnd) {
- if (preStart >= preEnd) { // way out
- return null;
- }
- int rootIndex = indexOf(in, inStart, inEnd, pre[preStart]);
- TreeNode root = new TreeNode(pre[preStart]);
- int leftPreStart = preStart + 1;
- int leftPreEnd = leftPreStart + (rootIndex - inStart);
- int leftInStart = inStart;
- int leftInEnd = rootIndex;
- // 構建左子樹
- root.left = reConstructBinaryTree0(pre, leftPreStart, leftPreEnd, in, leftInStart, leftInEnd);
- int rightPreStart = leftPreEnd;
- int rightPreEnd = preEnd;
- int rightInStart = rootIndex + 1;
- int rightInEnd = inEnd;
- // 構建右子樹
- root.right = reConstructBinaryTree0(pre, rightPreStart, rightPreEnd, in, rightInStart, rightInEnd);
- return root;
- }
- // 找下標
- public int indexOf(int[] array, int start, int end, int value) {
- for (int i = start; i < end; i++) {
- if (array[i] == value) {
- return i;
- }
- }
- return -1;
- }
- }
解法:有兩個棧,棧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();
- import java.util.Stack;
- publicclass Solution {
- Stack<Integer> stack1 = new Stack<Integer>();
- Stack<Integer> stack2 = new Stack<Integer>();
- publicvoid push(int node) {
- stack1.push(new Integer(node));
- }
- publicint pop() {
- int pop;
- while (!stack1.isEmpty()) {
- stack2.push(stack1.pop());
- }
- pop=stack2.pop().intValue();
- while(!stack2.isEmpty()){
- stack1.push(stack2.pop());
- }
- return pop;
- }
- }
輸入一個非遞減排序的陣列的一個旋轉,輸出旋轉陣列的最小元素。
例如陣列{3,4,5,1,2}為{1,2,3,4,5}的一個旋轉,該陣列的最小值為1。
NOTE:給出的所有元素都大於0,若陣列大小為0,請返回0。
- public static int minOfShiftedArray(int[] x){
- if(x==null||x.length==0){
- return -1;
- }
- int len=x.length;
- int low=0;
- int high=len-1;
- if(x[low]<x[high]){//if the array is not shifted actually,e.g. {1,2,3,4,5}
- return x[low];
- }
- int mid=0;
- while(low<high){
- mid=(low&high)+(low^high)/2;
- if(mid==low){//if there are only two elements left
- return x[low]<=x[high]?x[low]:x[high];
- }
- if(x[mid]>=x[low]){
- low=mid;
- }else{
- high=mid;
- }
- }
- return x[mid];
- }