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

Commit9c0dd10

Browse files
committed
findword
1 parentc8175e1 commit9c0dd10

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
packageAlgorithms.algorithm.interviews.uber;
2+
3+
importjava.util.ArrayList;
4+
5+
publicclassFindWord {
6+
publicstaticvoidmain(String[]strs) {
7+
System.out.println(findAllStrings("xxisxhis"));
8+
9+
}
10+
11+
publicstaticbooleanisAWord(Strings) {
12+
if (s.equals("is")) {
13+
returntrue;
14+
}elseif (s.equals("his")){
15+
returntrue;
16+
}
17+
returnfalse;
18+
}
19+
20+
publicstaticArrayList<String>findAllStrings(Strings) {
21+
ArrayList<String>ret =newArrayList<String>();
22+
23+
if (s ==null ||s.length() ==0) {
24+
returnret;
25+
}
26+
27+
intlen =s.length();
28+
29+
for (inti =0;i <len;i++) {
30+
for (intj =i;j <len;j++) {
31+
Stringsub =s.substring(i,j +1);
32+
if (isAWord(sub)) {
33+
ret.add(sub);
34+
}
35+
}
36+
}
37+
38+
returnret;
39+
}
40+
}

‎dfs/Subsets.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,37 @@ public void subsets(int[] S, List<Integer> path, List<List<Integer>> ret, int in
3131
path.remove(path.size() -1);
3232
}
3333
}
34+
35+
publicList<List<Integer>>subsets2(int[]S) {
36+
List<List<Integer>>ret =newArrayList<List<Integer>>();
37+
if (S ==null ||S.length ==0) {
38+
returnret;
39+
}
40+
41+
intlen =S.length;
42+
Arrays.sort(S);
43+
44+
// forget to add (long).
45+
longnumOfSet = (long)Math.pow(2,len);
46+
47+
for (inti =0;i <numOfSet;i++) {
48+
// bug 3: should use tmp - i.
49+
longtmp =i;
50+
51+
ArrayList<Integer>list =newArrayList<Integer>();
52+
while (tmp !=0) {
53+
// bug 2: use error NumberOfTrailingZeros.
54+
intindexOfLast1 =Long.numberOfTrailingZeros(tmp);
55+
list.add(S[indexOfLast1]);
56+
57+
// clear the bit.
58+
tmp ^= (1 <<indexOfLast1);
59+
}
60+
61+
ret.add(list);
62+
}
63+
64+
returnret;
65+
}
3466
}
67+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp