Skip to content

Commit a6e9ffc

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents cc67879 + 9f3885f commit a6e9ffc

File tree

7 files changed

+248
-73
lines changed

7 files changed

+248
-73
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@
8080
| [696][696-question] | [Count Binary Substrings][696-tips] | [][696-java] | [][696-js] | |
8181
| [697][697-question] | [Degree of an Array][697-tips] | [][697-java] | [][697-js] | |
8282
| [717][717-question] | [1-bit and 2-bit Characters][717-tips] | [][717-java] | [][717-js] | |
83-
| [720][720-question] | [Longest Word in Dictionary][720-tips] | | [][720-js] | |
84-
| [724][724-question] | [Find Pivot Index][724-tips] | | [][724-js] | |
83+
| [720][720-question] | [Longest Word in Dictionary][720-tips] | [][720-java] | [][720-js] | |
84+
| [724][724-question] | [Find Pivot Index][724-tips] | [][724-java] | [][724-js] | |
8585
| [728][728-question] | [Self Dividing Numbers][728-tips] | [][728-java] | [][728-js] | |
86-
| [733][733-question] | [Flood Fill][733-tips] | | [][733-js] | |
86+
| [733][733-question] | [Flood Fill][733-tips] | [][733-java] | [][733-js] | |
8787
| [744][744-question] | [Find Smallest Letter Greater Than Target][744-tips] | | [][744-js] | |
8888
| [746][746-question] | [Min Cost Climbing Stairs][746-tips] | | [][746-js] | |
8989
| [747][747-question] | [Largest Number At Least Twice of Others][747-tips] | | [][747-js] | |
@@ -478,7 +478,10 @@
478478
[696-java]: ./src/_696/Solution.java
479479
[697-java]: ./src/_697/Solution.java
480480
[717-java]: ./src/_717/Solution.java
481+
[720-java]: ./src/_720/Solution.java
482+
[724-java]: ./src/_724/Solution.java
481483
[728-java]: ./src/_728/Solution.java
484+
[733-java]: ./src/_733/Solution.java
482485
[771-java]: ./src/_771/Solution.java
483486
[804-java]: ./src/_804/Solution.java
484487

src/_720/Solution.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package _720;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
6+
class Solution {
7+
public String longestWord(String[] words) {
8+
String ans = "";
9+
HashSet<String> set = new HashSet<>(words.length);
10+
Arrays.sort(words);
11+
for (String s : words) {
12+
if (s.length() == 1 || set.contains(s.substring(0, s.length()-1))) {
13+
if (ans.length() < s.length()) {
14+
ans = s;
15+
}
16+
set.add(s);
17+
}
18+
}
19+
return ans;
20+
}
21+
}

src/_724/Solution.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package _724;
2+
3+
class Solution {
4+
public int pivotIndex(int[] nums) {
5+
if (nums == null || nums.length == 0) {
6+
return -1;
7+
}
8+
9+
int leftSum = 0;
10+
int rightSum = 0;
11+
12+
for (int i = nums.length - 1; i > 0; i--) {
13+
rightSum += nums[i];
14+
}
15+
16+
if (rightSum == 0) {
17+
return 0;
18+
}
19+
20+
for (int i = 1; i < nums.length; i++) {
21+
leftSum += nums[i - 1];
22+
rightSum -= nums[i];
23+
if (leftSum == rightSum) {
24+
return i;
25+
}
26+
}
27+
28+
return -1;
29+
}
30+
31+
public static void main(String[] args) {
32+
Solution solution = new Solution();
33+
System.out.println(solution.pivotIndex(new int[]{1, 7, 3, 6, 5, 6}));
34+
System.out.println(solution.pivotIndex(new int[]{1, 2, 3}));
35+
System.out.println(solution.pivotIndex(new int[]{0}));
36+
}
37+
}

