字符串不同子串
代码如下:public class Main {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
int[] c = {0,0,0,1,1,0,0,0,1,0,1,0,0,0,1};
for (int i = 0; i < c.length; i++) {
for (int j = i; j < c.length; j++) {
List<Integer> arr = new ArrayList<>();
for ( int head = i;head <= j; head++) {
arr.add(c);
}
set.add(arr.toString());
}
}
System.out.print(set.size());
}
}
题目如图,答案是100
请问我的代码错在哪里了呢? import java.util.List;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
int[] c = {0, 1, 0,0,1,1,0,0,0,1,0,1,0,0,0,1}; // 这里少了个 1,数组的第二个元素
for (int i = 0; i < c.length; i++) {
for (int j = i; j < c.length; j++) {
List<Integer> arr = new ArrayList<>();
for ( int head = i;head <= j; head++) {
arr.add(c);
}
set.add(arr.toString());
}
}
System.out.print(set.size());
}
} isdkz 发表于 2022-3-5 17:48
不会JAVA不过100倒是对的,我用1楼代码 是87 后来查看 是少了个1
d=list(map(str,))
s=[]
for y in range(0,len(d)):
for x,i in enumerate(d):
if x+y+1<=len(d):
s.append("".join(d))
print(len(set(s)))
isdkz 发表于 2022-3-5 17:48
感谢!
wp231957 发表于 2022-3-5 18:27
不会JAVA不过100倒是对的,我用1楼代码 是87 后来查看 是少了个1
感谢!!
页:
[1]