如何优化这个函数(初学者)JAVA

huangapple go评论65阅读模式
英文:

How can i optimise this function (beginner ) JAVA

问题

以下是已翻译的代码部分:

public static int recherche(int cherche, int[] t) {
    int srch = 0;
    int tmp = 0;
    boolean result = false;
    for (int i = 0; i < t.length; i++) {
        if (t[i] == cherche && result == false) {
            tmp++;
            srch = i;
            result = true;
        }
    }
    if (tmp != 0) {
        return srch;
    } else {
        return -1;
    }
}

请注意,我已经按照您的要求进行了中文翻译,并且只返回了翻译好的代码部分,没有包含其他内容。如果您有任何更多的翻译需求,请随时告诉我。

英文:

I have this function and I need to optimise it so that it takes less time running:

public static int recherche(int cherche, int[] t) {
	int srch = 0;
	int tmp=0;
	boolean result = false;
	for (int i=0; i&lt;t.length ; i++) {
		if (t[i]== cherche &amp;&amp; result == false) {
			tmp++;
			srch = i;
		    result=true;
		}
	}		
	if (tmp!=0) { 
		return srch ;
	}
	else { 
	    return -1;
    }
}

Also I can't use any library tools.

答案1

得分: 2

如果我正确理解你的方法,你是在数组t中搜索第一个出现的cherche的索引,如果没有找到则返回-1。

你的代码问题在于,即使已经找到了条目,你仍然保持循环。最好立即终止循环。你也不需要额外的变量。

public static int recherche(int cherche, int[] t) {
    int srch = -1;
    for (int i = 0; i < t.length; i++) {
        if (t[i] == cherche) {
            srch = i;
            break;
        }
    }       
    return srch;
}

如果这仍然太慢,你可以尝试将数据存储为排序的,或者创建某种索引。

如果你有一个List而不是一个Array,你可以使用indexOf方法,但我认为对于未排序的数据来说它不会更快。
在最坏的情况下,它始终是O(n),因为你必须检查所有数组值。

附注:请考虑使用更好的变量名。我不知道tcherche分别代表什么。

英文:

If I understand your method correctly you are searching for the index of the first occurrence of cherche in your array t, or -1 if not found.

The problem with your code is, that you keep looping even if you already found the entry. It is better to break the loop immediately. You also do not need the extra variables.

public static int recherche(int cherche, int[] t) {
    int srch = -1;
    for (int i = 0; i &lt; t.length; i++) {
        if (t[i] == cherche) {
            srch = i;
            break;
        }
    }       
    return srch;
} 

If this is still too slow, you could try to store the data sorted or create some kind of index.

If you had a List instead of an Array, you could use the indexOf method, but I do not think it can be any faster for unsorted data.
It will be always O(n), because you have to check all array values in the worst case.

P.S: Please consider to use better variable names. I have no idea what t and cherche are.

答案2

得分: 1

这个函数在Java中无法进行真正的优化。如果你知道数组t是排序的,你可以使用二分查找。

我稍微优化了你的代码

public static int recherche(int cherche, int[] t) {
    for (int i = 0; i < t.length; i++) {
        if (t[i] == cherche) {
            return i;
        }
    }

    return -1;
}
英文:

There is no real optimization that can be done with this function in Java. If you know that the array t is sorted, you could use binary sort.

I cleaned your code up a bit though

public static int recherche(int cherche, int[] t) {
    for (int i = 0; i &lt; t.length; i++) {
        if (t[i] == cherche) {
            return i;
        }
    }

    return -1;
}

答案3

得分: 0

public static int research(int searched, int[] t) {
    for (int i=0; i < t.length ; i++) {
        if (t[i] == searched) {
            return i;
        }
    }
    return -1;
}
英文:

The optimizations you can do are returning the result right after you found it and removing redundant variables.

public static int research(int searched, int[] t) {
    for (int i=0; i &lt; t.length ; i++) {
        if (t[i] == searched) {
            return i;
        }
    }
    return -1;
}

huangapple
  • 本文由 发表于 2020年10月23日 01:12:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64487237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定