src/_733/Solution.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package _733;
2+
3+
class Solution {
4+
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
5+
int oldColor = image[sr][sc];
6+
if (oldColor == newColor) {
7+
return image;
8+
}
9+
dfs(image, sr, sc, oldColor, newColor);
10+
return image;
11+
}
12+
13+
private void dfs(int[][] image, int sr, int sc, int oldColor, int newColor) {
14+
if (image[sr][sc] == oldColor) {
15+
image[sr][sc] = newColor;
16+
if (sr > 0) dfs(image, sr - 1, sc, oldColor, newColor);
17+
if (sr + 1 < image.length) dfs(image, sr + 1, sc, oldColor, newColor);
18+
if (sc > 0) dfs(image, sr, sc - 1, oldColor, newColor);
19+
if (sc + 1 < image[sr].length) dfs(image, sr, sc + 1, oldColor, newColor);
20+
}
21+
}
22+
23+
public static void main(String[] args) {
24+
Solution solution = new Solution();
25+
int[][] image = solution.floodFill(new int[][]{{1, 1, 1}, {1, 1, 0}, {1, 0, 1}}, 1, 1, 2);
26+
for (int[] row : image) {
27+
for (int num : row) {
28+
System.out.print(num + " ");
29+
}
30+
System.out.println();
31+
}
32+
}
33+
}

tips/720/README.md

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,65 @@
1-
[xxxx][title]
1+
[Longest Word in Dictionary ][title]
22

33
## Description
4-
// 抄题目
54

5+
Given a list of strings `words` representing an English Dictionary, find the longest word in `words` that can be built one character at a time by other words in `words`. If there is more than one possible answer, return the longest word with the smallest lexicographical order.
66

7-
**Example:**
7+
If there is no answer, return the empty string.
8+
9+
**Example 1:**
810

911
```
10-
// 抄Example
12+
Input:
13+
words = ["w","wo","wor","worl", "world"]
14+
Output: "world"
15+
Explanation:
16+
The word "world" can be built one character at a time by "w", "wo", "wor", and "worl".
1117
```
1218

13-
**Note:**
14-
// Note
19+
**Example 2:**
1520

16-
**Tags:** // tags
21+
```
22+
Input:
23+
words = ["a", "banana", "app", "appl", "ap", "apply", "apple"]
24+
Output: "apple"
25+
Explanation:
26+
Both "apply" and "apple" can be built from other words in the dictionary. However, "apple" is lexicographically smaller than "apply".
27+
```
1728

29+
**Note:**
1830

19-
## 思路 1
20-
// 贴一些关键代码,说一些解题思路
21-
// (同一种语言可以写多种思路,与某种语言思路相同的另一种语言的思路无须赘述,但可以把代码贴在后面)
22-
```java
31+
All the strings in the input will only contain lowercase letters.
2332

24-
```
25-
```javascript
33+
The length of `words` will be in the range `[1, 1000]`.
2634

27-
```
35+
The length of `words[i]` will be in the range `[1, 30]`.
2836

29-
## 思路 2
30-
// 贴一些关键代码,说一些解题思路
31-
```java
37+
**Tags:** [Hash Table](https://leetcode.com/tag/hash-table/)[Trie](https://leetcode.com/tag/trie/)
3238

33-
```
39+
## 思路 1
3440

