嗨,这是我的算法,它采用一个前面排序的浮点数的数组.因为我认为在使用这个算法之前我们对数组进行排序;它的最坏情况性能将是O(nlogn)但没有排序它将是O( n ^ 2).所以我认为这个算法可以找到一个重复的数字.我是吗?谢谢
1 Algorithm Duplicate_Number(a , n)
2 // Find one duplicate number in a[1 :n ]
3 {
4 temp: = a [0];
5 while (i<n) do
6 {
7 if (temp=a[i])
8 {
9 return a[i]; break;
10 }
11 else
12 temp: =a [++i];
13 }
Run Code Online (Sandbox Code Playgroud) 嗨我写了这部分代码.在for循环之前我有这个arraylist:
[X :49.0 Y: 113.0 , angle :0.0, X :141.0 Y: 106.0 , angle :0.0, X :110.0 Y: 185.0 , angle: 1.0768211289482128, X :99.0 Y: 139.0 , angle: 1.9961041242180873, X :103.0 Y: 126.0 , angle : 2.4208694638343324]
这表明"x" and "y" and "angle"了一些观点.但在for循环中我想删除那些超过的元素
X :110.0 Y: 185.0 , angle: 1.0768211289482128
但它为我打印这个数组:
[X :49.0 Y: 113.0angle0.0, X :141.0 Y: 106.0 , angle:0.0, X :110.0 Y: 185.0 , angle: 1.0768211289482128, X :103.0 Y: 126.0 , angle: 2.4208694638343324]
哪个不对
int size = …Run Code Online (Sandbox Code Playgroud) 嗨我已经在下面写了这样的代码,但它返回错误的输出,这不是最小值.
private Element heuristic_Function(List<Element> objectList) {
System.out.println(objectList.toString());
Element node =objectList.get(objectList.size() - 1);
double leastValue = objectList.get(0).getGreedy();
System.out.println(leastValue);
for (Element e : objectList) {
if (leastValue > e.getGreedy()) {
leastValue = e.getGreedy();
node = e;
}
}
System.out.println(node.toString());
System.out.println(leastValue);
return node;
}
Run Code Online (Sandbox Code Playgroud)
我多次向此方法发送一个列表,但它会返回错误的输出.
输出:
[digit:1 greedy2.87 , digit:2 greedy3.67 ,digit:3 greedy3.24 , digit:4 greedy3.67 ] //System.out.println(objectList.toString());
2.87 //System.out.println(leastValue);
digit:4 greedy3.67 //System.out.println(node.toString());
2.87 //System.out.println(leastValue);
[digit:1 greedy2.87 , digit:2 greedy3.67 , digit:3 greedy3.24 , digit:1 greedy3.67 , digit:2 greedy4.47 , digit:3 greedy4.3500000000000005 ] …Run Code Online (Sandbox Code Playgroud) 嗨,我已经写了这个代码,输出你可以得到该.remove()方法不起作用.a,b,c,和d一些Points对象有x和y成员.
以下是a and b and c and d值,必须删除if语句中的值,upper但不是.
X :59 Y: 143
X :165 Y: 140
X :59 Y: 143
X :165 Y: 140
System.out.println(upper.toString());
for(int i =0;i<upper.size();i++)
if(upper.get(i)==a||upper.get(i)==b||upper.get(i)==c||upper.get(i)==d){
upper.remove(i);
}
for(int i =0;i<lower.size();i++)
if(lower.get(i)==a||lower.get(i)==b||lower.get(i)==c||lower.get(i)==d){
upper.remove(i);
}
System.out.println(upper.toString());
System.out.println(lower.toString());
first println : [X :108 Y: 89, X :165 Y: 140]
second println: [X :108 Y: 89, X :165 Y: 140] …Run Code Online (Sandbox Code Playgroud) 其他人说,制作带有数组的二进制搜索树{3,7,1,90,45,67,54,23,...}是很好的.但是TreeSet我的代码会引发异常,我不知道为什么?我的数组列表"array"包含100 objects每个对象都有two fields 1)digit 2)name,我想BST用这些对象的数字字段.请帮助我谢谢.
TreeSet<Element> set = null;
set = new TreeSet<Element>();
for(Element e :array){
set.add(e);
}
Iterator it1 = set.iterator();
while (it1.hasNext()) {
Object o1 = it1.next();
System.out.println(o1);
}
Run Code Online (Sandbox Code Playgroud)
例外:
Exception in thread "main" java.lang.ClassCastException: OBST.Element cannot be cast to java.lang.Comparable
at java.util.TreeMap.put(TreeMap.java:542)
at java.util.TreeSet.add(TreeSet.java:238)
at OBST.GreedyVersion.<init>(GreedyVersion.java:25)
at OBST.GreedyVersion.main(GreedyVersion.java:66)
Run Code Online (Sandbox Code Playgroud)
这是因为线: set.add(e);
我已经编写了下面的代码.但它会打印出这个异常,我真的不知道它的问题是什么,请帮助我谢谢
码:
private void fillMinAverageTime() { //T(n) = O(n^3)
for (int i = list.size() - 2; i >= 0; i--) {
for (int j = i + 1; j < list.size(); j++) {
for (k = i; k <= j; k++) {
minOne = fillMinAverageTimeArray(i, j);
if (min == 0.0) {
min = minOne;
} else if (minOne < min) {
min = minOne;
}
}
min = 0.0;
minOne = 0.0;
minAverageTimeArray[i][j] = min + probability[i][j];
}
}
} …Run Code Online (Sandbox Code Playgroud) 我写了这么简单的代码:
我在我的类的构造函数中写了这一行: List element = new ArrayList();
我有一个名为cost其类型 的变量int
一个方法将返回三个具有不同对象的列表: listOne, listTwo, listThree
在另一种方法中我写了下面的代码,这个方法将使用在上面的方法中创建的那些列表.对于上面的三个列表,这个方法将被调用三次.每个名单的每个电话.
// begining of the method:
int cost = 0;
if(cost==0){
element = listOne;
cost = 3;
}
if(cost<4){
element = listtwo;
cost = 6;
}
// end
System.out.println(element.toString());
Run Code Online (Sandbox Code Playgroud)不幸的是,listTwo它将打印而不是打印listThree(如果我们有4个或更多列表,它将打印最后一个)!
if-else条件有问题吗?
谢谢
编辑:这是我的主要代码,但它的条件类似于上面的代码:
auxiliaryList在下面的代码是listOne或list Two或listThree分别.
cost = 0;
public void method {
System.out.println(element.toString());//number one
if (cost== 0) {
element = auxiliaryList;
cost = …Run Code Online (Sandbox Code Playgroud) 我已经编写了下面的代码,如果这个else if语句运行,我想突然从这个方法出来,然后回到调用这个方法的下一行.我已经使用了返回但是效果不好.
else if (balance == 0 && noSolution == 0) {
noSolution = 0;
return; // it doesn't work.
}
Run Code Online (Sandbox Code Playgroud)
方法:
public <E> void rand_Function(List<E> tree, List<E> array) {
if (array.isEmpty()) {
return;
}
if (array.size() == 1) {
preorder = (List<Element>) new ArrayList<E>(tree);
preorder.addAll((Collection<? extends Element>) array);
for (Element e : preorder) {
e.setLevel(0);
}
E1 = getAverageAccessTime(preorder);
listTwo = new ArrayList<Element>(preorder);
if ((E1 < E) || (rand.nextDouble() <= Math.exp(-(Math.abs(E1 - E)) / 0.5 * T))) …Run Code Online (Sandbox Code Playgroud) 嗨,我有一个问题,在下面的代码中我们需要写n--;吗?
int n = pointList.size();
for(int i=pointList.size()-1;i>=0;i--){
for(int j=0;j<list.size();j++){
if(pointList.get(i).equals(list.get(j))){
pointList.remove(i);
n--;
}
}
}
Run Code Online (Sandbox Code Playgroud)
list也是一个arrayList.谢谢.