Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0ba2286

Browse files
refactor 527
1 parent5cebad4 commit0ba2286

File tree

2 files changed

+60
-36
lines changed

2 files changed

+60
-36
lines changed

‎src/main/java/com/fishercoder/solutions/_527.java

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,48 +28,48 @@
2828
*/
2929
publicclass_527 {
3030

31-
/**reference: https://discuss.leetcode.com/topic/82613/really-simple-and-straightforward-java-solution*/
32-
publicList<String>wordsAbbreviation(List<String>dict) {
33-
intlen =dict.size();
34-
String[]ans =newString[len];
35-
int[]prefix =newint[len];
36-
for (inti =0;i <len;i++) {
37-
prefix[i] =1;
38-
ans[i] =abbreviate(dict.get(i),1);// make abbreviation for each string
39-
}
40-
for (inti =0;i <len;i++) {
41-
while (true) {
42-
HashSet<Integer>set =newHashSet<>();
43-
for (intj =i +1;j <len;j++) {
44-
if (ans[j].equals(ans[i])) {
45-
set.add(j);// check all strings with the same abbreviation
31+
publicstaticclassSolution1 {
32+
/**
33+
* reference: https://discuss.leetcode.com/topic/82613/really-simple-and-straightforward-java-solution
34+
*/
35+
publicList<String>wordsAbbreviation(List<String>dict) {
36+
intlen =dict.size();
37+
String[]ans =newString[len];
38+
int[]prefix =newint[len];
39+
for (inti =0;i <len;i++) {
40+
prefix[i] =1;
41+
ans[i] =abbreviate(dict.get(i),1);// make abbreviation for each string
42+
}
43+
for (inti =0;i <len;i++) {
44+
while (true) {
45+
HashSet<Integer>set =newHashSet<>();
46+
for (intj =i +1;j <len;j++) {
47+
if (ans[j].equals(ans[i])) {
48+
set.add(j);// check all strings with the same abbreviation
49+
}
50+
}
51+
if (set.isEmpty()) {
52+
break;
53+
}
54+
set.add(i);
55+
for (intk :set) {
56+
ans[k] =abbreviate(dict.get(k), ++prefix[k]);// increase the prefix
4657
}
47-
}
48-
if (set.isEmpty()) {
49-
break;
50-
}
51-
set.add(i);
52-
for (intk :set) {
53-
ans[k] =abbreviate(dict.get(k), ++prefix[k]);// increase the prefix
5458
}
5559
}
60+
returnArrays.asList(ans);
5661
}
57-
returnArrays.asList(ans);
58-
}
5962

60-
privateStringabbreviate(Stringword,intk) {
61-
if (k +2 >=word.length()) {
62-
returnword;
63+
privateStringabbreviate(Stringword,intk) {
64+
if (k +2 >=word.length()) {
65+
returnword;
66+
}
67+
StringBuilderstringBuilder =newStringBuilder();
68+
stringBuilder.append(word.substring(0,k));
69+
stringBuilder.append(word.length() -1 -k);
70+
stringBuilder.append(word.substring(word.length() -1));
71+
returnstringBuilder.toString();
6372
}
64-
StringBuilderstringBuilder =newStringBuilder();
65-
stringBuilder.append(word.substring(0,k));
66-
stringBuilder.append(word.length() -1 -k);
67-
stringBuilder.append(word.substring(word.length() -1));
68-
returnstringBuilder.toString();
6973
}
7074

71-
publicstaticvoidmain(String...args) {
72-
_527test =new_527();
73-
System.out.println(test.abbreviate("saaap",2));
74-
}
7575
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._527;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importjava.util.Arrays;
8+
9+
importstaticorg.junit.Assert.assertEquals;
10+
11+
publicclass_527Test {
12+
privatestatic_527.Solution1solution1;
13+
14+
@BeforeClass
15+
publicstaticvoidsetup() {
16+
solution1 =new_527.Solution1();
17+
}
18+
19+
@Test
20+
publicvoidtest1() {
21+
assertEquals(Arrays.asList("l2e","god","internal","me","i6t","interval","inte4n","f2e","intr4n"),solution1.wordsAbbreviation(Arrays.asList("like","god","internal","me","internet","interval","intension","face","intrusion")));
22+
}
23+
24+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp