answer:
public boolean containsDuplicate(int[] nums) { if (nums.length <= 1) return false; Set2. 389找不同set = new HashSet<>(); for (int n : nums) { if (!set.add(n)) return true; } return false; }
answer:
char类型的运算以ASCII码形式(即数字);
b-a是一个数字,需转回成char类型;
public char findTheDifference(String s, String t) { int a = 0; int b = 0; for (char c : s.toCharArray()) { a += c; } for (char c : t.toCharArray()) { b += c; } return (char)(b-a); }3. 705设计哈希集合
answer:
用list实现的,性能拉跨!
private Listlist; public MyHashSet() { list = new ArrayList<>(); } public void add(int key) { if (list.contains(key)) return; list.add(key); } public void remove(int key) { for (int i =0; i < list.size(); i++) { if (list.get(i) == key) { list.remove(i); // 重点!!!! 移除的是索引i; break; } } } public boolean contains(int key) { return list.contains(key); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)