Two Sum III - Data structure design
2016-06-08 06:14
330 查看
丢人!!!即便我们是在公司偷闲。。。。。。
是否要处理重复数字?如何处理?????
怎么写的这么痛苦
public class TwoSum {
private Map<Integer/*value*/, Integer/*number*/> map = new HashMap<>();
public void add(int number) {
if (map.containsKey(number)) {
map.put(number, map.get(number) + 1);
} else {
map.put(number, 1);
}
}
// Find if there exists any pair of numbers which sum is equal to the value.
public boolean find(int value) {
for (int valueKey: map.keySet()) {
int target = value - valueKey;
if (target == valueKey && map.get(target) > 1) {
return true;
} else if (target != valueKey && map.containsKey(target)) {
return true;
}
}
return false;
}
// // Add the number to an internal data structure.
// public void add(int number) {
// //list.add(number);
// set.add(number);
// }
//
// // Find if there exists any pair of numbers which sum is equal to the value.
// public boolean find(int value) {
// Iterator<Integer> i = set.iterator();
// while (i.hasNext()) {
// int s = i.next();
// if (set.contains(value - s)) {
// if (set.contains(s))
// return false;
// return true;
// }
// }
// //Set<Integer/*Value*/> set = new HashSet<>();
//// for (int i = 0; i < list.size(); i++) {
//// if (set.contains(list.get(i))) {
//// return true;
//// } else {
//// set.add(value - list.get(i));
//// }
//// }
// return false;
// }
//
// //private List<Integer> list = null;
// private Set<Integer> set = null;
//
// public TwoSum() {
// //list = new LinkedList<>();
// set = new HashSet<>();
// }
}
// Your TwoSum object will be instantiated and called as such:
// TwoSum twoSum = new TwoSum();
// twoSum.add(number);
// twoSum.find(value);
是否要处理重复数字?如何处理?????
怎么写的这么痛苦
public class TwoSum {
private Map<Integer/*value*/, Integer/*number*/> map = new HashMap<>();
public void add(int number) {
if (map.containsKey(number)) {
map.put(number, map.get(number) + 1);
} else {
map.put(number, 1);
}
}
// Find if there exists any pair of numbers which sum is equal to the value.
public boolean find(int value) {
for (int valueKey: map.keySet()) {
int target = value - valueKey;
if (target == valueKey && map.get(target) > 1) {
return true;
} else if (target != valueKey && map.containsKey(target)) {
return true;
}
}
return false;
}
// // Add the number to an internal data structure.
// public void add(int number) {
// //list.add(number);
// set.add(number);
// }
//
// // Find if there exists any pair of numbers which sum is equal to the value.
// public boolean find(int value) {
// Iterator<Integer> i = set.iterator();
// while (i.hasNext()) {
// int s = i.next();
// if (set.contains(value - s)) {
// if (set.contains(s))
// return false;
// return true;
// }
// }
// //Set<Integer/*Value*/> set = new HashSet<>();
//// for (int i = 0; i < list.size(); i++) {
//// if (set.contains(list.get(i))) {
//// return true;
//// } else {
//// set.add(value - list.get(i));
//// }
//// }
// return false;
// }
//
// //private List<Integer> list = null;
// private Set<Integer> set = null;
//
// public TwoSum() {
// //list = new LinkedList<>();
// set = new HashSet<>();
// }
}
// Your TwoSum object will be instantiated and called as such:
// TwoSum twoSum = new TwoSum();
// twoSum.add(number);
// twoSum.find(value);
相关文章推荐
- 专技天下河北省2016年专业技术人员继续教育公需科目题库答案(答题器)
- linux的库文件为何不能单独执行
- MemSQL 取代 HDFS 与 Spark 结合,性能大幅提升
- 要引用这几个才有GetOwinContext与GetAutofacLifetimeScope
- Isomorphic Strings
- 求Sn=a+aa+aaa+aaaa+……+aaa…aaa(n个a)
- Autofac IContainer 测试
- 乡土中国笔记
- linux设置密码规则
- UBUNTU下LAMP的配置
- linux修改密码
- C# 按指定数量从前面或者后面删除字符串
- SDUT 3554 无尽走廊 (动态规划) -- 解题报告
- 使用pscp实现Windows 和 Linux服务器间远程拷贝文件
- 勾股定理一日一证连载17
- Autofac.Integration.Owin
- Autofac.Integration.Mvc.Owin分析
- 框架 day48 BOS项目练习2(base dao/action,登陆,消息提示框,菜单按钮,修改密码,登陆拦截器)
- leecode_236 Lowest Common Ancestor of a Binary Tree
- Autofac.Integration.Web分析