Java集合与手动循环-编码挑战/访谈最佳实践
对于编码挑战/面试以及优化,使用集合是否有好处?例如,检查以下程序的回文运行时间Java集合与手动循环-编码挑战/访谈最佳实践,java,algorithm,optimization,collections,Java,Algorithm,Optimization,Collections,对于编码挑战/面试以及优化,使用集合是否有好处?例如,检查以下程序的回文运行时间 import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Palindrome { public static void main(String[] args) { // TODO Auto-generated method stub Scanner
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Palindrome {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String str = sc.next();
if(isPalindrome(str)) {
System.out.println("YES");
} else {
System.out.println("NO");
}
if(isPalindromeUsingSet(str)) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
public static boolean isPalindrome(String str) {
final long startTime = System.nanoTime();
final long duration;
for (int i=0;i<str.length()/2;i++) {
if(str.charAt(i) != str.charAt(str.length() -1 -i)) {
duration = System.nanoTime() - startTime;
System.out.println("isPalindrome - " + duration);
return false;
}
}
duration = System.nanoTime() - startTime;
System.out.println("isPalindrome - " + duration);
return true;
}
public static boolean isPalindromeUsingSet(String str) {
final long startTime = System.nanoTime();
final long duration;
Set<Character> charSet = new HashSet<>();
for (int i=0;i<str.length()/2;i++) {
if(charSet.add(str.charAt(i)) == false) {
duration = System.nanoTime() - startTime;
System.out.println("isPalindromeUsingSet - " + duration);
return false;
} else {
charSet.remove(str.charAt(i));
}
}
duration = System.nanoTime() - startTime;
System.out.println("isPalindromeUsingSet - " + duration);
return true;
}
}
我的两个问题是:
谢谢让我们看看
哈希集。添加
实现:
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
public V put(K key, V value) {
if (table == EMPTY_TABLE) {
inflateTable(threshold);
}
if (key == null)
return putForNullKey(value);
int hash = hash(key);
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(hash, key, value, i);
return null;
}
在内部,HashSet
使用HashMap
现在,让我们看一下<代码> HASMAP。放< /Cord>方法实现:
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
public V put(K key, V value) {
if (table == EMPTY_TABLE) {
inflateTable(threshold);
}
if (key == null)
return putForNullKey(value);
int hash = hash(key);
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(hash, key, value, i);
return null;
}
public V put(K键,V值){
如果(表==空表){
可充气(基本要求);
}
if(key==null)
返回putForNullKey(值);
int hash=散列(键);
int i=indexFor(散列,table.length);
for(条目e=表[i];e!=null;e=e.next){
对象k;
如果(e.hash==hash&((k=e.key)==key | | key.equals(k))){
V oldValue=e.value;
e、 价值=价值;
e、 记录存取(本);
返回旧值;
}
}
modCount++;
加法器(散列、键、值、i);
返回null;
}
我们可以注意到,从计算角度来说,将注册表放入地图是一项“艰巨的任务”。有一个:
- 散列算法
循环用于
- 添加条目方法执行
- 等等
现在,就可读性和可维护性而言,集合是最好的选择。让我们看看
哈希集。添加实现:
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
public V put(K key, V value) {
if (table == EMPTY_TABLE) {
inflateTable(threshold);
}
if (key == null)
return putForNullKey(value);
int hash = hash(key);
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(hash, key, value, i);
return null;
}
在内部,HashSet
使用HashMap
现在,让我们看一下<代码> HASMAP。放< /Cord>方法实现:
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
public V put(K key, V value) {
if (table == EMPTY_TABLE) {
inflateTable(threshold);
}
if (key == null)
return putForNullKey(value);
int hash = hash(key);
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(hash, key, value, i);
return null;
}
public V put(K键,V值){
如果(表==空表){
可充气(基本要求);
}
if(key==null)
返回putForNullKey(值);
int hash=散列(键);
int i=indexFor(散列,table.length);
for(条目e=表[i];e!=null;e=e.next){
对象k;
如果(e.hash==hash&((k=e.key)==key | | key.equals(k))){
V oldValue=e.value;
e、 价值=价值;
e、 记录存取(本);
返回旧值;
}
}
modCount++;
加法器(散列、键、值、i);
返回null;
}
我们可以注意到,从计算角度来说,将注册表放入地图是一项“艰巨的任务”。有一个:
- 散列算法
用于
循环
- 添加条目方法执行
- 等等
因此,如果执行时间是您的场景中的一个问题,那么手动循环是比Java集合更好的选择
现在,就可读性和可维护性而言,集合是最佳选择。在了解两个函数的比较错误后关闭 了解两个函数的比较错误后关闭 我无法理解你的第二种方法。第二个函数看起来不像第一个函数那样工作!您的第二个函数是错误的-它为AABB返回true。此外,您应该仔细阅读适当的基准测试,因为您在那里所做的工作由于多种原因无法提供有意义的度量(输入大小太小,花费的时间不够长,并且您在单个迭代中度量时间)。另外,这个问题的答案大致是“考虑任何给定方法的时间和空间复杂性(以及常量因素),而不是它是否使用Java集合”。不要使用Set。只有当元素在集合中不存在时,才会将其添加到集合中现在我知道了,所有输入都有重复字符,我不会使用集合,我看到它可以用于查找是否存在重复字符!!谢谢我无法理解你的第二种方法。第二个函数看起来不像第一个函数那样工作!您的第二个函数是错误的-它为AABB返回true。此外,您应该仔细阅读适当的基准测试,因为您在那里所做的工作由于多种原因无法提供有意义的度量(输入大小太小,花费的时间不够长,并且您在单个迭代中度量时间)。另外,这个问题的答案大致是“考虑任何给定方法的时间和空间复杂性(以及常量因素),而不是它是否使用Java集合”。不要使用Set。只有当元素在集合中不存在时,才会将其添加到集合中现在我知道了,所有输入都有重复字符,我不会使用集合,我看到它可以用于查找是否存在重复字符!!谢谢我同意可读性和可维护性,现在所有的系统都是超高速的。因为我习惯使用java集合,这在内存上有点贵。我同意可读性和可维护性,现在所有的系统都非常快。因为我习惯使用java集合,这在内存上有点贵。只是切换到使用低级循环进行编码优化。对。您没有将集的速度与其他东西进行比较,而是与不做任何事情进行比较。因此,集合
必须