Skip to content

Commit 2acab57

Browse files
committed
commit changes to gh-pages aug 29
1 parent b546556 commit 2acab57

File tree

5 files changed

+147
-64
lines changed

5 files changed

+147
-64
lines changed
Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,62 @@
1+
## [Transpose](http://en.wikipedia.org/wiki/Transpose) then Reverse
12

2-
## TODO
3-
* write down thinking
3+
You must know this method, or it is difficult.
44

5+
```
6+
+---+---+
7+
| 1 | 2 |
8+
+---+---+
9+
| 3 | 4 |
10+
+---+---+
11+
```
12+
13+
Transpose: reflect the matrix over the main diagonal.
14+
15+
16+
```
17+
Before
18+
19+
+
20+
+---+---+
21+
| + | 2 |
22+
+---+---+
23+
| 3 | + |
24+
+---+---+
25+
+
26+
27+
After
28+
29+
+
30+
+---+---+
31+
| 1 | 3 |
32+
+---+---+
33+
| 2 | 4 |
34+
+---+---+
35+
+
36+
```
37+
38+
Reverse: exchange each number along the vertical line in the middle
39+
40+
```
41+
Before
42+
43+
|
44+
+---+---+
45+
| 1 | 3 |
46+
+---+---+
47+
| 2 | 4 |
48+
+---+---+
49+
|
50+
51+
52+
After
53+
54+
|
55+
+---+---+
56+
| 3 | 1 |
57+
+---+---+
58+
| 4 | 2 |
59+
+---+---+
60+
|
61+
62+
```
Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,39 @@
1+
## [Word Break](../word-break) with traceback
2+
3+
In order to output all solutions,
4+
just change P from `boolean` to a collection which contains all parent index.
5+
6+
7+
```
8+
0 1 2 3 4 5 6 7 8 9 A
9+
* c a t s a n d d o g
10+
11+
0 0 3 7
12+
4
13+
14+
15+
P[3] = [0]
16+
P[4] = [0]
17+
18+
P[7] = [3, 4]
19+
P[10] = [7]
20+
21+
```
22+
23+
## Recover strings using DFS
24+
25+
To find all strings break at breakpoint, just using DFS to find out them.
26+
27+
* 10, 7, 3, 0: dog sand cat
28+
* 10, 7, 4, 0: dog and cats
29+
30+
### Note:
31+
32+
offsets here is 1 larger than the original offset.
33+
34+
So, `7..10` here is to start form the next element, the 8th to the 11th(exclusive).
35+
36+
Luckily, restore the `S[8th..11th]` is just the same as `S[7..10]`,
37+
Because index of S starts from `0`.
138

2-
## TODO
3-
* write down thinking
439

Lines changed: 46 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,67 @@
11
public class Solution {
22

3-
final int STACK_SIZE = 10;
4-
5-
String[] stack;
6-
ArrayList<String> found;
7-
8-
void pushWord(int p, String word){
9-
while(p >= stack.length){
10-
stack = Arrays.copyOf(stack, stack.length + STACK_SIZE);
3+
String join(List<String> list){
4+
if(list.isEmpty()) return "";
5+
6+
StringBuilder s = new StringBuilder(list.get(0));
7+
8+
for(int i = 1; i < list.size(); i++){
9+
s.append(' ');
10+
s.append(list.get(i));
1111
}
12-
13-
stack[p] = word;
12+
13+
return s.toString();
1414
}
1515

16-
void wordBreak(String s, Set<String> dict, final int p){
17-
if("".equals(s) || s == null){
18-
19-
if ( p > 0){
20-
String join = stack[0];
21-
for(int i = 1; i < p ; i++){
22-
join += " " + stack[i];
23-
}
24-
25-
found.add(join);
26-
}
16+
ArrayList<Integer>[] P;
17+
char[] S;
18+
ArrayList<String> rt;
19+
20+
void joinAll(int offset, LinkedList<String> parents){
21+
22+
if(P[offset].isEmpty()){
2723

24+
rt.add(join(parents));
2825
return;
2926
}
3027

31-
for(String d : dict){
32-
if(s.startsWith(d)){
33-
34-
int l = d.length();
35-
pushWord(p, d);
36-
37-
wordBreak(s.substring(l), dict, p + 1);
38-
}
28+
for(Integer p : P[offset]){
29+
30+
parents.push(new String(S, p, offset - p));
31+
32+
joinAll(p, parents);
33+
34+
parents.pop();
3935
}
4036

4137
}
42-
43-
boolean _wordBreak(String s, Set<String> dict) {
44-
45-
char[] S = s.toCharArray();
46-
47-
boolean[] P = new boolean[S.length + 1];
48-
P[0] = true;
49-
38+
39+
public List<String> wordBreak(String s, Set<String> dict) {
40+
S = s.toCharArray();
41+
42+
P = new ArrayList[S.length + 1];
43+
P[0] = new ArrayList<Integer>();
44+
5045
for(int i = 0; i < S.length; i++){
51-
5246
for(int j = 0; j <= i; j++){
53-
if(P[j] && dict.contains(new String(S, j, i - j + 1))){
54-
P[i + 1] = true;
55-
continue;
47+
String w = new String(S, j, i - j + 1);
48+
if(P[j] != null && dict.contains(w)){
49+
50+
if(P[i + 1] == null){
51+
P[i + 1] = new ArrayList<Integer>();
52+
}
53+
54+
P[i + 1].add(j);
5655
}
5756
}
5857
}
58+
59+
rt = new ArrayList<String>();
5960

60-
return P[S.length];
61-
62-
63-
}
64-
65-
public ArrayList<String> wordBreak(String s, Set<String> dict) {
66-
// Note: The Solution object is instantiated only once and is reused by each test case.
67-
68-
found = new ArrayList<String>();
69-
70-
if(_wordBreak(s, dict)){
71-
if(dict.size() <= 0) return found;
72-
stack = new String[STACK_SIZE];
73-
74-
wordBreak(s, dict, 0);
61+
if(P[S.length] != null){
62+
joinAll(S.length, new LinkedList<String>());
7563
}
7664

77-
return found;
65+
return rt;
7866
}
79-
}
67+
}

rotate-image/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
22
layout: solution
33
title: Rotate Image
4-
date: 2014-07-23 02:42:48 +0800
4+
date: 2014-08-28 16:12:56 +0800
5+
eaten: true
56
---
67
{% assign leetcode_name = {{page.path | remove: '/index.md'}} %}
78
{% assign leetcode_readme = {{leetcode_name | append: '/README.md' | prepend: '_root/' }} %}

word-break-ii/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
22
layout: solution
33
title: Word Break II
4-
date: 2014-07-23 02:42:48 +0800
4+
date: 2014-08-28 22:21:44 +0800
5+
eaten: true
56
---
67
{% assign leetcode_name = {{page.path | remove: '/index.md'}} %}
78
{% assign leetcode_readme = {{leetcode_name | append: '/README.md' | prepend: '_root/' }} %}

0 commit comments

Comments
 (0)