35-
## 思路 3
36-
// 贴一些关键代码,说一些解题思路
37-
```kotlin
41+
我们可以将字符串按长度从小到大依次放入一个集合中,这样可以保证在放入字符串时,所有有可能是此字符串前缀的字符串都在此集合中。当要放入集合中时,我们判断原字符串去掉最后一个字符后的字符串是否存在于集合中,若存在才将此字符串放入集合,这样可以保证,集合中的字符串都可以在集合中找到自己的任意长度前缀。按上述要求将所有字符串放入集合,过程中不断更新较长的符合要求的字符串即可。
3842

43+
```java
44+
public String longestWord(String[] words) {
45+
String ans = "";
46+
HashSet<String> set = new HashSet<>(words.length);
47+
Arrays.sort(words);
48+
for (String s : words) {
49+
if (s.length() == 1 || set.contains(s.substring(0, s.length()-1))) {
50+
if (ans.length() < s.length()) {
51+
ans = s;
52+
}
53+
set.add(s);
54+
}
55+
}
56+
return ans;
57+
}
3958
```
4059

4160
## 结语
42-
61+
4362
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
4463

45-
[title]: https://leetcode.com/problems/xxxx
64+
[title]: https://leetcode.com/problems/longest-word-in-dictionary/description/
4665
[ls]: https://github.com/RichCodersAndMe/LeetCode-Solution

tips/724/README.md

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,83 @@
1-
[xxxx][title]
1+
[Find Pivot Index ][title]
22

33
## Description
4-
// 抄题目
54

5+
Given an array of integers `nums`, write a method that returns the "pivot" index of this array.
66

7-
**Example:**
7+
We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.
8+
9+
If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.
10+
11+
**Example 1:**
812

913
```
10-
// 抄Example
14+
Input:
15+
nums = [1, 7, 3, 6, 5, 6]
16+
Output: 3
17+
Explanation:
18+
The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3.
19+
Also, 3 is the first index where this occurs.
1120
```
1221

13-
**Note:**
14-
// Note
22+
**Example 2:**
1523

16-
**Tags:** // tags
24+
```
25+
Input:
26+
nums = [1, 2, 3]
27+
Output: -1
28+
Explanation:
29+
There is no index that satisfies the conditions in the problem statement.
30+
```
1731

32+
**Note:**
1833

19-
## 思路 1
20-
// 贴一些关键代码,说一些解题思路
21-
// (同一种语言可以写多种思路,与某种语言思路相同的另一种语言的思路无须赘述,但可以把代码贴在后面)
22-
```java
34+
The length of `nums` will be in the range `[0, 10000]`.
2335

24-
```
25-
```javascript
36+
Each element `nums[i]` will be an integer in the range `[-1000, 1000]`.
2637

27-
```
38+
**Tags:** [Array](https://leetcode.com/tag/array/)
2839

29-
## 思路 2
30-
// 贴一些关键代码,说一些解题思路
31-
```java
40+
## 思路
3241

33-
```
42+
在所给整型数组中寻找枢纽点(枢纽点左侧数字的和等于右侧数字的和),若有多个枢纽点,仅返回最左边的,若没有枢纽点则返回`-1`
43+
44+
我们通过一遍循环就可以获得所有数字的左右两侧的和,将其记录下来就可以轻松的得出枢纽点的位置。不过一次性记录下来需要额外的申请两个等大的数组。实际上我们只需要通过整个数组的和,就可以简单的推算出左右两边的和。假使我们从左往右遍历,每遍历到一个数都将其当做枢纽点,将枢纽左侧的数依次相加就等于左边的和,将数组的和依次减去枢纽的值就等于右边的和。
3445

35-
## 思路 3
36-
// 贴一些关键代码,说一些解题思路
37-
```kotlin
46+
**Java:**
3847

48+
```java
49+
public int pivotIndex(int[] nums) {
50+
if (nums == null || nums.length == 0) {
51+
return -1;
52+
}
53+
54+
int leftSum = 0;
55+
int rightSum = 0;
56+
57+
for (int i = nums.length - 1; i > 0; i--) {
58+
rightSum += nums[i];
59+
}
60+
61+
if (rightSum == 0) {
62+
return 0;
63+
}
64+
65+
for (int i = 1; i < nums.length; i++) {
66+
leftSum += nums[i - 1];
67+
rightSum -= nums[i];
68+
if (leftSum == rightSum) {
69+
return i;
70+
}
71+
}
72+
73+
return -1;
74+
}
3975
```
4076

4177
## 结语
42-
78+
4379
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
4480

45-
[title]: https://leetcode.com/problems/xxxx
46-
[ls]: https://github.com/RichCodersAndMe/LeetCode-Solution
81+
[title]: https://leetcode.com/problems/find-pivot-index/description/
82+
[ls]: https://github.com/RichCodersAndMe/LeetCode-Solution
83+

0 commit comments

Comments
 (0)