Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 3742681

Browse files
committed
1047 done
1 parent 8ede965 commit 3742681

File tree

5 files changed

+104
-26
lines changed

5 files changed

+104
-26
lines changed

Algorithms/1046.last-stone-weight/last-stone-weight_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ var tcs = []struct {
1212
ans int
1313
}{
1414

15+
{
16+
[]int{1, 1, 1, 1},
17+
0,
18+
},
19+
1520
{
1621
[]int{2, 7, 4, 1, 8, 1},
1722
1,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# [1047. Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/)
2+
3+
Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent and equal letters, and removing them.
4+
5+
We repeatedly make duplicate removals on S until we no longer can.
6+
7+
Return the final string after all such duplicate removals have been made. It is guaranteed the answer is unique.
8+
9+
Example 1:
10+
11+
```text
12+
Input: "abbaca"
13+
Output: "ca"
14+
Explanation:
15+
For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".
16+
```
17+
18+
Note:
19+
20+
1. `1 <= S.length <= 20000`
21+
1. `S consists only of English lowercase letters.`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package problem1047
2+
3+
func removeDuplicates(S string) string {
4+
bs := []byte(S)
5+
stack, top := make([]byte, len(S)), -1
6+
for _, b := range bs {
7+
if top >= 0 && stack[top] == b {
8+
top--
9+
} else {
10+
top++
11+
stack[top] = b
12+
}
13+
}
14+
return string(stack[:top+1])
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package problem1047
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
// tcs is testcase slice
10+
var tcs = []struct {
11+
S string
12+
ans string
13+
}{
14+
15+
{
16+
"abbaca",
17+
"ca",
18+
},
19+
20+
// 可以有多个 testcase
21+
}
22+
23+
func Test_removeDuplicates(t *testing.T) {
24+
ast := assert.New(t)
25+
26+
for _, tc := range tcs {
27+
ast.Equal(tc.ans, removeDuplicates(tc.S), "输入:%v", tc)
28+
}
29+
}
30+
31+
func Benchmark_removeDuplicates(b *testing.B) {
32+
for i := 0; i < b.N; i++ {
33+
for _, tc := range tcs {
34+
removeDuplicates(tc.S)
35+
}
36+
}
37+
}

leetcode.json

+26-26
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"Username": "aQuaYi",
3-
"Ranking": 614,
4-
"Updated": "2019-07-02T20:22:19.35265445+08:00",
3+
"Ranking": 613,
4+
"Updated": "2019-07-03T20:18:56.536384624+08:00",
55
"Record": {
66
"Easy": {
77
"Solved": 246,
8-
"Total": 253
8+
"Total": 252
99
},
1010
"Medium": {
1111
"Solved": 413,
12-
"Total": 428
12+
"Total": 429
1313
},
1414
"Hard": {
1515
"Solved": 175,
@@ -229,7 +229,7 @@
229229
"ID": 17,
230230
"Title": "Letter Combinations of a Phone Number",
231231
"TitleSlug": "letter-combinations-of-a-phone-number",
232-
"PassRate": "41%",
232+
"PassRate": "42%",
233233
"Difficulty": "Medium",
234234
"IsAccepted": true,
235235
"IsPaid": false,
@@ -877,7 +877,7 @@
877877
"ID": 71,
878878
"Title": "Simplify Path",
879879
"TitleSlug": "simplify-path",
880-
"PassRate": "28%",
880+
"PassRate": "29%",
881881
"Difficulty": "Medium",
882882
"IsAccepted": true,
883883
"IsPaid": false,
@@ -1249,7 +1249,7 @@
12491249
"ID": 102,
12501250
"Title": "Binary Tree Level Order Traversal",
12511251
"TitleSlug": "binary-tree-level-order-traversal",
1252-
"PassRate": "48%",
1252+
"PassRate": "49%",
12531253
"Difficulty": "Medium",
12541254
"IsAccepted": true,
12551255
"IsPaid": false,
@@ -1273,7 +1273,7 @@
12731273
"ID": 104,
12741274
"Title": "Maximum Depth of Binary Tree",
12751275
"TitleSlug": "maximum-depth-of-binary-tree",
1276-
"PassRate": "60%",
1276+
"PassRate": "61%",
12771277
"Difficulty": "Easy",
12781278
"IsAccepted": true,
12791279
"IsPaid": false,
@@ -1765,7 +1765,7 @@
17651765
"ID": 145,
17661766
"Title": "Binary Tree Postorder Traversal",
17671767
"TitleSlug": "binary-tree-postorder-traversal",
1768-
"PassRate": "48%",
1768+
"PassRate": "49%",
17691769
"Difficulty": "Hard",
17701770
"IsAccepted": true,
17711771
"IsPaid": false,
@@ -2785,7 +2785,7 @@
27852785
"ID": 230,
27862786
"Title": "Kth Smallest Element in a BST",
27872787
"TitleSlug": "kth-smallest-element-in-a-bst",
2788-
"PassRate": "51%",
2788+
"PassRate": "52%",
27892789
"Difficulty": "Medium",
27902790
"IsAccepted": true,
27912791
"IsPaid": false,
@@ -3277,7 +3277,7 @@
32773277
"ID": 271,
32783278
"Title": "Encode and Decode Strings",
32793279
"TitleSlug": "encode-and-decode-strings",
3280-
"PassRate": "26%",
3280+
"PassRate": "27%",
32813281
"Difficulty": "Medium",
32823282
"IsAccepted": false,
32833283
"IsPaid": true,
@@ -4141,7 +4141,7 @@
41414141
"ID": 343,
41424142
"Title": "Integer Break",
41434143
"TitleSlug": "integer-break",
4144-
"PassRate": "47%",
4144+
"PassRate": "48%",
41454145
"Difficulty": "Medium",
41464146
"IsAccepted": true,
41474147
"IsPaid": false,
@@ -6205,7 +6205,7 @@
62056205
"ID": 515,
62066206
"Title": "Find Largest Value in Each Tree Row",
62076207
"TitleSlug": "find-largest-value-in-each-tree-row",
6208-
"PassRate": "57%",
6208+
"PassRate": "58%",
62096209
"Difficulty": "Medium",
62106210
"IsAccepted": true,
62116211
"IsPaid": false,
@@ -6253,7 +6253,7 @@
62536253
"ID": 519,
62546254
"Title": "Random Flip Matrix",
62556255
"TitleSlug": "random-flip-matrix",
6256-
"PassRate": "33%",
6256+
"PassRate": "32%",
62576257
"Difficulty": "Medium",
62586258
"IsAccepted": true,
62596259
"IsPaid": false,
@@ -6589,7 +6589,7 @@
65896589
"ID": 547,
65906590
"Title": "Friend Circles",
65916591
"TitleSlug": "friend-circles",
6592-
"PassRate": "53%",
6592+
"PassRate": "54%",
65936593
"Difficulty": "Medium",
65946594
"IsAccepted": true,
65956595
"IsPaid": false,
@@ -6685,7 +6685,7 @@
66856685
"ID": 555,
66866686
"Title": "Split Concatenated Strings",
66876687
"TitleSlug": "split-concatenated-strings",
6688-
"PassRate": "39%",
6688+
"PassRate": "40%",
66896689
"Difficulty": "Medium",
66906690
"IsAccepted": false,
66916691
"IsPaid": true,
@@ -7993,7 +7993,7 @@
79937993
"ID": 664,
79947994
"Title": "Strange Printer",
79957995
"TitleSlug": "strange-printer",
7996-
"PassRate": "36%",
7996+
"PassRate": "37%",
79977997
"Difficulty": "Hard",
79987998
"IsAccepted": true,
79997999
"IsPaid": false,
@@ -9637,7 +9637,7 @@
96379637
"ID": 801,
96389638
"Title": "Minimum Swaps To Make Sequences Increasing",
96399639
"TitleSlug": "minimum-swaps-to-make-sequences-increasing",
9640-
"PassRate": "34%",
9640+
"PassRate": "35%",
96419641
"Difficulty": "Medium",
96429642
"IsAccepted": true,
96439643
"IsPaid": false,
@@ -10321,7 +10321,7 @@
1032110321
"ID": 858,
1032210322
"Title": "Mirror Reflection",
1032310323
"TitleSlug": "mirror-reflection",
10324-
"PassRate": "51%",
10324+
"PassRate": "52%",
1032510325
"Difficulty": "Medium",
1032610326
"IsAccepted": true,
1032710327
"IsPaid": false,
@@ -10381,7 +10381,7 @@
1038110381
"ID": 863,
1038210382
"Title": "All Nodes Distance K in Binary Tree",
1038310383
"TitleSlug": "all-nodes-distance-k-in-binary-tree",
10384-
"PassRate": "47%",
10384+
"PassRate": "48%",
1038510385
"Difficulty": "Medium",
1038610386
"IsAccepted": true,
1038710387
"IsPaid": false,
@@ -12697,7 +12697,7 @@
1269712697
"ID": 1056,
1269812698
"Title": "Confusing Number",
1269912699
"TitleSlug": "confusing-number",
12700-
"PassRate": "52%",
12700+
"PassRate": "51%",
1270112701
"Difficulty": "Easy",
1270212702
"IsAccepted": false,
1270312703
"IsPaid": true,
@@ -12805,7 +12805,7 @@
1280512805
"ID": 1065,
1280612806
"Title": "Index Pairs of a String",
1280712807
"TitleSlug": "index-pairs-of-a-string",
12808-
"PassRate": "59%",
12808+
"PassRate": "58%",
1280912809
"Difficulty": "Easy",
1281012810
"IsAccepted": false,
1281112811
"IsPaid": true,
@@ -13249,7 +13249,7 @@
1324913249
"ID": 1102,
1325013250
"Title": "Path With Maximum Minimum Value",
1325113251
"TitleSlug": "path-with-maximum-minimum-value",
13252-
"PassRate": "36%",
13252+
"PassRate": "39%",
1325313253
"Difficulty": "Medium",
1325413254
"IsAccepted": false,
1325513255
"IsPaid": true,
@@ -13273,8 +13273,8 @@
1327313273
"ID": 1104,
1327413274
"Title": "Path In Zigzag Labelled Binary Tree",
1327513275
"TitleSlug": "path-in-zigzag-labelled-binary-tree",
13276-
"PassRate": "70%",
13277-
"Difficulty": "Easy",
13276+
"PassRate": "71%",
13277+
"Difficulty": "Medium",
1327813278
"IsAccepted": false,
1327913279
"IsPaid": false,
1328013280
"IsFavor": false,
@@ -13285,7 +13285,7 @@
1328513285
"ID": 1105,
1328613286
"Title": "Filling Bookcase Shelves",
1328713287
"TitleSlug": "filling-bookcase-shelves",
13288-
"PassRate": "51%",
13288+
"PassRate": "52%",
1328913289
"Difficulty": "Medium",
1329013290
"IsAccepted": false,
1329113291
"IsPaid": false,

0 commit comments

Comments
 (0)