X Tutup
Skip to content

Commit 159f0e7

Browse files
author
liwentian
committed
fd
1 parent bee879a commit 159f0e7

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@
243243
|316|[Remove Duplicate Letters](https://leetcode.com/problems/remove-duplicate-letters/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/RemoveDuplicateLetters.java)|70|
244244
|317|[Shortest Distance from All Buildings](https://leetcode.com/problems/shortest-distance-from-all-buildings/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/ShortestDistanceFromAllBuildings.java)|75|典型的bfs,多做两遍|
245245
|319|[Bulb Switcher](https://leetcode.com/problems/bulb-switcher/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/BulbSwitcher.java)|90|
246+
|320|[Generalized Abbreviation](https://leetcode.com/problems/generalized-abbreviation/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/GeneralizedAbbreviation.java)|70|这题是典型的back tracking,多做几遍|
246247
|323|[Number of Connected Components in an Undirected Graph](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/NumberOfConnectedComponents.java)||
247248
|324|[Wiggle Sort II](https://leetcode.com/problems/wiggle-sort-ii/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/WiggleSortII.java)|60|
248249
|325|[Maximum Size Subarray Sum Equals k](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/MaximumSizeSubarraySumEqualsK.java)|75|这题思路有意思,多做几遍|
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.leetcode.google;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
/**
7+
* Created by liwentian on 2017/9/1.
8+
*/
9+
10+
public class GeneralizedAbbreviation {
11+
12+
public List<String> generateAbbreviations(String word) {
13+
List<String> list = new LinkedList<>();
14+
helper(word, 0, list, "", 0);
15+
return list;
16+
}
17+
18+
private void helper(String word, int idx, List<String> list, String cur, int count) {
19+
if (idx == word.length()) {
20+
if (count > 0) {
21+
cur += count;
22+
}
23+
list.add(cur);
24+
return;
25+
}
26+
27+
helper(word, idx + 1, list, cur, count + 1);
28+
helper(word, idx + 1, list, cur + (count > 0 ? count : "") + word.charAt(idx), 0);
29+
}
30+
}
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
11
package com.inuker.solution;
22

3+
import java.util.LinkedList;
34
import java.util.List;
45

56
/**
67
* Created by liwentian on 2016/12/29.
78
*/
89

10+
/**
11+
* https://leetcode.com/articles/generalized-abbreviation/
12+
* 思路就是back tracking,对于每个字母,到底是选择留下还是缩写
13+
*/
914
public class GeneralizedAbbreviation {
1015

1116
public List<String> generateAbbreviations(String word) {
12-
return null;
17+
List<String> list = new LinkedList<>();
18+
helper(word, 0, list, "", 0);
19+
return list;
20+
}
21+
22+
private void helper(String word, int idx, List<String> list, String cur, int count) {
23+
if (idx == word.length()) {
24+
if (count > 0) {
25+
cur += count;
26+
}
27+
list.add(cur);
28+
return;
29+
}
30+
31+
helper(word, idx + 1, list, cur, count + 1);
32+
helper(word, idx + 1, list, cur + (count > 0 ? count : "") + word.charAt(idx), 0);
1333
}
1434
}

0 commit comments

Comments
 (0)
X Tutup