我正在尝试解决http://projecteuler.net/problem=1.
我想创建一个方法,它接受一个整数,然后创建一个包含在它之前的所有整数的数组,并将整数本身作为数组中的值.
以下是我到目前为止的情况.代码不起作用.
def make_array(num)
numbers = Array.new num
count = 1
numbers.each do |number|
numbers << number = count
count = count + 1
end
return numbers
end
make_array(10)
Run Code Online (Sandbox Code Playgroud) 我正在编写一个方法,允许我计算String类型的元素在String类型的LinkedList中出现的次数.我的代码如下所示不起作用.我继续在下面评论的行中获得索引超出界限.似乎无法找到这个bug
public int findDuplicate (LinkedList<String> e) {
int j = 1;
LinkedList<String> test = e;
while (!test.isEmpty()){
test = e;
String value = test.pop();
//Screws up here when i = 6
for(int i =0; i<=test.size() && test.get(i)!=null; i++){
String value3 = test.get(i);
if(e.get(i).equals(value) && i<=test.size()){
String value2 = test.get(i);
j++;
String Duplicate = e.get(i);
e.remove(i);
}
}
System.out.println(value + " is listed " + j + " times");
}
return j;
}
Run Code Online (Sandbox Code Playgroud)
使用hashmaps ..仍然无效public void findDuplicate(LinkedList e){
Map<String,Integer> counts = …Run Code Online (Sandbox Code Playgroud) 我正在编写一个方法,允许我计算String类型的元素在String类型的LinkedList中出现的次数.我的代码如下所示不起作用.我继续在下面评论的行中获得索引超出界限.似乎无法找到这个bug
public int findDuplicate (LinkedList<String> e) {
int j = 1;
LinkedList<String> test = e;
while (!test.isEmpty()){
test = e;
String value = test.pop();
//Screws up here when i = 6
for(int i =0; i<=test.size() && test.get(i)!=null; i++){
String value3 = test.get(i);
if(e.get(i).equals(value) && i<=test.size()){
String value2 = test.get(i);
j++;
String Duplicate = e.get(i);
e.remove(i);
}
}
System.out.println(value + " is listed " + j + " times");
}
return j;
}
Run Code Online (Sandbox Code Playgroud)
使用hashmaps ..仍然无法正常工作
public void findDuplicate (LinkedList e) { …Run Code Online (Sandbox Code Playgroud)