diff --git a/code/src/main/java/com/raorao/Test.java b/code/src/main/java/com/raorao/Test.java index 6262bfa4..7c195f38 100644 --- a/code/src/main/java/com/raorao/Test.java +++ b/code/src/main/java/com/raorao/Test.java @@ -1,35 +1,5 @@ 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; public static void main(String[] args) { -// //System.out.println(foo()); -// int r = new Test().foo(); -// //System.out.println(r); -// PriorityBlockingQueue aa; -// LinkedBlockingQueue ss; -// ss = new LinkedBlockingQueue<>(); -// ss.add(1); -// ArrayBlockingQueue s; -// LinkedBlockingQueue 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; - } - } } diff --git a/code/src/main/java/com/raorao/interview/meituan/M.java b/code/src/main/java/com/raorao/interview/meituan/M.java new file mode 100644 index 00000000..c8f65eb6 --- /dev/null +++ b/code/src/main/java/com/raorao/interview/meituan/M.java @@ -0,0 +1,11 @@ +package com.raorao.interview.meituan; + +/** + * . + * + * @author Xiong Raorao + * @since 2018-09-06-20:29 + */ +public class M { + +} diff --git a/code/src/main/java/com/raorao/interview/meituan/t1/Main.java b/code/src/main/java/com/raorao/interview/meituan/t1/Main.java new file mode 100644 index 00000000..e69de29b diff --git a/code/src/main/java/com/raorao/interview/meituan/t2/Main.java b/code/src/main/java/com/raorao/interview/meituan/t2/Main.java new file mode 100644 index 00000000..e69de29b diff --git a/code/src/main/java/com/raorao/sword/ListNode.java b/code/src/main/java/com/raorao/sword/ListNode.java new file mode 100644 index 00000000..d4375b68 --- /dev/null +++ b/code/src/main/java/com/raorao/sword/ListNode.java @@ -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; + } +} diff --git a/code/src/main/java/com/raorao/sword/Solution01.java b/code/src/main/java/com/raorao/sword/Solution01.java new file mode 100644 index 00000000..32fc42cc --- /dev/null +++ b/code/src/main/java/com/raorao/sword/Solution01.java @@ -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; + } +} diff --git a/code/src/main/java/com/raorao/sword/Solution02.java b/code/src/main/java/com/raorao/sword/Solution02.java new file mode 100644 index 00000000..14e43def --- /dev/null +++ b/code/src/main/java/com/raorao/sword/Solution02.java @@ -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(); + } +} diff --git a/code/src/main/java/com/raorao/sword/Solution03.java b/code/src/main/java/com/raorao/sword/Solution03.java new file mode 100644 index 00000000..7312709d --- /dev/null +++ b/code/src/main/java/com/raorao/sword/Solution03.java @@ -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 printListFromTailToHead(ListNode listNode) { + if (listNode == null) { + return new ArrayList<>(); + } + ListNode root = listNode; + Deque stack = new LinkedList<>(); + while (root != null) { + stack.push(root.val); + root = root.next; + } + ArrayList ret = new ArrayList<>(); + while (!stack.isEmpty()) { + ret.add(stack.pop()); + } + return ret; + } +} diff --git a/code/src/main/java/com/raorao/sword/Solution04.java b/code/src/main/java/com/raorao/sword/Solution04.java new file mode 100644 index 00000000..f96d127b --- /dev/null +++ b/code/src/main/java/com/raorao/sword/Solution04.java @@ -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; + } +} diff --git a/code/src/main/java/com/raorao/sword/Solution05.java b/code/src/main/java/com/raorao/sword/Solution05.java new file mode 100644 index 00000000..1e6417ef --- /dev/null +++ b/code/src/main/java/com/raorao/sword/Solution05.java @@ -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 stack1 = new Stack(); + Stack stack2 = new Stack(); + + 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; + } +} diff --git a/code/src/main/java/com/raorao/sword/Solution06.java b/code/src/main/java/com/raorao/sword/Solution06.java new file mode 100644 index 00000000..721230d4 --- /dev/null +++ b/code/src/main/java/com/raorao/sword/Solution06.java @@ -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); + } +} diff --git a/code/src/main/java/com/raorao/sword/Test.java b/code/src/main/java/com/raorao/sword/Test.java new file mode 100644 index 00000000..4831c442 --- /dev/null +++ b/code/src/main/java/com/raorao/sword/Test.java @@ -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 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); + } +} diff --git a/code/src/main/java/com/raorao/sword/TreeNode.java b/code/src/main/java/com/raorao/sword/TreeNode.java new file mode 100644 index 00000000..e409aa67 --- /dev/null +++ b/code/src/main/java/com/raorao/sword/TreeNode.java @@ -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; + } +}