File tree Expand file tree Collapse file tree 3 files changed +18
-14
lines changed
solution/src/main/java/com/inuker/solution
test/src/main/java/com/inuker/test Expand file tree Collapse file tree 3 files changed +18
-14
lines changed Original file line number Diff line number Diff line change @@ -13,16 +13,11 @@ public class ValidWordSquare {
13
13
14
14
public boolean validWordSquare (List <String > words ) {
15
15
for (int i = 0 ; i < words .size (); i ++) {
16
- String word = words .get (i );
17
- /**
18
- * 如果单词长度比单词个数还多肯定是非法,因为横向和纵向长度不同
19
- */
20
- if (word .length () > words .size ()) {
21
- return false ;
22
- }
23
- for (int j = 0 ; j < word .length (); j ++) {
24
- String s = words .get (j );
25
- if (i >= s .length () || word .charAt (j ) != s .charAt (i )) {
16
+ for (int j = 0 ; j < words .get (i ).length (); j ++) {
17
+ if (j >= words .size () || i >= words .get (j ).length ()) {
18
+ return false ;
19
+ }
20
+ if (words .get (i ).charAt (j ) != words .get (j ).charAt (i )) {
26
21
return false ;
27
22
}
28
23
}
Original file line number Diff line number Diff line change 16
16
* 第一步建立所有前缀对应的字符串集合
17
17
* 第二步依次填充
18
18
*/
19
- public class WordSquare {
19
+ public class WordSquares {
20
20
21
21
public List <List <String >> wordSquares (String [] words ) {
22
22
List <List <String >> ret = new ArrayList <List <String >>();
Original file line number Diff line number Diff line change @@ -22,8 +22,17 @@ public static void main(String[] args) {
22
22
23
23
}
24
24
25
- class LFUCache {
26
-
27
-
25
+ public boolean validWordSquare (List <String > words ) {
26
+ for (int i = 0 ; i < words .size (); i ++) {
27
+ for (int j = 0 ; j < words .get (i ).length (); j ++) {
28
+ if (j >= words .size () || i >= words .get (j ).length ()) {
29
+ return false ;
30
+ }
31
+ if (words .get (i ).charAt (j ) != words .get (j ).charAt (i )) {
32
+ return false ;
33
+ }
34
+ }
35
+ }
36
+ return true ;
28
37
}
29
38
}
You can’t perform that action at this time.
0 commit comments