LeetCode 0001
来源:互联网 发布:淘宝视频教程全集 编辑:程序博客网 时间:2024/06/13 16:29
原题链接
我的解法:
一开始是想着排序之后双指针,后来发现题目要求的是存储下标。就想着搞个struct存下标和内容,然后根据内容排序,再存下标。后来没有这么做,是因为觉得有点烦。直接暴力求解了,显然是可以的,只是效率有点不行。需要吐槽一句,这里没有给出范围,就很难受。
最佳答案的解法是搜一遍的hash table,就是边加边找有没有符合答案的
public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { int complement = target - nums[i]; if (map.containsKey(complement)) { return new int[] { map.get(complement), i }; } map.put(nums[i], i); } throw new IllegalArgumentException("No two sum solution");}
我所说的最佳,是单从时间来看的。
阅读全文
0 0
- LeetCode 0001
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode:
- 层次遍历引入队列解决,队列
- Python-Day02-列表2
- 综合练习-介绍家乡
- 单源最短路径--Bellman-Ford算法及SPFA
- 安装cocoapods
- LeetCode 0001
- Kotlin极简教程:第10章 Kotlin与Java互操作
- 位运算实现二进制转换 C语言
- 【福利】日语学习资料大集合!6G资源!
- 重庆“背篓夫妻”相伴29年 夫妻二人互为腿眼
- 浅谈苏联军工水平
- python里预先编译正则表达式提高效率
- namenode正处于safemode状态,怎么处理?
- h5开发通用sass基础配置文件