一位朋友给了我这段代码并说有一个bug.是的,这段代码永远运行.
我得到的答案是:
在打印任何东西之前,它会持续> 10 ^ 15年.
public class Match {
public static void main(String[] args) {
Pattern p = Pattern.compile("(aa|aab?)+");
int count = 0;
for(String s = ""; s.length() < 200; s += "a")
if (p.matcher(s).matches())
count++;
System.out.println(count);
}
}
Run Code Online (Sandbox Code Playgroud)
我真的不明白为什么我看到这种行为,我是java新手,你有什么建议吗?
看到这段代码:
import java.util.*;
public class Temp{
public static void main(String[] args){
List<int[]> list1 = new ArrayList<int[]>(); //WORKS!
List<double[]> list2 = new ArrayList<double[]>(); //WORKS!
//List<double> list3 = new ArrayList<double>(); //DOES NOT WORK
//List<int> list4 = new ArrayList<int>(); //DOES NOT WORK
}
}
Run Code Online (Sandbox Code Playgroud)
AFAIK,java泛型不支持原始类型,那么int[]编译是怎么回事?如何在这里进行自动装箱?
def insertion_sort(A):
for j in range(1, len(A)):
key = A[j]
i = j - 1
while (i >= 0) and (A[i] > key):
A[i+1] = A[i]
i = i-1
A[i+1] = key
return A
print insertion_sort([8, 1, 3, 4, 9, 5, 2])
Run Code Online (Sandbox Code Playgroud)
现在打印: [8, 1, 3, 4, 9, 5, 2]
但我假设,我正在改变列表A,那么为什么返回值相同?
我想写合并排序并卡在这里.
我的代码有什么问题?我试图在不引用任何资源的情况下实现它,并且不必要地编写这一行,因为Stackoverflow中的一些愚蠢的规则迫使我解释我的代码.
def merge_sort(A):
if len(A) <= 1:
return A
#split list in 2
mid = len(A)/2
B = A[:mid]
C = A[mid:]
B = merge_sort(B)
C = merge_sort(C)
#merge
result = []
while len(B) > 0 and len(C) > 0:
if B[0] > C[0]:
result.append(C.pop(0))
else:
result.append(B.pop(0))
if len(B) > 0:
result.extend(merge_sort(B))
else:
result.extend(merge_sort(C))
print merge_sort([8, 2, 1, 1, 4, 45, 9, 3])
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
Traceback (most recent call last):
File "merge_sort.py", line 31, in <module>
print merge_sort([8, 2, 1, …Run Code Online (Sandbox Code Playgroud)