leetcode每日一题,两数之和
2018-12-17 17:23
113 查看
class Solution {
public int[] twoSum(int[] nums, int target) {
int i=0;int j=0;
if(nums!=null&&nums.length>1){
for(;i<nums.length-1;i++){
for(j=i+1;j<nums.length;j++){
if(target==(nums[i]+nums[j])){
return new int[]{i,j}; } } } } return null; }
}
看了一下后面的【官方题解】,有三种方法,我这种属于第一种,暴力破解
O(n^2)。
其他两种都是通过hashmap做的,我想了想,应该再加一个数组也能过达到他那个O(n),不行,还是O(n^2);
但是我看了下hashtable的源码,也是for循环遍历出来的,你用了java自带的工具包,这也没什么里子,纯面子上好看呀。
对hashmap的介绍:https://www.cnblogs.com/skywang12345/p/3310835.html#b2
相关文章推荐
- LeetCode每日一题 009 两数相加
- LeetCode每日一题 008 两数之和
- (LeetCode每日一刷01)两数之和
- Leetcode每日一道 -- 两数相加
- Leetcode每日一道 -- 两数之和
- leetcode 每日一题 101. Symmetric Tree
- LeetCode1. python实现:两数之和问题☆
- 从零开始刷LeetCode:[2] 两数相加
- LeetCode初级算法数组9 两数之和
- Leetcode——两数之和(twoSum)、三数之和(threeSum)——Python
- 从零打卡leetcode之day 2---两数相加
- leetcode:206 Reverse Linked List-每日编程第十四题
- [leetcode]两数之和
- LeetCode - 167. 两数之和 II - 输入有序数组
- LeetCode1. 两数之和(Java)
- leetcode 两数之和
- 【leetcode每日一题】138.Copy List with Random Pointer
- leetcode 每日一题 102. Binary Tree Level Order Traversal
- LeetCode 1. 两数之和
- LeetCode每日一题(2018-01-08)