Skip to content

Commit d53abc3

Browse files
author
Shuo
authored
Merge pull request #716 from openset/develop
Add: new
2 parents 0e827a5 + 2b7c3b6 commit d53abc3

File tree

32 files changed

+739
-49
lines changed

32 files changed

+739
-49
lines changed

Diff for: README.md

+8
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ LeetCode Problems' Solutions
6262

6363
| # | Title | Solution | Difficulty |
6464
| :-: | - | - | :-: |
65+
| <span id="1263">1263</span> | [Minimum Moves to Move a Box to Their Target Location](https://leetcode.com/problems/minimum-moves-to-move-a-box-to-their-target-location "推箱子") | [Go](https://github.com/openset/leetcode/tree/master/problems/minimum-moves-to-move-a-box-to-their-target-location) | Hard |
66+
| <span id="1262">1262</span> | [Greatest Sum Divisible by Three](https://leetcode.com/problems/greatest-sum-divisible-by-three "可被三整除的最大和") | [Go](https://github.com/openset/leetcode/tree/master/problems/greatest-sum-divisible-by-three) | Medium |
67+
| <span id="1261">1261</span> | [Find Elements in a Contaminated Binary Tree](https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree "在受污染的二叉树中查找元素") | [Go](https://github.com/openset/leetcode/tree/master/problems/find-elements-in-a-contaminated-binary-tree) | Medium |
68+
| <span id="1260">1260</span> | [Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid "二维网格迁移") | [Go](https://github.com/openset/leetcode/tree/master/problems/shift-2d-grid) | Easy |
69+
| <span id="1259">1259</span> | [Handshakes That Don't Cross](https://leetcode.com/problems/handshakes-that-dont-cross "不相交的握手") | [Go](https://github.com/openset/leetcode/tree/master/problems/handshakes-that-dont-cross) | Hard |
70+
| <span id="1258">1258</span> | [Synonymous Sentences](https://leetcode.com/problems/synonymous-sentences "近义词句子") | [Go](https://github.com/openset/leetcode/tree/master/problems/synonymous-sentences) | Medium |
71+
| <span id="1257">1257</span> | [Smallest Common Region](https://leetcode.com/problems/smallest-common-region "最小公共区域") | [Go](https://github.com/openset/leetcode/tree/master/problems/smallest-common-region) | Medium |
72+
| <span id="1256">1256</span> | [Encode Number](https://leetcode.com/problems/encode-number "加密数字") | [Go](https://github.com/openset/leetcode/tree/master/problems/encode-number) | Medium |
6573
| <span id="1255">1255</span> | [Maximum Score Words Formed by Letters](https://leetcode.com/problems/maximum-score-words-formed-by-letters "得分最高的单词集合") | [Go](https://github.com/openset/leetcode/tree/master/problems/maximum-score-words-formed-by-letters) | Hard |
6674
| <span id="1254">1254</span> | [Number of Closed Islands](https://leetcode.com/problems/number-of-closed-islands "统计封闭岛屿的数目") | [Go](https://github.com/openset/leetcode/tree/master/problems/number-of-closed-islands) | Medium |
6775
| <span id="1253">1253</span> | [Reconstruct a 2-Row Binary Matrix](https://leetcode.com/problems/reconstruct-a-2-row-binary-matrix "重构 2 行二进制矩阵") | [Go](https://github.com/openset/leetcode/tree/master/problems/reconstruct-a-2-row-binary-matrix) | Medium |

Diff for: internal/leetcode/base.go

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var (
2222
jsonIndent = base.JSONIndent
2323
LockStr = " 🔒"
2424
translationSet = make(map[int]string)
25+
titleSlugID = make(map[string]int)
2526
)
2627

2728
// Clean - leetcode.Clean

Diff for: internal/leetcode/problems_all.go

+7
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,10 @@ func ProblemsAll() (ps ProblemsType) {
107107
})
108108
return
109109
}
110+
111+
func init() {
112+
questions := ProblemsAll().StatStatusPairs
113+
for _, item := range questions {
114+
titleSlugID[item.Stat.QuestionTitleSlug] = item.Stat.FrontendQuestionID
115+
}
116+
}

Diff for: internal/leetcode/question_data.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ func (question *questionType) getDescContent() []byte {
127127
var buf bytes.Buffer
128128
buf.WriteString(authInfo("description"))
129129
buf.WriteString(question.getNavigation())
130-
buf.WriteString(fmt.Sprintf("\n## [%s. %s%s](%s \"%s\")\n\n",
131-
question.QuestionFrontendID,
130+
buf.WriteString(fmt.Sprintf("\n## [%d. %s%s](%s \"%s\")\n\n",
131+
question.FrontendID(),
132132
question.Title,
133133
question.Difficulty.Str(),
134134
question.LeetCodeURL(),
@@ -152,6 +152,15 @@ func (question *questionType) questionID() int {
152152
return id
153153
}
154154

155+
func (question *questionType) FrontendID() int {
156+
id := titleSlugID[question.TitleSlug]
157+
if id != 0 {
158+
return id
159+
}
160+
id, _ = strconv.Atoi(question.QuestionFrontendID)
161+
return id
162+
}
163+
155164
func (question *questionType) translatedTitle() string {
156165
if question.TranslatedTitle == "" {
157166
question.TranslatedTitle = translationSet[question.questionID()]

Diff for: internal/leetcode/topic_tag.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,19 @@ type TagType struct {
3030
func (tag *TagType) SaveContents() {
3131
questions := GetTopicTag(tag.Slug).Data.TopicTag.Questions
3232
sort.Slice(questions, func(i, j int) bool {
33-
m, _ := strconv.Atoi(questions[i].QuestionFrontendID)
34-
n, _ := strconv.Atoi(questions[j].QuestionFrontendID)
35-
return m > n
33+
return questions[i].frontendID() > questions[j].frontendID()
3634
})
3735
var buf bytes.Buffer
3836
buf.WriteString(authInfo("tag"))
3937
buf.WriteString(fmt.Sprintf("\n## [话题分类](https://github.com/openset/leetcode/blob/master/tag/README.md) > %s\n\n", tag.name()))
4038
buf.WriteString("| # | 题名 | 标签 | 难度 |\n")
4139
buf.WriteString("| :-: | - | - | :-: |\n")
42-
format := "| %s | [%s](https://github.com/openset/leetcode/tree/master/problems/%s)%s | %s | %s |\n"
40+
format := "| %d | [%s](https://github.com/openset/leetcode/tree/master/problems/%s)%s | %s | %s |\n"
4341
for _, question := range questions {
4442
if question.TranslatedTitle == "" {
4543
question.TranslatedTitle = question.Title
4644
}
47-
buf.WriteString(fmt.Sprintf(format, question.QuestionFrontendID, question.TranslatedTitle, question.TitleSlug, question.IsPaidOnly.Str(), question.TagsStr(), question.Difficulty))
45+
buf.WriteString(fmt.Sprintf(format, question.frontendID(), question.TranslatedTitle, question.TitleSlug, question.IsPaidOnly.Str(), question.TagsStr(), question.Difficulty))
4846
}
4947
filename := filepath.Join("tag", tag.Slug, "README.md")
5048
filePutContents(filename, buf.Bytes())
@@ -95,6 +93,15 @@ func (question *ttQuestionType) TagsStr() string {
9593
return buf.String()
9694
}
9795

96+
func (question *ttQuestionType) frontendID() int {
97+
id := titleSlugID[question.TitleSlug]
98+
if id != 0 {
99+
return id
100+
}
101+
id, _ = strconv.Atoi(question.QuestionFrontendID)
102+
return id
103+
}
104+
98105
// GetTags - leetcode.GetTags
99106
func GetTags() (tags []TagType) {
100107
cts := fileGetContents(tagsFile)

Diff for: internal/post/post.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func runPost(cmd *base.Command, args []string) {
7979
strings.Join(tags, ", "),
8080
question.TitleSlug,
8181
))
82-
buf.WriteString(fmt.Sprintf("\n## %s. %s%s\n\n", question.QuestionFrontendID, question.TranslatedTitle, question.Difficulty.Str()))
82+
buf.WriteString(fmt.Sprintf("\n## %d. %s%s\n\n", question.FrontendID(), question.TranslatedTitle, question.Difficulty.Str()))
8383
buf.WriteString("{% raw %}\n\n")
8484
content := strings.ReplaceAll(question.TranslatedContent, "\r", "")
8585
// remove style

Diff for: problems/convert-to-base-2/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
### Related Topics
5959
[[Math](https://github.com/openset/leetcode/tree/master/tag/math/README.md)]
6060

61+
### Similar Questions
62+
1. [Encode Number](https://github.com/openset/leetcode/tree/master/problems/encode-number) (Medium)
63+
6164
### Hints
6265
<details>
6366
<summary>Hint 1</summary>

Diff for: problems/encode-number/README.md

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
2+
<!--+----------------------------------------------------------------------+-->
3+
<!--|@author openset <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](https://github.com/openset/leetcode/tree/master/problems/maximum-score-words-formed-by-letters "Maximum Score Words Formed by Letters")
9+
                
10+
[Next >](https://github.com/openset/leetcode/tree/master/problems/smallest-common-region "Smallest Common Region")
11+
12+
## [1256. Encode Number (Medium)](https://leetcode.com/problems/encode-number "加密数字")
13+
14+
<p>Given a non-negative integer <code>num</code>, Return its <em>encoding</em> string.</p>
15+
16+
<p>The encoding is done by converting the integer to a string using a secret function that you should deduce from the following table:</p>
17+
18+
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/06/21/encode_number.png" style="width: 164px; height: 360px;" /></p>
19+
20+
<p>&nbsp;</p>
21+
<p><strong>Example 1:</strong></p>
22+
23+
<pre>
24+
<strong>Input:</strong> num = 23
25+
<strong>Output:</strong> &quot;1000&quot;
26+
</pre>
27+
28+
<p><strong>Example 2:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> num = 107
32+
<strong>Output:</strong> &quot;101100&quot;
33+
</pre>
34+
35+
<p>&nbsp;</p>
36+
<p><strong>Constraints:</strong></p>
37+
38+
<ul>
39+
<li><code>0 &lt;= num &lt;= 10^9</code></li>
40+
</ul>
41+
42+
### Related Topics
43+
[[Bit Manipulation](https://github.com/openset/leetcode/tree/master/tag/bit-manipulation/README.md)]
44+
[[Math](https://github.com/openset/leetcode/tree/master/tag/math/README.md)]
45+
46+
### Similar Questions
47+
1. [Convert to Base -2](https://github.com/openset/leetcode/tree/master/problems/convert-to-base-2) (Medium)
48+
49+
### Hints
50+
<details>
51+
<summary>Hint 1</summary>
52+
Try to find the number of binary digits returned by the function.
53+
</details>
54+
55+
<details>
56+
<summary>Hint 2</summary>
57+
The pattern is to start counting from zero after determining the number of binary digits.
58+
</details>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
2+
<!--+----------------------------------------------------------------------+-->
3+
<!--|@author openset <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](https://github.com/openset/leetcode/tree/master/problems/shift-2d-grid "Shift 2D Grid")
9+
                
10+
[Next >](https://github.com/openset/leetcode/tree/master/problems/greatest-sum-divisible-by-three "Greatest Sum Divisible by Three")
11+
12+
## [1261. Find Elements in a Contaminated Binary Tree (Medium)](https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree "在受污染的二叉树中查找元素")
13+
14+
<p>Given a&nbsp;binary tree with the following rules:</p>
15+
16+
<ol>
17+
<li><code>root.val == 0</code></li>
18+
<li>If <code>treeNode.val == x</code> and <code>treeNode.left != null</code>, then <code>treeNode.left.val == 2 * x + 1</code></li>
19+
<li>If <code>treeNode.val == x</code> and <code>treeNode.right != null</code>, then <code>treeNode.right.val == 2 * x + 2</code></li>
20+
</ol>
21+
22+
<p>Now the binary tree is contaminated, which means all&nbsp;<code>treeNode.val</code>&nbsp;have&nbsp;been changed to <code>-1</code>.</p>
23+
24+
<p>You need to first recover the binary tree and then implement the <code>FindElements</code> class:</p>
25+
26+
<ul>
27+
<li><code>FindElements(TreeNode* root)</code>&nbsp;Initializes the object with a&nbsp;contamined binary tree, you need to recover it first.</li>
28+
<li><code>bool find(int target)</code>&nbsp;Return if the <code>target</code> value exists in the recovered binary tree.</li>
29+
</ul>
30+
31+
<p>&nbsp;</p>
32+
<p><strong>Example 1:</strong></p>
33+
34+
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/11/06/untitled-diagram-4-1.jpg" style="width: 320px; height: 119px;" /></strong></p>
35+
36+
<pre>
37+
<strong>Input</strong>
38+
[&quot;FindElements&quot;,&quot;find&quot;,&quot;find&quot;]
39+
[[[-1,null,-1]],[1],[2]]
40+
<strong>Output</strong>
41+
[null,false,true]
42+
<strong>Explanation</strong>
43+
FindElements findElements = new FindElements([-1,null,-1]);
44+
findElements.find(1); // return False
45+
findElements.find(2); // return True </pre>
46+
47+
<p><strong>Example 2:</strong></p>
48+
49+
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/11/06/untitled-diagram-4.jpg" style="width: 400px; height: 198px;" /></strong></p>
50+
51+
<pre>
52+
<strong>Input</strong>
53+
[&quot;FindElements&quot;,&quot;find&quot;,&quot;find&quot;,&quot;find&quot;]
54+
[[[-1,-1,-1,-1,-1]],[1],[3],[5]]
55+
<strong>Output</strong>
56+
[null,true,true,false]
57+
<strong>Explanation</strong>
58+
FindElements findElements = new FindElements([-1,-1,-1,-1,-1]);
59+
findElements.find(1); // return True
60+
findElements.find(3); // return True
61+
findElements.find(5); // return False</pre>
62+
63+
<p><strong>Example 3:</strong></p>
64+
65+
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/11/07/untitled-diagram-4-1-1.jpg" style="width: 306px; height: 274px;" /></strong></p>
66+
67+
<pre>
68+
<strong>Input</strong>
69+
[&quot;FindElements&quot;,&quot;find&quot;,&quot;find&quot;,&quot;find&quot;,&quot;find&quot;]
70+
[[[-1,null,-1,-1,null,-1]],[2],[3],[4],[5]]
71+
<strong>Output</strong>
72+
[null,true,false,false,true]
73+
<strong>Explanation</strong>
74+
FindElements findElements = new FindElements([-1,null,-1,-1,null,-1]);
75+
findElements.find(2); // return True
76+
findElements.find(3); // return False
77+
findElements.find(4); // return False
78+
findElements.find(5); // return True
79+
</pre>
80+
81+
<p>&nbsp;</p>
82+
<p><strong>Constraints:</strong></p>
83+
84+
<ul>
85+
<li><code>TreeNode.val == -1</code></li>
86+
<li>The height of the binary tree is less than or equal to <code>20</code></li>
87+
<li>The total number of nodes is between <code>[1,&nbsp;10^4]</code></li>
88+
<li>Total calls of <code>find()</code> is between <code>[1,&nbsp;10^4]</code></li>
89+
<li><code>0 &lt;= target &lt;= 10^6</code></li>
90+
</ul>
91+
92+
### Related Topics
93+
[[Tree](https://github.com/openset/leetcode/tree/master/tag/tree/README.md)]
94+
[[Hash Table](https://github.com/openset/leetcode/tree/master/tag/hash-table/README.md)]
95+
96+
### Hints
97+
<details>
98+
<summary>Hint 1</summary>
99+
Use DFS to traverse the binary tree and recover it.
100+
</details>
101+
102+
<details>
103+
<summary>Hint 2</summary>
104+
Use a hashset to store TreeNode.val for finding.
105+
</details>

Diff for: problems/friend-requests-ii-who-has-the-most-friends/README.md

+25-17
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,48 @@
1111

1212
## [602. Friend Requests II: Who Has the Most Friends (Medium)](https://leetcode.com/problems/friend-requests-ii-who-has-the-most-friends "好友申请 II :谁有最多的好友")
1313

14-
In social network like Facebook or Twitter, people send friend requests and accept others&#39; requests as well.
15-
<p>&nbsp;</p>
16-
Table <code>request_accepted</code> holds the data of friend acceptance, while <b>requester_id</b> and <b>accepter_id</b> both are the id of a person.
14+
<p>In social network like Facebook or Twitter, people send friend requests and accept others&#39; requests as well.</p>
1715

1816
<p>&nbsp;</p>
1917

18+
<p>Table <code>request_accepted</code></p>
19+
2020
<pre>
21+
+--------------+-------------+------------+
2122
| requester_id | accepter_id | accept_date|
2223
|--------------|-------------|------------|
2324
| 1 | 2 | 2016_06-03 |
2425
| 1 | 3 | 2016-06-08 |
2526
| 2 | 3 | 2016-06-08 |
2627
| 3 | 4 | 2016-06-09 |
28+
+--------------+-------------+------------+
29+
This table holds the data of friend acceptance, while <b>requester_id</b> and <b>accepter_id</b> both are the id of a person.
2730
</pre>
28-
Write a query to find the the people who has most friends and the most friends number. For the sample data above, the result is:
2931

30-
<pre>
31-
| id | num |
32-
|----|-----|
33-
| 3 | 3 |
34-
</pre>
35-
<b>Note:</b>
32+
<p>&nbsp;</p>
33+
34+
<p>Write a query to find the the people who has most friends and the most friends number under the following rules:</p>
3635

3736
<ul>
3837
<li>It is guaranteed there is only 1 people having the most friends.</li>
39-
<li>The friend request could only been accepted once, which mean there is no multiple records with the same <b>requester_id</b> and <b>accepter_id</b> value.
40-
<p>&nbsp;</p>
41-
<b>Explanation:</b><br />
42-
The person with id &#39;3&#39; is a friend of people &#39;1&#39;, &#39;2&#39; and &#39;4&#39;, so he has 3 friends in total, which is the most number than any others.
43-
<p>&nbsp;</p>
44-
<b>Follow-up:</b><br />
45-
In the real world, multiple people could have the same most number of friends, can you find all these people in this case?</li>
38+
<li>The friend request could only been accepted once, which mean there is no multiple records with the same <b>requester_id</b> and <b>accepter_id</b> value.</li>
4639
</ul>
4740

41+
<p>For the sample data above, the result is:</p>
42+
43+
<pre>
44+
Result table:
45+
+------+------+
46+
| id | num |
47+
|------|------|
48+
| 3 | 3 |
49+
+------+------+
50+
The person with id &#39;3&#39; is a friend of people &#39;1&#39;, &#39;2&#39; and &#39;4&#39;, so he has 3 friends in total, which is the most number than any others.
51+
</pre>
52+
53+
<p><b>Follow-up:</b><br />
54+
In the real world, multiple people could have the same most number of friends, can you find all these people in this case?</p>
55+
4856
### Hints
4957
<details>
5058
<summary>Hint 1</summary>

0 commit comments

Comments
 (0)