=0){
32 | if (matrix[row][col] == key){
33 | return 1;
34 | } else if (matrix[row][col] < key){
35 | row++;
36 | } else {
37 | col--;
38 | }
39 | }
40 | return 0;
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/src/zzzTest/StaticFunctionTest.java:
--------------------------------------------------------------------------------
1 | package zzzTest;
2 |
3 | import java.util.List;
4 |
5 | public class StaticFunctionTest {
6 | static class test {
7 | int id;
8 | String name;
9 |
10 | public test(int id, String name) {
11 | this.id = id;
12 | this.name = name;
13 | }
14 |
15 | @Override
16 | public String toString() {
17 | return "test{" +
18 | "id=" + id +
19 | ", name='" + name + '\'' +
20 | '}';
21 | }
22 |
23 | public int getId() {
24 | return id;
25 | }
26 |
27 | public void setId(int id) {
28 | this.id = id;
29 | }
30 |
31 | public String getName() {
32 | return name;
33 | }
34 |
35 | public void setName(String name) {
36 | this.name = name;
37 | }
38 | }
39 |
40 | public static void main(String[] args) {
41 | String xx = null;
42 |
43 | test t = new test(1, xx);
44 | System.out.println(t.toString());
45 | setTestId(t);
46 | System.out.println(t.toString());
47 | }
48 |
49 | public static void setTestId(test t) {
50 | t.setId(2);
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/nowcoder/AlgorithmPrototype/SlidingWindowMaxValue.java:
--------------------------------------------------------------------------------
1 | package nowcoder.AlgorithmPrototype;
2 |
3 | import Standard.stdOut;
4 |
5 | import java.util.LinkedList;
6 |
7 | /**
8 | * 双端队列的操作
9 | *
10 | * Created by nibnait on 2016/9/13.
11 | */
12 | public class SlidingWindowMaxValue {
13 |
14 | public static void main(String[] args) {
15 | int[] arr = {4, 3, 5, 4, 3, 3, 6, 7};
16 | int w = 3;
17 | stdOut.print(getWindowMaxValue(arr, w));
18 | }
19 |
20 | private static int[] getWindowMaxValue(int[] arr, int w) {
21 |
22 | if (arr == null || w < 1 || arr.length < w) {
23 | return null;
24 | }
25 |
26 | LinkedList deque = new LinkedList();
27 | int[] res = new int[arr.length - w + 1];
28 | int cnt = 0;
29 | for (int i = 0; i < arr.length; i++) {
30 | while (!deque.isEmpty() && arr[deque.peekFirst()] <= arr[i]) {
31 | deque.pollLast();
32 | }
33 | deque.addLast(i);
34 | if (deque.peekFirst() <= i - w) {
35 | deque.pollFirst();
36 | }
37 | if (i >= w - 1) {
38 | res[cnt++] = arr[deque.peekFirst()];
39 | }
40 | }
41 |
42 | return res;
43 | }
44 |
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/src/nowcoder/b_2nd_Season/bc160803/src/CompleteTreeNodeNumber.java:
--------------------------------------------------------------------------------
1 | package nowcoder.b_2nd_Season.bc160803.src;
2 |
3 | public class CompleteTreeNodeNumber {
4 |
5 | public static class Node {
6 | public int value;
7 | public Node left;
8 | public Node right;
9 |
10 | public Node(int data) {
11 | this.value = data;
12 | }
13 | }
14 |
15 | public static int nodeNum(Node head) {
16 | if (head == null) {
17 | return 0;
18 | }
19 | return bs(head, 1, mostLeftLevel(head, 1));
20 | }
21 |
22 | public static int bs(Node node, int l, int h) {
23 | if (l == h) {
24 | return 1;
25 | }
26 | if (mostLeftLevel(node.right, l + 1) == h) {
27 | return (1 << (h - l)) + bs(node.right, l + 1, h);
28 | } else {
29 | return (1 << (h - l - 1)) + bs(node.left, l + 1, h);
30 | }
31 | }
32 |
33 | public static int mostLeftLevel(Node node, int level) {
34 | while (node != null) {
35 | level++;
36 | node = node.left; //一路向左
37 | }
38 | return level - 1; //返回高度
39 | }
40 |
41 | public static void main(String[] args) {
42 | Node head = new Node(1);
43 | head.left = new Node(2);
44 | head.right = new Node(3);
45 | head.left.left = new Node(4);
46 | head.left.right = new Node(5);
47 | head.right.left = new Node(6);
48 | System.out.println(nodeNum(head));
49 |
50 | }
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/src/nowcoder/b_2nd_Season/bj160928/IsSearchBinaryTree.java:
--------------------------------------------------------------------------------
1 | package nowcoder.b_2nd_Season.bj160928;
2 |
3 | import Standard.BinaryTreeNode;
4 |
5 | /**
6 | * 判断一棵二叉树是否为搜索二叉树
7 | *
8 | * Created by nibnait on 2016/10/9.
9 | */
10 | public class IsSearchBinaryTree {
11 |
12 | public static boolean isBST(BinaryTreeNode head) {
13 | if (head == null) {
14 | return true;
15 | }
16 | boolean res = true;
17 | BinaryTreeNode pre = null;
18 | BinaryTreeNode cur1 = head;
19 | BinaryTreeNode cur2 = null;
20 | while (cur1 != null) {
21 | cur2 = cur1.left;
22 | if (cur2 != null) {
23 | while (cur2.right != null && cur2.right != cur1) {
24 | cur2 = cur2.right;
25 | }
26 | if (cur2.right == null) {
27 | cur2.right = cur1;
28 | cur1 = cur1.left;
29 | continue;
30 | } else {
31 | cur2.right = null;
32 | }
33 | }
34 | if (pre != null && pre.value > cur1.value) {
35 | res = false;
36 | }
37 | pre = cur1;
38 | cur1 = cur1.right;
39 | }
40 | return res;
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/src/nowcoder/b_2nd_Season/bd160810/SlidingWindowMaxValue.java:
--------------------------------------------------------------------------------
1 | package nowcoder.b_2nd_Season.bd160810;
2 |
3 | import Standard.stdOut;
4 |
5 | import java.util.LinkedList;
6 |
7 | /**
8 | * 双端队列的操作
9 | *
10 | * Created by nibnait on 2016/9/13.
11 | */
12 | public class SlidingWindowMaxValue {
13 |
14 | public static void main(String[] args) {
15 | int[] arr = {4, 3, 5, 4, 3, 3, 6, 7};
16 | int w = 3;
17 | stdOut.print(getWindowMaxValue(arr, w));
18 | }
19 |
20 | private static int[] getWindowMaxValue(int[] arr, int w) {
21 |
22 | if (arr == null || w < 1 || arr.length < w) {
23 | return null;
24 | }
25 |
26 | LinkedList deque = new LinkedList();
27 | int[] res = new int[arr.length - w + 1];
28 | int cnt = 0;
29 | for (int i = 0; i < arr.length; i++) {
30 | while (!deque.isEmpty() && arr[deque.peekFirst()] <= arr[i]) {
31 | deque.pollLast();
32 | }
33 | deque.addLast(i);
34 | if (deque.peekFirst() <= i - w) {
35 | deque.pollFirst();
36 | }
37 | if (i >= w - 1) {
38 | res[cnt++] = arr[deque.peekFirst()];
39 | }
40 | }
41 |
42 | return res;
43 | }
44 |
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/src/nowcoder/b_2nd_Season/bj160928/IsCompleteBinaryTree.java:
--------------------------------------------------------------------------------
1 | package nowcoder.b_2nd_Season.bj160928;
2 |
3 | import Standard.BinaryTreeNode;
4 |
5 | import java.util.LinkedList;
6 | import java.util.Queue;
7 |
8 | /**
9 | * 判断一棵二叉树是否为 完全二叉树
10 | * Created by nibnait on 2016/10/9.
11 | */
12 | public class IsCompleteBinaryTree {
13 |
14 | public static boolean isCBT(BinaryTreeNode head) {
15 | if (head == null) {
16 | return true;
17 | }
18 | Queue queue = new LinkedList(); //按层遍历
19 | boolean leaf = false;
20 | BinaryTreeNode l = null;
21 | BinaryTreeNode r = null;
22 | queue.offer(head);
23 | while (!queue.isEmpty()) {
24 | head = queue.poll();
25 | l = head.left;
26 | r = head.right;
27 | if ( (leaf && (l != null || r != null)) || (l == null && r != null) ) {
28 | //当前是叶节点的阶段 && head居然还有孩子 //不管哪个阶段,此结点只要有右无左
29 | return false;
30 | }
31 | if (l != null) {
32 | queue.offer(l);
33 | }
34 | if (r != null) {
35 | queue.offer(r);
36 | } else { //有左无右 或 无左无右
37 | leaf = true;
38 | }
39 | }
40 | return true;
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/src/SwordOffer/e31_连续子数组的最大和.java:
--------------------------------------------------------------------------------
1 | package SwordOffer;
2 |
3 | /**
4 | * 题目:输入一个整型数组,数组里有正数也有负数。数组中一个或连续的多个整数组成一个子数组。求所有子数组的和的最大值。
5 | * 要求时间复杂度为 O(n)。
6 | *
7 | * /src/nowcoder/b_2nd_Season/bb160727/README.md:
8 | * 左神做法:
9 | cur 依次累加各个元素,一旦cur为负数时,则将cur清为零。
10 | 并尝试更新一次result(最大值)
11 | 最终返回result即为子数组的最大累加和
12 |
13 | 解释:
14 | 因为最大和的子数组:其任意数量的前缀一定不为负。
15 | 也就是 cur如果没有累加出到负数,就继续往下走。 即模拟了“前缀不可能为负数”的情况。
16 |
17 | *
18 | *
19 | * Created by nibnait on 2016/10/1.
20 | */
21 | public class e31_连续子数组的最大和 {
22 |
23 | public static void main(String[] args) {
24 | int[] data = {1, -2, 3, 10, -4, 7, 2, -5};
25 | int[] data2 = {-2, -8, -1, -5, -9};
26 | int[] data3 = {2, 8, 1, 5, 9};
27 | System.out.println(findGreatestSumOfSubArray(data));
28 | System.out.println(findGreatestSumOfSubArray(data2));
29 | System.out.println(findGreatestSumOfSubArray(data3));
30 | }
31 |
32 | private static int findGreatestSumOfSubArray(int[] arr) {
33 | if (arr==null || arr.length<=0){
34 | return 0;
35 | }
36 |
37 | int cur = arr[0];
38 | int res = cur;
39 | for (int i = 1; i < arr.length; i++) {
40 | cur += arr[i];
41 | res = Math.max(res, cur);
42 | cur = cur>0? cur:0;
43 | }
44 | return res;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/zzzTest/JsonParseTest/certificationConvert/CertificationDto.java:
--------------------------------------------------------------------------------
1 | package zzzTest.JsonParseTest.certificationConvert;
2 |
3 | public class CertificationDto {
4 | private String created_at;
5 | private String operator_id;
6 | private String operator_type;
7 | private String restaurant_id;
8 | private String updated_at;
9 |
10 | public String getCreated_at() {
11 | return created_at;
12 | }
13 |
14 | public void setCreated_at(String created_at) {
15 | this.created_at = created_at;
16 | }
17 |
18 | public String getOperator_id() {
19 | return operator_id;
20 | }
21 |
22 | public void setOperator_id(String operator_id) {
23 | this.operator_id = operator_id;
24 | }
25 |
26 | public String getOperator_type() {
27 | return operator_type;
28 | }
29 |
30 | public void setOperator_type(String operator_type) {
31 | this.operator_type = operator_type;
32 | }
33 |
34 | public String getRestaurant_id() {
35 | return restaurant_id;
36 | }
37 |
38 | public void setRestaurant_id(String restaurant_id) {
39 | this.restaurant_id = restaurant_id;
40 | }
41 |
42 | public String getUpdated_at() {
43 | return updated_at;
44 | }
45 |
46 | public void setUpdated_at(String updated_at) {
47 | this.updated_at = updated_at;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/nowcoder/b_2nd_Season/bg160831/src/KMPAlgorithm.java:
--------------------------------------------------------------------------------
1 | package nowcoder.b_2nd_Season.bg160831.src;
2 |
3 | public class KMPAlgorithm {
4 |
5 | public static int getIndexOf(String s, String m) {
6 | if (s == null || m == null || m.length() < 1 || s.length() < m.length()) {
7 | return -1;
8 | }
9 | char[] ss = s.toCharArray();
10 | char[] ms = m.toCharArray();
11 | int si = 0;
12 | int mi = 0;
13 | int[] next = getNextArray(ms);
14 | while (si < ss.length && mi < ms.length) {
15 | if (ss[si] == ms[mi]) {
16 | si++;
17 | mi++;
18 | } else if (next[mi] == -1) {
19 | si++;
20 | } else {
21 | mi = next[mi];
22 | }
23 | }
24 | return mi == ms.length ? si - mi : -1;
25 | }
26 |
27 | public static int[] getNextArray(char[] ms) {
28 | if (ms.length == 1) {
29 | return new int[] { -1 };
30 | }
31 | int[] next = new int[ms.length];
32 | next[0] = -1;
33 | next[1] = 0;
34 | int pos = 2; //当前位置
35 | int cn = 0; //最长匹配前缀字串后面的那个字符位置
36 | while (pos < next.length) {
37 | if (ms[pos - 1] == ms[cn]) {
38 | next[pos++] = ++cn;
39 | } else if (cn > 0) {
40 | cn = next[cn];
41 | } else {
42 | next[pos++] = 0;
43 | }
44 | }
45 | return next;
46 | }
47 |
48 | public static void main(String[] args) {
49 | String str = "abcabcababaccc";
50 | String match = "ababa";
51 | System.out.println(getIndexOf(str, match));
52 |
53 | }
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/src/zzzTest/HangzhouFdaTest/util/aes/AES.java:
--------------------------------------------------------------------------------
1 | package zzzTest.HangzhouFdaTest.util.aes;
2 |
3 | import java.io.IOException;
4 | import java.io.UnsupportedEncodingException;
5 | import java.security.InvalidKeyException;
6 | import java.security.NoSuchAlgorithmException;
7 | import javax.crypto.BadPaddingException;
8 | import javax.crypto.IllegalBlockSizeException;
9 | import javax.crypto.NoSuchPaddingException;
10 |
11 | public class AES {
12 | static String spec = "9238513401340235";
13 |
14 | public AES() {
15 | }
16 |
17 | public String encrypt(String content, String key) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
18 | AESencrp aeSencrp = new AESencrp();
19 | aeSencrp.setALGO("AES");
20 | aeSencrp.setKeyValue(key.getBytes());
21 | return aeSencrp.encrypt(content);
22 | }
23 |
24 | public String decrypt(String src, String bm, String key) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, IOException {
25 | AESencrp aeSencrp = new AESencrp();
26 | aeSencrp.setALGO("AES");
27 | aeSencrp.setKeyValue(key.getBytes());
28 | return aeSencrp.decrypt(src, bm);
29 | }
30 |
31 | public static void main(String[] args) throws Exception {
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/nowcoder/b_2nd_Season/bc160803/src/MaxSubMatrixSumLessK.java:
--------------------------------------------------------------------------------
1 | package nowcoder.b_2nd_Season.bc160803.src;
2 |
3 | import Standard.BinarySearch;
4 |
5 | public class MaxSubMatrixSumLessK {
6 |
7 | public static int maxSubMatrixSumLessThanK(int[][] m, int sum) {
8 | if (m == null || m.length == 0 || m[0] == null || m[0].length == 0) {
9 | return 0;
10 | }
11 | int res = 0;
12 | for (int i = 0; i < m.length; i++) {
13 | int[] sumArr = new int[m[0].length];
14 | for (int j = i; j < m.length; j++) {
15 | for (int k = 0; k < m[0].length; k++) {
16 | sumArr[k] += m[j][k];
17 | }
18 | res = Math.max(res, (j - i + 1) * maxLength(sumArr, sum));
19 | }
20 | }
21 | return res;
22 | }
23 |
24 | public static int maxLength(int[] arr, int k) {
25 | int[] h = new int[arr.length + 1];
26 | int sum = 0;
27 | h[0] = sum;
28 | for (int i = 0; i != arr.length; i++) {
29 | sum += arr[i];
30 | h[i + 1] = Math.max(sum, h[i]);
31 | }
32 | sum = 0;
33 | int res = 0;
34 | int pre = 0;
35 | int len = 0;
36 | for (int i = 0; i != arr.length; i++) {
37 | sum += arr[i];
38 | pre = BinarySearch.search(h, sum - k);
39 | len = pre == -1 ? 0 : i - pre + 1;
40 | res = Math.max(res, len);
41 | }
42 | return res;
43 | }
44 |
45 | public static void main(String[] args) {
46 | int[][] matrix = { { 1, 0, 1 }, { 0, -2, 3 } };
47 | System.out.println(maxSubMatrixSumLessThanK(matrix, 2));
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/src/zzzTest/JsonParseTest/certificationConvert/RunshopDto.java:
--------------------------------------------------------------------------------
1 | package zzzTest.JsonParseTest.certificationConvert;
2 |
3 | public class RunshopDto {
4 | private String dom_id;
5 | private String source;
6 | private String user_id;
7 | private String user_name;
8 | private String user_email;
9 | private String created_at;
10 |
11 | public String getCreated_at() {
12 | return created_at;
13 | }
14 |
15 | public void setCreated_at(String created_at) {
16 | this.created_at = created_at;
17 | }
18 |
19 | public String getDom_id() {
20 | return dom_id;
21 | }
22 |
23 | public void setDom_id(String dom_id) {
24 | this.dom_id = dom_id;
25 | }
26 |
27 | public String getSource() {
28 | return source;
29 | }
30 |
31 | public void setSource(String source) {
32 | this.source = source;
33 | }
34 |
35 | public String getUser_id() {
36 | return user_id;
37 | }
38 |
39 | public void setUser_id(String user_id) {
40 | this.user_id = user_id;
41 | }
42 |
43 | public String getUser_name() {
44 | return user_name;
45 | }
46 |
47 | public void setUser_name(String user_name) {
48 | this.user_name = user_name;
49 | }
50 |
51 | public String getUser_email() {
52 | return user_email;
53 | }
54 |
55 | public void setUser_email(String user_email) {
56 | this.user_email = user_email;
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/SwordOffer/f42_2$左旋转字符串.java:
--------------------------------------------------------------------------------
1 | package SwordOffer;
2 |
3 | import Standard.StringUtils;
4 |
5 | import static SwordOffer.f42_1$翻转单词顺序.Reverse;
6 |
7 | /**
8 | * 题目二:字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。
9 | * 比如输入字符串“abcdefg”和数字 2,
10 | * 该函数将返回左旋转 2 位得到的结果“cdefgab”。
11 | *
12 | *
13 | * 【解】:与上一题类似:
14 | * 第一步,翻转前半部分的字符:ba cdefg
15 | * 第二步,翻转后半部分的字符:ba gfedc
16 | * 第三部,翻转整个字符串:cdegfab
17 | *
18 | * 【左神做法】:/src/nowcoder/b_2nd_Season/bk161012/src/
19 | *
20 | * `小块换过来的东西,定住!
21 | *
22 | *
23 | * Created by nibnait on 2016/10/2.
24 | */
25 | public class f42_2$左旋转字符串 {
26 |
27 | public static void main(String[] args) {
28 | System.out.println(new String(leftRotateString("abcdefg", 2)));
29 | System.out.println(new String(leftRotateString("abcdefg", 1)));
30 | System.out.println(new String(leftRotateString("abcdefg", 6)));
31 | System.out.println(new String(leftRotateString("abcdefg", 7)));
32 | System.out.println(new String(leftRotateString("abcdefg", 0)));
33 | }
34 |
35 | private static String leftRotateString(String str, int n) {
36 | if (StringUtils.isBlank(str) || n<=0 || n>=str.length()){ //n==0,n==str.length 都不用翻转!
37 | return str;
38 | }
39 | char[] chars = str.toCharArray();
40 | Reverse(chars, 0, n-1);
41 | Reverse(chars, n, chars.length-1);
42 | Reverse(chars, 0, chars.length-1);
43 | return new String(chars);
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/nowcoder/b_2nd_Season/ba160720/README.md:
--------------------------------------------------------------------------------
1 | 课程回顾:[http://www.nowcoder.com/live/11/1/1](http://www.nowcoder.com/live/11/1/1)
2 | 课件下载:[https://pan.baidu.com/s/1hs8WGh2](https://pan.baidu.com/s/1hs8WGh2)
3 |
4 | ## 1. 给定一个N*2 的二维数组,看作是一个个二元组,例如[[a1,b1],[a2,b2],[a3,b3]],
5 | 规定:一个如果想把二元组甲放在二元组乙上,甲中的a 值必须大于乙中的a 值,甲中的b
6 | 值必须大于乙中的b 值。如果在二维数组中随意选择二元组,请问二元组最多可以往上摞
7 | 几个?
8 | 例如:[[5,4],[6,4],[6,7],[2,3]], 最大数量可以摞3 个,[2,3] => [5,4] => [6,7]
9 | 要求:实现时间复杂度O(N*logN)的解法
10 |
11 | 最长递增子序列:
12 | h(i):当必须以arr的第i号元素结尾的情况下 最长递增子序列的长度的值。
13 |
14 | (2)h(i):假设我当前遍历到cur这个数时,有效区中,h(i)遍历到 当前时刻为止,长度为i+1的最长递增子序列(子数组)的最小**末尾**
15 |
16 | 排序策略:
17 | - 二元组中两个元素是同等重要的!
18 | - 最优解:先 a 小-->大,(a = a', 再 b 大-->小 排序)。
19 |
20 |
21 |
22 | ## 2.给定一个非负数的数组,代表一个容器。例如数组[0,1,0,2,1,0,1,3,2,1,2,1],就是
23 | 以下图形中黑色的部分。如果用这个容器接水的话,请问可以接多少水?还以这个数组为例,
24 | 可以接6 格水,就是以下图形中蓝色的部分。
25 | 要求:实现时间复杂度O(N),额外空间复杂度O(1)的解法
26 |
27 | - 判断有效的波峰波谷!
28 | water[i] = 取两侧的最大值,即可求出此位置的最大水量
29 | 加速求两边的最大值:
30 | 两个辅助数组,
31 | L[i]:从左边一直到当前位置的最大值 、R[i]:
32 | 改进:L[i] --> 一个变量(即可保存遍历到当前位置时的最大值)
33 |
34 | 神级方法:
35 | 两个指针 L、R
36 | 谁指的数字更小,结算当前水量,往中间移动一位,
37 | 相遇即停止
38 |
39 |
40 | ## 3.给定一个非负数的数组,数组中的每个值代表一个柱子的高度,柱子的宽度是1。两个柱
41 | 子之间可以围成一个面积,规定:面积=两根柱子的最小值*两根柱子之间的距离。比如数
42 | 组[3,4,2,5]。3 和4 之间围成的面积为0,因为两个柱子是相邻的,中间没有距离。3 和
43 | 2 之间围成的面积为2,因为两个柱子的距离为1,且2 是最短的柱子,所以面积=1*2。
44 | 3 和5 之间围成的面积为6,因为两个柱子的距离为2,且3 是最短的柱子,所以面积=
45 | 3*2。求在一个数组中,哪两个柱子围成的面积最大,并返回值。
46 | 要求:实现时间复杂度O(N),额外空间复杂度O(1)的解法
47 |
48 |
--------------------------------------------------------------------------------
/src/nowcoder/b_2nd_Season/bh160907/BiggestSubBSTInTree.java:
--------------------------------------------------------------------------------
1 | package nowcoder.b_2nd_Season.bh160907;
2 |
3 | import Standard.BinaryTreeNode;
4 | import others.PrintBinaryTree;
5 |
6 | /**
7 | * 找到二叉树中的最大搜索二叉子树
8 | *
9 | * Created by nibnait on 2016/9/16.
10 | */
11 | public class BiggestSubBSTInTree {
12 |
13 | private static BinaryTreeNode biggestSubBST(BinaryTreeNode head) {
14 |
15 | return null;
16 | }
17 |
18 |
19 |
20 |
21 | public static void main(String[] args) {
22 |
23 | BinaryTreeNode head = new BinaryTreeNode(6);
24 | head.left = new BinaryTreeNode(1);
25 | head.left.left = new BinaryTreeNode(0);
26 | head.left.right = new BinaryTreeNode(3);
27 | head.right = new BinaryTreeNode(12);
28 | head.right.left = new BinaryTreeNode(10);
29 | head.right.left.left = new BinaryTreeNode(4);
30 | head.right.left.left.left = new BinaryTreeNode(2);
31 | head.right.left.left.right = new BinaryTreeNode(5);
32 | head.right.left.right = new BinaryTreeNode(14);
33 | head.right.left.right.left = new BinaryTreeNode(11);
34 | head.right.left.right.right = new BinaryTreeNode(15);
35 | head.right.right = new BinaryTreeNode(13);
36 | head.right.right.left = new BinaryTreeNode(20);
37 | head.right.right.right = new BinaryTreeNode(16);
38 |
39 | PrintBinaryTree.print(head);
40 | BinaryTreeNode bst = biggestSubBST(head);
41 | PrintBinaryTree.print(bst);
42 |
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/out/production/algorithms/nowcoder/b_2nd_Season/ba160720/README.md:
--------------------------------------------------------------------------------
1 | 课程回顾:[http://www.nowcoder.com/live/11/1/1](http://www.nowcoder.com/live/11/1/1)
2 | 课件下载:[https://pan.baidu.com/s/1hs8WGh2](https://pan.baidu.com/s/1hs8WGh2)
3 |
4 | ## 1. 给定一个N*2 的二维数组,看作是一个个二元组,例如[[a1,b1],[a2,b2],[a3,b3]],
5 | 规定:一个如果想把二元组甲放在二元组乙上,甲中的a 值必须大于乙中的a 值,甲中的b
6 | 值必须大于乙中的b 值。如果在二维数组中随意选择二元组,请问二元组最多可以往上摞
7 | 几个?
8 | 例如:[[5,4],[6,4],[6,7],[2,3]], 最大数量可以摞3 个,[2,3] => [5,4] => [6,7]
9 | 要求:实现时间复杂度O(N*logN)的解法
10 |
11 | 最长递增子序列:
12 | h(i):当必须以arr的第i号元素结尾的情况下 最长递增子序列的长度的值。
13 |
14 | (2)h(i):假设我当前遍历到cur这个数时,有效区中,h(i)遍历到 当前时刻为止,长度为i+1的最长递增子序列(子数组)的最小**末尾**
15 |
16 | 排序策略:
17 | - 二元组中两个元素是同等重要的!
18 | - 最优解:先 a 小-->大,(a = a', 再 b 大-->小 排序)。
19 |
20 |
21 |
22 | ## 2.给定一个非负数的数组,代表一个容器。例如数组[0,1,0,2,1,0,1,3,2,1,2,1],就是
23 | 以下图形中黑色的部分。如果用这个容器接水的话,请问可以接多少水?还以这个数组为例,
24 | 可以接6 格水,就是以下图形中蓝色的部分。
25 | 要求:实现时间复杂度O(N),额外空间复杂度O(1)的解法
26 |
27 | - 判断有效的波峰波谷!
28 | water[i] = 取两侧的最大值,即可求出此位置的最大水量
29 | 加速求两边的最大值:
30 | 两个辅助数组,
31 | L[i]:从左边一直到当前位置的最大值 、R[i]:
32 | 改进:L[i] --> 一个变量(即可保存遍历到当前位置时的最大值)
33 |
34 | 神级方法:
35 | 两个指针 L、R
36 | 谁指的数字更小,结算当前水量,往中间移动一位,
37 | 相遇即停止
38 |
39 |
40 | ## 3.给定一个非负数的数组,数组中的每个值代表一个柱子的高度,柱子的宽度是1。两个柱
41 | 子之间可以围成一个面积,规定:面积=两根柱子的最小值*两根柱子之间的距离。比如数
42 | 组[3,4,2,5]。3 和4 之间围成的面积为0,因为两个柱子是相邻的,中间没有距离。3 和
43 | 2 之间围成的面积为2,因为两个柱子的距离为1,且2 是最短的柱子,所以面积=1*2。
44 | 3 和5 之间围成的面积为6,因为两个柱子的距离为2,且3 是最短的柱子,所以面积=
45 | 3*2。求在一个数组中,哪两个柱子围成的面积最大,并返回值。
46 | 要求:实现时间复杂度O(N),额外空间复杂度O(1)的解法
47 |
48 |
--------------------------------------------------------------------------------
/src/xiaozhao/SoHu_03.java:
--------------------------------------------------------------------------------
1 | package xiaozhao;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | *
7 | 袋鼠过河
8 | 时间限制:C/C++语言 1000MS;其他语言 3000MS
9 | 内存限制:C/C++语言 131072KB;其他语言 655360KB
10 | 题目描述:
11 | 一只袋鼠要从河这边跳到河对岸,河很宽,但是河中间打了很多桩子,每隔一米就有一个,每个桩子上都有一个弹簧,袋鼠跳到弹簧上就可以跳的更远。每个弹簧力量不同,用一个数字代表它的力量,如果弹簧力量为5,就代表袋鼠下一跳最多能够跳5米,如果为0,就会陷进去无法继续跳跃。河流一共N米宽,袋鼠初始位置就在第一个弹簧上面,要跳到最后一个弹簧之后就算过河了,给定每个弹簧的力量,求袋鼠最少需要多少跳能够到达对岸。如果无法到达输出-1
12 | 输入
13 | 输入分两行,第一行是数组长度N,第二行是每一项的值,用空格分隔
14 | 输出
15 | 输出最少的跳数,无法到达输出-1
16 |
17 | 样例输入
18 | 5
19 | 2 0 1 1 1
20 | 样例输出
21 | 4
22 | * Created by nibnait on 2016/9/21.
23 | */
24 | public class SoHu_03 {
25 | public static void main(String[] args) {
26 | Scanner sc = new Scanner(System.in);
27 | while (sc.hasNext()) {
28 | int n = sc.nextInt();
29 | int arr[] = new int[n + 1];
30 | for (int i = 1; i <= n; i++) {
31 | arr[i] = sc.nextInt();
32 | }
33 | int times = 0;
34 | int s = 0;
35 | for (int i = 1; i < arr.length; i++) {
36 | if (s >= n) break;
37 | if (arr[i] != 0) {
38 | times++;
39 | s += arr[i];
40 | continue;
41 | }
42 | if (arr[i] == 0 && arr[1 + s] > 0) {
43 | s += arr[i];
44 | } else {
45 | times = -1;
46 | break;
47 | }
48 | }
49 | System.out.println(times);
50 | }
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/nowcoder/b_2nd_Season/bd160810/src/TwoStacksImplementQueue.java:
--------------------------------------------------------------------------------
1 | package nowcoder.b_2nd_Season.bd160810.src;
2 |
3 | import java.util.Stack;
4 |
5 | public class TwoStacksImplementQueue {
6 |
7 | private static class TwoStacksQueue {
8 | public Stack stackPush;
9 | public Stack stackPop;
10 |
11 | public TwoStacksQueue() {
12 | stackPush = new Stack();
13 | stackPop = new Stack();
14 | }
15 |
16 | public void add(int pushInt) {
17 | stackPush.push(pushInt);
18 | }
19 |
20 | public int poll() {
21 | if (stackPop.empty() && stackPush.empty()) {
22 | throw new RuntimeException("Queue is empty!");
23 | } else if (stackPop.empty()) {
24 | while (!stackPush.empty()) {
25 | stackPop.push(stackPush.pop());
26 | }
27 | }
28 | return stackPop.pop();
29 | }
30 |
31 | public int peek() {
32 | if (stackPop.empty() && stackPush.empty()) {
33 | throw new RuntimeException("Queue is empty!");
34 | } else if (stackPop.empty()) {
35 | while (!stackPush.empty()) {
36 | stackPop.push(stackPush.pop());
37 | }
38 | }
39 | return stackPop.peek();
40 | }
41 | }
42 |
43 | public static void main(String[] args) {
44 | TwoStacksQueue test = new TwoStacksQueue();
45 | test.add(1);
46 | test.add(2);
47 | test.add(3);
48 | System.out.println(test.peek());
49 | System.out.println(test.poll());
50 | System.out.println(test.peek());
51 | System.out.println(test.poll());
52 | System.out.println(test.peek());
53 | System.out.println(test.poll());
54 | }
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/src/Algorithms_4thEdition/a_Sorting/g_计数排序.java:
--------------------------------------------------------------------------------
1 | package Algorithms_4thEdition.a_Sorting;
2 |
3 | /**
4 | * 一些时间复杂度趋近于O(N)的排序算法
5 | * (不是基于比较的排序算法)
6 | * 思想来源于桶排序
7 | */
8 |
9 | /**
10 | * Counting_Sort
11 | * 时间复杂度:
12 | * 【题目】
13 | 对于一个int数组,请编写一个计数排序算法,对数组元素排序。
14 | 给定一个int数组A及数组的大小n,请返回排序后的数组。
15 | 测试样例:
16 | [1,2,3,5,2,3],6
17 | [1,2,2,3,3,5]
18 | *
19 | * 【思路】
20 | * 桶排序
21 | *
22 | * Created by nibnait on 2016/9/23.
23 | */
24 |
25 | public class g_计数排序 {
26 | public static void main(String[] args) {
27 | int[] A = new int[]{54,35,48,36,27,12,44,44,8,14,26,17,28};
28 |
29 | A = Counting_Sort(A, 13);
30 |
31 | for (int i = 0; i < A.length; i++) {
32 | System.out.print(A[i]+" ");
33 | }
34 | }
35 |
36 | public static int[] Counting_Sort(int[] A, int n) {
37 |
38 | int max = A[0];
39 | int min = A[0];
40 | for (int i = 0; i < A.length; i++) {
41 | if (A[i]>max) {
42 | max = A[i];
43 | }
44 | if (A[i]0){
58 | A[cnt++] = i+min;
59 | bucket[i]--;
60 | }
61 | }
62 |
63 | return A;
64 | }
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/src/nowcoder/b_2nd_Season/ba160720/arrUp.java:
--------------------------------------------------------------------------------
1 | package nowcoder.b_2nd_Season.ba160720;
2 |
3 | /**
4 | * 求最长递增子序列的长度
5 | * 自然想法:
6 | * 求出以数组中每一位置结尾的最长递增子序列,
7 | * 即:利用一个辅助数组h[], h(i):表示当必须以arr的第i号元素结尾的情况下 最长递增子序列的长度的值。
8 | * 但是这样,你每求一个位置的h(i)都要先遍历一遍arr中他前面的值,都要找到 【比cur(当前值)小的arr[i] 对应的h[i]中最大那个长度 】 然后再加1.,简称:【枚举过程】
9 | * 时间复杂度:O(N^2)
10 | *
11 | * 得分方法:
12 | * 时间复杂度:O(N*log(N) )
13 | * 加速枚举过程,
14 | * 重新定义辅助数组h[],h(i):代表当前遍历到cur这个数时,有效区中,h(i)遍历到 当前时刻为止,长度为i+1的最长递增子序列的**最小末尾**
15 | * 这样【因为h[]是有序的】,所以当遍历到cur的时候,cur应该放的位置 可以用二分查找 直接找到!
16 | *
17 | * Created by nibnait on 2016/9/9.
18 | */
19 | public class arrUp {
20 |
21 | public static void main(String[] args) {
22 | int[] arr = new int[]{2, 1, 6, 4, 5, 2, 7, 4};
23 | System.out.println(arrUp(arr));
24 | }
25 |
26 | public static int arrUp(int[] arr) {
27 |
28 | int[] h = new int[arr.length];
29 | int j = 0; //有效区的范围
30 | h[j] = arr[0]; //h[0]表示 当遍历到arr[0]的时候,长度为1的最长递增子序列的最小末尾:就是arr[0]他自己么
31 | for (int i = 1; i < arr.length; i++) {
32 | int lo = 0;
33 | int hi = j;
34 | while (lo <= hi) { //因为h[]是有序的,所以可以利用二分查找,时间复杂度:O(logN)
35 | int mid = (lo + hi) / 2;
36 | if (h[mid] < arr[i]) {
37 | lo = mid + 1;
38 | } else {
39 | hi = mid - 1;
40 | }
41 | }
42 | h[lo] = arr[i]; //将arr[i]放到他应该放的位置上
43 |
44 | j = lo>j? lo: j;
45 | }
46 | return j+1;
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------