This commit is contained in:
xiongraorao 2018-09-06 21:27:07 +08:00
parent d90c453a20
commit 2fb7dfb2df
13 changed files with 293 additions and 63 deletions

View File

@ -1,35 +1,5 @@
package com.raorao; package com.raorao;
import afu.org.checkerframework.checker.igj.qual.I;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import sun.awt.image.ImageWatched.Link;
/** /**
* . * .
* *
@ -41,40 +11,7 @@ public class Test {
private int a; private int a;
public static void main(String[] args) { public static void main(String[] args) {
// //System.out.println(foo());
// int r = new Test().foo();
// //System.out.println(r);
// PriorityBlockingQueue<Integer> aa;
// LinkedBlockingQueue<Integer> ss;
// ss = new LinkedBlockingQueue<>();
// ss.add(1);
// ArrayBlockingQueue<Integer> s;
// LinkedBlockingQueue<Integer> a;
// FileInputStream fis;
// FileOutputStream fos;
// InputStreamReader isr;
// OutputStreamWriter osw;
// float b = Math.round(11.5);
// //System.out.println(b);
int[] t = new int[]{1,2};
int[] temp = t;
System.out.println(t[0]);
t[0]= 10;
System.out.println(temp[0]);
} }
public int foo() {
try {
System.out.println("sadfasdfasdf");
return a;
} catch (Exception e) {
e.printStackTrace();
return 3;
} finally {
System.out.println("finally: " + ++a);
return a;
}
}
} }

View File

@ -0,0 +1,11 @@
package com.raorao.interview.meituan;
/**
* .
*
* @author Xiong Raorao
* @since 2018-09-06-20:29
*/
public class M {
}

View File

@ -0,0 +1,17 @@
package com.raorao.sword;
/**
* .
*
* @author Xiong Raorao
* @since 2018-09-06-10:28
*/
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}

View File

@ -0,0 +1,29 @@
package com.raorao.sword;
/**
* .
*
* @author Xiong Raorao
* @since 2018-09-06-10:04
*/
public class Solution01 {
public boolean Find(int target, int[][] array) {
if (array == null || array[0].length == 0) {
return false;
}
int m = array.length;
int n = array[0].length;
for (int i = m - 1; i >= 0; i--) {
for (int j = 0; j < n; j++) {
if (target < array[i][j]) {
break;
}
if (target == array[i][j]) {
return true;
}
}
}
return false;
}
}

View File

@ -0,0 +1,32 @@
package com.raorao.sword;
/**
* .
*
* @author Xiong Raorao
* @since 2018-09-06-10:20
*/
public class Solution02 {
public String replaceSpace(StringBuffer sb) {
int oldLength = sb.length();
for (int i = 0; i < oldLength; i++) {
if (sb.charAt(i) == ' ') {
sb.append(" ");
}
}
int newLength = sb.length();
int oldIndex = oldLength - 1;
int newIndex = newLength - 1;
for (; (oldIndex >= 0 && newIndex > oldIndex); oldIndex--) {
if (sb.charAt(oldIndex) == ' ') {
sb.setCharAt(newIndex--, '0');
sb.setCharAt(newIndex--, '2');
sb.setCharAt(newIndex--, '%');
} else {
sb.setCharAt(newIndex--, sb.charAt(oldIndex));
}
}
return sb.toString();
}
}

View File

@ -0,0 +1,31 @@
package com.raorao.sword;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
/**
* .
*
* @author Xiong Raorao
* @since 2018-09-06-10:28
*/
public class Solution03 {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
if (listNode == null) {
return new ArrayList<>();
}
ListNode root = listNode;
Deque<Integer> stack = new LinkedList<>();
while (root != null) {
stack.push(root.val);
root = root.next;
}
ArrayList<Integer> ret = new ArrayList<>();
while (!stack.isEmpty()) {
ret.add(stack.pop());
}
return ret;
}
}

View File

@ -0,0 +1,45 @@
package com.raorao.sword;
/**
* 重建二叉树.
*
* 已知前序和中序遍历重建二叉树
*
* @author Xiong Raorao
* @since 2018-09-06-10:38
*/
public class Solution04 {
public TreeNode reConstructBinaryTree(int[] pre, int[] in) {
if (pre == null || in == null || pre.length != in.length) {
return null;
}
return construct(pre, in, 0, pre.length - 1, 0, in.length - 1);
}
private TreeNode construct(int[] pre, int[] in, int preStart, int preEnd, int inStart,
int inEnd) {
// 递归终止条件
if (preStart > preEnd || inStart > inEnd) {
return null;
}
int rootValue = pre[preStart];
TreeNode root = new TreeNode(rootValue);
// 找到根节点在中序遍历的位置
int rootInorder = inStart;
while (true) {
if (in[rootInorder] == rootValue) {
int leftLength = rootInorder - inStart;
root.left = construct(pre, in, preStart + 1, preStart + leftLength, inStart,
rootInorder - 1);
root.right = construct(pre, in, preStart + leftLength + 1, preEnd, rootInorder + 1, inEnd);
break;
} else {
rootInorder++;
}
}
return root;
}
}

View File

@ -0,0 +1,32 @@
package com.raorao.sword;
import java.util.Stack;
/**
* .
*
* @author Xiong Raorao
* @since 2018-09-06-11:36
*/
public class Solution05 {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
public int pop() {
int ret;
if (!stack2.isEmpty()) {
ret = stack2.pop();
} else {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
ret = stack2.pop();
}
return ret;
}
}

View File

@ -0,0 +1,30 @@
package com.raorao.sword;
/**
* 旋转数组的最小数字平多多面试题.
*
* @author Xiong Raorao
* @since 2018-09-06-11:38
*/
public class Solution06 {
public int minNumberInRotateArray(int[] array) {
return find(array, 0, array.length - 1);
}
private int find(int[] array, int start, int end) {
if (end - start == 1) {
return array[end];
}
int mid = start + (end - start) / 2;
if (array[start] <= array[mid]) {
// mid 属于旋转过去的
start = mid;
} else if (array[end] >= array[mid]) {
end = mid;
}
return find(array, start, end);
}
}

View File

@ -0,0 +1,48 @@
package com.raorao.sword;
import java.util.ArrayList;
import java.util.Arrays;
/**
* .
*
* @author Xiong Raorao
* @since 2018-09-06-10:15
*/
public class Test {
public static void main(String[] args) {
//test01();
//test03();
test06();
}
public static void test01() {
Solution01 s = new Solution01();
int[][] data = new int[][] {
{1, 2, 8, 9},
{2, 4, 9, 12},
{4, 7, 10, 13},
{6, 8, 11, 15}
};
boolean r = s.Find(7, data);
System.out.println(r);
}
public static void test03() {
Solution03 s = new Solution03();
ListNode root = new ListNode(67);
root.next = new ListNode(0);
root.next.next = new ListNode(24);
root.next.next.next = new ListNode(58);
ArrayList<Integer> ret = s.printListFromTailToHead(root);
System.out.println(Arrays.toString(ret.toArray()));
}
public static void test06() {
Solution06 s = new Solution06();
int[] array = new int[] {3, 4, 5, 1, 2};
int r = s.minNumberInRotateArray(array);
System.out.println(r);
}
}

View File

@ -0,0 +1,18 @@
package com.raorao.sword;
/**
* .
*
* @author Xiong Raorao
* @since 2018-09-06-10:38
*/
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}