Skip to content

Commit 827002b

Browse files
committed
feat: add 100 and fix code
1 parent 8f6f65d commit 827002b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+417
-300
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
|70|[Climbing Stairs][070]|Dynamic Programming|
2727
|83|[Remove Duplicates from Sorted List][083]|Linked List|
2828
|88|[Merge Sorted Array][088]|Array, Two Pointers|
29+
|100|[Same Tree][100]|Tree, Depth-first Search|
2930

3031

3132
## Medium
@@ -69,6 +70,7 @@
6970
[070]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/070/README.md
7071
[083]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/083/README.md
7172
[088]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/088/README.md
73+
[100]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/100/README.md
7274

7375
[008]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/008/README.md
7476
[019]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/019/README.md

note/001/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ return [0, 1].
2323
题意是让你从给定的数组中找到两个元素的和为指定值的两个索引,最容易的当然是循环两次,复杂度为`O(n^2)`,首次提交居然是2ms,打败了100%的提交,谜一样的结果,之后再次提交就再也没跑到过2ms了。
2424

2525
``` java
26-
public class Solution {
26+
class Solution {
2727
public int[] twoSum(int[] nums, int target) {
2828
int st = 0, end = nums.length;
2929
for (int i = 0; i < end; ++i) {

note/007/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The input is assumed to be a 32-bit signed integer. Your function should **retur
3232
题意是给你一个整型数,求它的逆序整型数,而且有个小坑点,当它的逆序整型数溢出的话,那么就返回0,用我们代码表示的话可以求得结果保存在long中,最后把结果和整型的两个范围比较即可。
3333

3434
``` java
35-
public class Solution {
35+
class Solution {
3636
public int reverse(int x) {
3737
long res = 0;
3838
for (; x != 0; x /= 10)

note/008/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ If no valid conversion could be performed, a zero value is returned. If the corr
2828
题意是把一个字符串转为整型,但要注意所给的要求,先去除最前面的空格,然后判断正负数,注意正数可能包含`+`,如果之后存在非数字或全为空则返回`0`,而如果合法的值超过int表示的最大范围,则根据正负号返回`INT_MAX``INT_MIN`
2929

3030
``` java
31-
public class Solution {
31+
class Solution {
3232
public int myAtoi(String str) {
3333
int i = 0, ans = 0, sign = 1, len = str.length();
3434
while (i < len && str.charAt(i) == ' ') ++i;

note/009/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ There is a more generic way of solving this problem.
2424
题意是判断一个有符号整型数是否是回文,也就是逆序过来的整数和原整数相同,首先负数肯定不是,接下来我们分析一下最普通的解法,就是直接算出他的回文数,然后和给定值比较即可。
2525

2626
``` java
27-
public class Solution {
27+
class Solution {
2828
public boolean isPalindrome(int x) {
2929
if (x < 0) return false;
3030
int copyX = x, reverse = 0;

note/013/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Input is guaranteed to be within the range from 1 to 3999.
2020
那么我们可以利用map来完成罗马数字的7个数字符号:I、V、X、L、C、D、M和整数的映射关系,然后根据上面的解释来模拟完成即可。
2121

2222
``` java
23-
public class Solution {
23+
class Solution {
2424
public int romanToInt(String s) {
2525
Map<Character, Integer> map = new HashMap<>();
2626
map.put('I', 1);

note/014/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Write a function to find the longest common prefix string amongst an array of st
1212
题意是让你从字符串数组中找出公共前缀,我的想法是找出最短的那个字符串的长度`minLen`,然后在`0...minLen`的范围比较所有字符串,如果比较到有不同的字符,那么直接返回当前索引长度的字符串即可,否则最后返回最短的字符串即可。
1313

1414
``` java
15-
public class Solution {
15+
class Solution {
1616
public String longestCommonPrefix(String[] strs) {
1717
int len = strs.length;
1818
if (len == 0) return "";

note/019/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Try to do this in one pass.
3434
* ListNode(int x) { val = x; }
3535
* }
3636
*/
37-
public class Solution {
37+
class Solution {
3838
public ListNode removeNthFromEnd(ListNode head, int n) {
3939
ListNode pre = head;
4040
ListNode afterPreN = head;

note/020/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The brackets must close in the correct order, `"()"` and `"()[]{}"` are all vali
1414
题意是判断括号匹配是否正确,很明显,我们可以用栈来解决这个问题,当出现左括号的时候入栈,当遇到右括号时,判断栈顶的左括号是否何其匹配,不匹配的话直接返回`false`即可,最终判断是否空栈即可,这里我们可以用数组模拟栈的操作使其操作更快,有个细节注意下`top = 1;`,从而省去了之后判空的操作和`top - 1`导致数组越界的错误。
1515

1616
``` java
17-
public class Solution {
17+
class Solution {
1818
public boolean isValid(String s) {
1919
int len = s.length();
2020
char[] stack = new char[len + 1];

note/021/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Your function should return length = `2`, with the first two elements of *nums*
2727
* ListNode(int x) { val = x; }
2828
* }
2929
*/
30-
public class Solution {
30+
class Solution {
3131
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
3232
ListNode head = new ListNode(0);
3333
ListNode temp = head;

note/026/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Your function should return length = `2`, with the first two elements of *nums*
1919
题意是让你从一个有序的数组中移除重复的元素,并返回之后数组的长度。我的思路是判断长度小于等于1的话直接返回原长度即可,否则的话遍历一遍数组,用一个`tail`变量指向尾部,如果后面的元素和前面的元素不同,就让`tail`变量加一,最后返回`tail`即可。
2020

2121
``` java
22-
public class Solution {
22+
class Solution {
2323
public int removeDuplicates(int[] nums) {
2424
int len = nums.length;
2525
if (len <= 1) return len;

note/027/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Your function should return length = 2, with the first two elements of *nums* be
2222
题意是移除数组中值等于`val`的元素,并返回之后数组的长度,并且题目中指定空间复杂度为O(1),我的思路是用`tail`标记尾部,遍历该数组时当索引元素不等于`val`时,`tail`加一,尾部指向当前元素,最后返回`tail`即可。
2323

2424
``` java
25-
public class Solution {
25+
class Solution {
2626
public int removeElement(int[] nums, int val) {
2727
int tail = 0;
2828
for (int i = 0, len = nums.length; i < len; ++i) {

note/028/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Returns the index of the first occurrence of needle in haystack, or -1 if needle
1414
题意是从主串中找到子串的索引,如果找不到则返回-1,我们只需要遍历主串长度减子串长度即可,利用substring比较即可。
1515

1616
``` java
17-
public class Solution {
17+
class Solution {
1818
public int strStr(String haystack, String needle) {
1919
int l1 = haystack.length(), l2 = needle.length(), l3 = l1 - l2;
2020
for (int i = 0; i <= l3; ++i) {

note/035/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Here are few examples.
2121
题意是让你从一个没有重复元素的已排序数组中找到插入位置的索引。因为数组已排序,所以我们可以想到二分查找法,因为查找到的条件是找到第一个等于或者大于`target`的元素的位置,所以二分法略作变动即可。
2222

2323
``` java
24-
public class Solution {
24+
class Solution {
2525
public int searchInsert(int[] nums, int target) {
2626
int left = 0, right = nums.length - 1, mid = (right + left) >> 1;
2727
while (left <= right) {

note/038/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Note: The sequence of integers will be represented as a string.
2424
题意是数和说,根据如下序列`1, 11, 21, 1211, 111221, ...`,求第n个数,规则很简单,就是数和说,数就是数这个数数字有几个,说就是说这个数,所以`1`就是1个1:`11`,`11`就是有2个1:`21``21`就是1个2、1个1:`1211`,可想而知后面就是`111221`,思路的话就是按这个逻辑模拟出来即可。
2525

2626
``` java
27-
public class Solution {
27+
class Solution {
2828
public String countAndSay(int n) {
2929
String str = "1";
3030
while (--n > 0) {

note/053/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ If you have figured out the O(*n*) solution, try coding another solution using t
1919
题意是求数组中子数组的最大和,这种最优问题一般第一时间想到的就是动态规划,我们可以这样想,当部分序列和大于零的话就一直加下一个元素即可,并和当前最大值进行比较,如果出现部分序列小于零的情况,那肯定就是从当前元素算起。其转移方程就是`dp[i] = nums[i] + (dp[i - 1] > 0 ? dp[i - 1] : 0);`,由于我们不需要保留dp状态,故可以优化空间复杂度为1,即`dp = nums[i] + (dp > 0 ? dp : 0);`
2020

2121
``` java
22-
public class Solution {
22+
class Solution {
2323
public int maxSubArray(int[] nums) {
2424
int len = nums.length, dp = nums[0], max = dp;
2525
for (int i = 1; i < len; ++i) {
@@ -36,7 +36,7 @@ public class Solution {
3636
题目也给了我们另一种思路,就是分治,所谓分治就是把问题分割成更小的,最后再合并即可,我们把`nums`一分为二先,那么就有两种情况,一种最大序列包括中间的值,一种就是不包括,也就是在左边或者右边;当最大序列在中间的时候那我们就把它两侧的最大和算出即可;当在两侧的话就继续分治即可。
3737

3838
``` java
39-
public class Solution {
39+
class Solution {
4040
public int maxSubArray(int[] nums) {
4141
return helper(nums, 0, nums.length - 1);
4242
}

note/058/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ return `5`.
2222
题意是让你从一个只包含大小字母和空格字符的字符串中得到最后一个单词的长度,很简单,我们倒序遍历,先得到最后一个非空格字符的索引,然后再得到它前面的空格字符索引,两者相减即可。当然,我们使用API来完成这件事更加方便,只需一行代码`return s.trim().length() - s.trim().lastIndexOf(" ") - 1;`,但我相信作者出这道题的目的肯定不是考你API的使用,所以我们还是用自己的思路来实现。
2323

2424
``` java
25-
public class Solution {
25+
class Solution {
2626
public int lengthOfLastWord(String s) {
2727
int p = s.length() - 1;
2828
while (p >= 0 && s.charAt(p) == ' ') p--;

note/066/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The digits are stored such that the most significant digit is at the head of the
1616
题意是给你一个数字数组,高位在前,并且首位不为0除非这个数组就是`[0]`,让你给该数组低位加一求其结果,那么我们就模拟小学数学那样进位去算即可,如果一直进位到首位,这种情况也就是都是由9组成的数组,此时我们只要new出一个多一个长度的数组即可,并把第0个元素赋1即可。
1717

1818
``` java
19-
public class Solution {
19+
class Solution {
2020
public int[] plusOne(int[] digits) {
2121
int p = digits.length - 1;
2222
if (digits[p] < 9) {

note/067/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Return `"100"`.
1717
题意是给你两个二进制串,求其和的二进制串。我们就按照小学算数那么来做,用`carry`表示进位,从后往前算,依次往前,每算出一位就插入到最前面即可,直到把两个二进制串都遍历完即可。
1818

1919
``` java
20-
public class Solution {
20+
class Solution {
2121
public String addBinary(String a, String b) {
2222
StringBuilder sb = new StringBuilder();
2323
int carry = 0, p1 = a.length() - 1, p2 = b.length() - 1;

note/069/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Compute and return the square root of x.
1414
题意是求平方根,参考[牛顿迭代法求平方根](https://wenku.baidu.com/view/6b74c622bcd126fff7050bfe.html),然后再参考维基百科的[Integer square root](https://en.wikipedia.org/wiki/Integer_square_root#Using_only_integer_division)即可。
1515

1616
``` java
17-
public class Solution {
17+
class Solution {
1818
public int mySqrt(int x) {
1919
long n = x;
2020
while (n * n > x) {

note/070/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Each time you can either climb 1 or 2 steps. In how many distinct ways can you c
1616
题意是爬楼梯,每次你只能爬一步或者两步,问到顶层共有多少种方案。我们假设到顶层共有`f(n)`种,那么`f(n) = f(n - 1) + f(n - 2)`肯定是成立的,意思就是我们迈向顶层的最后一步是在倒数第一级台阶或者在倒数第二级台阶。算法我对空间复杂度进行了优化,因为在迭代过程中只需要两个变量即可。
1717

1818
``` java
19-
public class Solution {
19+
class Solution {
2020
public int climbStairs(int n) {
2121
int a = 1, b = 1;
2222
while (--n > 0) {

note/083/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Given `1->1->2->3->3`, return `1->2->3`.
2424
* ListNode(int x) { val = x; }
2525
* }
2626
*/
27-
public class Solution {
27+
class Solution {
2828
public ListNode deleteDuplicates(ListNode head) {
2929
if (head == null || head.next == null) return head;
3030
ListNode curr = head;

note/088/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ You may assume that *nums1* has enough space (size that is greater or equal to *
1515
题意是给两个已排序的数组`nums1``nums2`,合并`nums2``nums1`中,两数组元素个数分别为`m``n`,而且`nums1`数组的长度足够容纳`m + n`个元素,如果我们按顺序排下去,那肯定要开辟一个新数组来保存元素,如果我们选择逆序,这样利用`nums1`自身空间足矣,不会出现覆盖的情况,依次把大的元素插入到`nums1`的末尾,确保`nums2`中的元素全部插入到`nums1`即可。
1616

1717
``` java
18-
public class Solution {
18+
class Solution {
1919
public void merge(int[] nums1, int m, int[] nums2, int n) {
2020
int p = m-- + n-- - 1;
2121
while (m >= 0 && n >= 0)

note/100/README.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
1-
# [Merge Sorted Array][title]
1+
# [Same Tree][title]
22

33
## Description
44

5-
Given two sorted integer arrays *nums1* and *nums2*, merge *nums2* into *nums1* as one sorted array.
5+
Given two binary trees, write a function to check if they are equal or not.
66

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.
7+
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
98

10-
**Tags:** Array, Two Pointers
9+
**Tags:** Tree, Depth-first Search
1110

1211

1312
## 思路
1413

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

1716
``` 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--];
17+
/**
18+
* Definition for a binary tree node.
19+
* public class TreeNode {
20+
* int val;
21+
* TreeNode left;
22+
* TreeNode right;
23+
* TreeNode(int x) { val = x; }
24+
* }
25+
*/
26+
class Solution {
27+
public boolean isSameTree(TreeNode p, TreeNode q) {
28+
if (p == null && q == null) return true;
29+
if (p == null || q == null) return false;
30+
if (p.val == q.val) {
31+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
32+
}
33+
return false;
2534
}
2635
}
2736
```
@@ -33,5 +42,5 @@ public class Solution {
3342

3443

3544

36-
[title]: https://leetcode.com/problems/merge-sorted-array
45+
[title]: https://leetcode.com/problems/same-tree
3746
[ajl]: https://github.com/Blankj/awesome-java-leetcode

0 commit comments

Comments
 (0)