Skip to content

Commit 9e1ceb2

Browse files
authored
Merge pull request gzc426#184 from beddingearly/master
4
2 parents 4a635fe + 0b8f148 commit 9e1ceb2

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
```
2+
class Solution {
3+
public int compress(char[] chars) {
4+
char base = chars[0];
5+
int count = 1;
6+
int pos = 0;
7+
for (int i = 1; i < chars.length; i++){
8+
if (base != chars[i]){
9+
base = chars[i];
10+
if (count == 1){
11+
chars[++pos] = base;
12+
continue;
13+
}
14+
// 先计数再写入下一个值
15+
for (int j = 0; j < String.valueOf(count).length(); j++){
16+
chars[++pos] = String.valueOf(count).charAt(j);
17+
}
18+
count = 1;
19+
chars[++pos] = base;
20+
}
21+
else {// base == chars[i]
22+
count ++;
23+
if (i == chars.length-1){
24+
if (count == 1){
25+
continue;
26+
}
27+
for (int j = 0; j < String.valueOf(count).length(); j++){
28+
chars[++pos] = String.valueOf(count).charAt(j);
29+
}
30+
31+
}
32+
33+
}
34+
35+
}
36+
pos++;
37+
//splitArray(chars, pos);
38+
if (pos == 0)
39+
return 1;
40+
return pos;
41+
}
42+
}
43+
```
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
```
2+
import java.util.*;
3+
class Solution {
4+
public List<String> findAndReplacePattern(String[] words, String pattern) {
5+
List<String> result = new ArrayList();
6+
String p = transfor(pattern);
7+
String w;
8+
String ww;
9+
for (int i = 0; i < words.length; i++){
10+
w = words[i];
11+
ww = transfor(w);
12+
if (ww.equals(p)){
13+
result.add(w);
14+
}
15+
16+
}
17+
return result;
18+
}
19+
20+
private String transfor(String s){
21+
Map<Character, Character> m = new HashMap<>();
22+
char j = 0;
23+
String ss = "";
24+
m.put(s.charAt(0), (char)(j + '0'));
25+
for(int i = 1; i < s.length(); i++){
26+
if (s.charAt(i-1) != s.charAt(i)){
27+
if (m.containsKey(s.charAt(i))){
28+
ss += m.get(s.charAt(i));
29+
}
30+
else {
31+
++j;
32+
ss += (char)(j+'0');
33+
m.put(s.charAt(i), (char)(j + '0'));
34+
}
35+
}
36+
}
37+
return ss;
38+
}
39+
}
40+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
```
2+
class Solution {
3+
public int firstUniqChar(String s) {
4+
//char[] c = s.toCharArray();
5+
Map<Character, Integer> m = new HashMap<>();
6+
for (int i = 0; i < s.length(); i++){
7+
if (m.containsKey(s.charAt(i))){
8+
m.put(s.charAt(i), m.get(s.charAt(i))+1);
9+
}
10+
else {
11+
m.put(s.charAt(i), 1);
12+
}
13+
}
14+
for (int i = 0; i < s.length(); i++){
15+
if (m.get(s.charAt(i)) == 1){
16+
return i;
17+
}
18+
}
19+
return -1;
20+
}
21+
}
22+
```

0 commit comments

Comments
 (0)