Skip to content

Commit c1dcfa1

Browse files
committed
feat: add 100
1 parent 36ba506 commit c1dcfa1

File tree

15 files changed

+406
-64
lines changed

15 files changed

+406
-64
lines changed

note/088/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ You may assume that *nums1* has enough space (size that is greater or equal to *
1212

1313
## 思路
1414

15-
题意是给两个已排序的数组`nums1``nums2`,合并`nums2``nums1`中,两数组元素个数分别为`m``n`,而且`nums1`数组的长度足够容纳`m + n`个元素,如果我们按顺序排下去,那肯定要开辟一个新数组来保存覆盖的元素,所以我们选择逆序,这样利用`nums1`自身空间足矣,依次把大的元素插入到`nums1`的末尾,确保`nums2`中的元素全部插入到`nums1`即可。
15+
题意是给两个已排序的数组`nums1``nums2`,合并`nums2``nums1`中,两数组元素个数分别为`m``n`,而且`nums1`数组的长度足够容纳`m + n`个元素,如果我们按顺序排下去,那肯定要开辟一个新数组来保存元素,如果我们选择逆序,这样利用`nums1`自身空间足矣,不会出现覆盖的情况,依次把大的元素插入到`nums1`的末尾,确保`nums2`中的元素全部插入到`nums1`即可。
1616

1717
``` java
1818
public class Solution {

note/100/README.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# [Merge Sorted Array][title]
2+
3+
## Description
4+
5+
Given two sorted integer arrays *nums1* and *nums2*, merge *nums2* into *nums1* as one sorted array.
6+
7+
**Note:**
8+
You may assume that *nums1* has enough space (size that is greater or equal to *m* + *n*) to hold additional elements from *nums2*. The number of elements initialized in *nums1* and *nums2* are *m* and *n* respectively.
9+
10+
**Tags:** Array, Two Pointers
11+
12+
13+
## 思路
14+
15+
题意是给两个已排序的数组`nums1``nums2`,合并`nums2``nums1`中,两数组元素个数分别为`m``n`,而且`nums1`数组的长度足够容纳`m + n`个元素,如果我们按顺序排下去,那肯定要开辟一个新数组来保存元素,如果我们选择逆序,这样利用`nums1`自身空间足矣,不会出现覆盖的情况,依次把大的元素插入到`nums1`的末尾,确保`nums2`中的元素全部插入到`nums1`即可。
16+
17+
``` java
18+
public class Solution {
19+
public void merge(int[] nums1, int m, int[] nums2, int n) {
20+
int p = m-- + n-- - 1;
21+
while (m >= 0 && n >= 0)
22+
nums1[p--] = nums1[m] > nums2[n] ? nums1[m--] : nums2[n--];
23+
while (n >= 0)
24+
nums1[p--] = nums2[n--];
25+
}
26+
}
27+
```
28+
29+
30+
## 结语
31+
32+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]
33+
34+
35+
36+
[title]: https://leetcode.com/problems/merge-sorted-array
37+
[ajl]: https://github.com/Blankj/awesome-java-leetcode

project/leetcode/.idea/workspace.xml

+219-38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

project/leetcode/src/com/blankj/easy/_021/Solution.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
3131

3232
public static void main(String[] args) {
3333
Solution solution = new Solution();
34-
;
3534
ListNode listNode00 = new ListNode(1);
3635
ListNode listNode01 = new ListNode(3);
3736
ListNode listNode02 = new ListNode(5);
@@ -52,10 +51,10 @@ public static void main(String[] args) {
5251
listNode12.next = listNode13;
5352
listNode13.next = listNode14;
5453
listNode14.next = null;
55-
ListNode listNode0 = ListNode.createTestData(new int[]{1, 3, 5, 7, 9});
56-
ListNode listNode1 = ListNode.createTestData(new int[]{2, 4, 6, 8, 10});
57-
System.out.println(listNode0.toString());
58-
System.out.println(listNode1.toString());
59-
System.out.println(solution.mergeTwoLists(listNode0, listNode1).toString());
54+
ListNode listNode0 = ListNode.createTestData("[1,3,5,7,9]");
55+
ListNode listNode1 = ListNode.createTestData("[2,4,6,8,10]");
56+
ListNode.print(listNode0);
57+
ListNode.print(listNode1);
58+
ListNode.print(solution.mergeTwoLists(listNode0, listNode1));
6059
}
6160
}

project/leetcode/src/com/blankj/easy/_067/Solution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public String addBinary(String a, String b) {
4444
// sb.insert(0, (char) (carry % 2 + '0'));
4545
// carry >>= 1;
4646
// }
47-
// return sb.toString();
47+
// return sb.print();
4848
// }
4949

5050
public static void main(String[] args) {

project/leetcode/src/com/blankj/easy/_083/Solution.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public ListNode deleteDuplicates(ListNode head) {
2727

2828
public static void main(String[] args) {
2929
Solution solution = new Solution();
30-
System.out.println(solution.deleteDuplicates(ListNode.createTestData(new int[]{1, 1, 2})));
31-
System.out.println(solution.deleteDuplicates(ListNode.createTestData(new int[]{1, 1, 2, 3, 3})));
30+
ListNode.print(solution.deleteDuplicates(ListNode.createTestData("[1,1,2]")));
31+
ListNode.print(solution.deleteDuplicates(ListNode.createTestData("[1,1,2,3,3]")));
3232
}
3333
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.blankj.easy._100;
2+
3+
4+
import com.blankj.structure.TreeNode;
5+
6+
/**
7+
* <pre>
8+
* author: Blankj
9+
* blog : http://blankj.com
10+
* time : 2017/06/05
11+
* desc :
12+
* </pre>
13+
*/
14+
15+
public class Solution {
16+
public boolean isSameTree(TreeNode p, TreeNode q) {
17+
return false;
18+
}
19+
20+
public static void main(String[] args) {
21+
Solution solution = new Solution();
22+
TreeNode.print(TreeNode.createTestData("[1,2,2,null,3,null,3]"));
23+
System.out.println();
24+
}
25+
}

project/leetcode/src/com/blankj/structure/ListNode.java

+24-16
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,34 @@ public ListNode(int x) {
1717
val = x;
1818
}
1919

20-
@Override
21-
public String toString() {
22-
String str = "[" + String.valueOf(val);
23-
ListNode p = next;
24-
while (p != null) {
25-
str += ", " + String.valueOf(p.val);
26-
p = p.next;
27-
}
28-
return str + "]";
29-
}
30-
31-
public static ListNode createTestData(int[] data) {
32-
int len = len = data.length;
33-
if (len == 0) return null;
20+
/**
21+
* 创建测试数据
22+
*
23+
* @param data [XX,XX,XX]
24+
* @return {@link ListNode}
25+
*/
26+
public static ListNode createTestData(String data) {
27+
if (data.equals("[]")) return null;
28+
data = data.substring(1, data.length() - 1);
29+
String[] split = data.split(",");
30+
int len = split.length;
3431
ListNode[] listNode = new ListNode[len + 1];
35-
listNode[0] = new ListNode(data[0]);
32+
listNode[0] = new ListNode(Integer.valueOf(split[0]));
3633
for (int i = 1; i < len; i++) {
37-
listNode[i] = new ListNode(data[i]);
34+
listNode[i] = new ListNode(Integer.valueOf(split[i]));
3835
listNode[i - 1].next = listNode[i];
3936
}
4037
return listNode[0];
4138
}
39+
40+
public static void print(ListNode listNode) {
41+
if (listNode == null) System.out.println("null");
42+
StringBuilder str = new StringBuilder("[" + String.valueOf(listNode.val));
43+
ListNode p = listNode.next;
44+
while (p != null) {
45+
str.append(",").append(String.valueOf(p.val));
46+
p = p.next;
47+
}
48+
System.out.println(str.append("]"));
49+
}
4250
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.blankj.structure;
2+
3+
/**
4+
* <pre>
5+
* author: Blankj
6+
* blog : http://blankj.com
7+
* time : 2017/06/05
8+
* desc :
9+
* </pre>
10+
*/
11+
public class TreeNode {
12+
13+
public int val;
14+
public TreeNode left;
15+
public TreeNode right;
16+
17+
TreeNode(int x) {
18+
val = x;
19+
}
20+
21+
/**
22+
* 创建测试数据
23+
*
24+
* @param data [XX,XX,null,xx]
25+
* @return {@link TreeNode}
26+
*/
27+
public static TreeNode createTestData(String data) {
28+
if (data.equals("[]")) return null;
29+
data = data.substring(1, data.length() - 1);
30+
String[] split = data.split(",");
31+
int len = len = split.length;
32+
TreeNode[] treeNodes = new TreeNode[len];
33+
data = data.substring(1, data.length() - 1);
34+
for (int i = 0; i < len; i++) {
35+
if (!split[i].equals("null")) {
36+
treeNodes[i] = new TreeNode(Integer.valueOf(split[i]));
37+
}
38+
}
39+
for (int i = 0; i < len; i++) {
40+
if (treeNodes[i] != null) {
41+
int leftIndex = i * 2 + 1;
42+
if (leftIndex < len) {
43+
treeNodes[i].left = treeNodes[leftIndex];
44+
}
45+
int rightIndex = leftIndex + 1;
46+
if (rightIndex < len) {
47+
treeNodes[i].right = treeNodes[rightIndex];
48+
}
49+
}
50+
}
51+
return treeNodes[0];
52+
}
53+
54+
private static final String space = " ";
55+
56+
/**
57+
* 竖向打印二叉树
58+
*
59+
* @param root 二叉树根节点
60+
*/
61+
public static void print(TreeNode root) {
62+
print(root, 0);
63+
64+
}
65+
66+
private static void print(TreeNode node, int deep) {
67+
if (node == null) {
68+
printSpace(deep);
69+
System.out.println("#");
70+
return;
71+
}
72+
print(node.right, deep + 1);
73+
printSpace(deep);
74+
printNode(node.val, 5);
75+
print(node.left, deep + 1);
76+
}
77+
78+
private static void printSpace(int count) {
79+
for (int i = 0; i < count; i++) {
80+
System.out.printf(space);
81+
}
82+
}
83+
84+
private static void printNode(int val, int len) {
85+
StringBuilder res = new StringBuilder(val + "<");
86+
int spaceNum = len - res.length();
87+
for (int i = 0; i < spaceNum; i++) {
88+
res.append(" ");
89+
}
90+
System.out.println(res);
91+
}
92+
}

0 commit comments

Comments
 (0)