第一个leetcode题目: two sum

第一个leetcode,大概意思:在一个数组里,任意找两个数字,让它们的和等于一个给定目标值。要求:1、输出这两个数在数组的下标 2、保证第一个数的下标要小于第二个数的下标

做这个题,还是有一点点欣慰的地方就是:读完题,就有思路,而且能顺利编出来。(虽然因为那个小小的 return b 修改了好几次)

public class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] b = new int[2];
        for(int i=0;i<nums.length;i++){
            for(int j=(i+1);j<nums.length;j++){
              if((nums[i]+nums[j])==target){
                   b[0]=i+1;
                   b[1]=j+1;
              }

            }

        }
       return b;
    }
}

  不多说,直接上 Submission Details。。。。看到这运行时间,我也是醉了(也第一次看到了Java与众语言的差距。。。)

不过在讨论版里学习了O(n)的方法:思路是先往map里面存一个numbers[0]的元素,判断target-numbers[0] 的结果存在于map中:如果不在,往map存numbers[1],判断target-numbers[1] 的结果是否存在于map中(此时target-numbers[1]的查找值只有numbers[0])。。。。依次类推,如果存在,则说明刚刚好遇到了结果,直接输出index1和(i+1),因为(i+1)总是要大于index1的~

另外还要纠正原来的思维定式:map<key,value>中,总以为key是要为value服务的(value比key重要),其实不然,key和value处在同等地位!以后还需要勤思考啊~!

public int[] twoSum(int[] numbers, int target) {
        Map<Integer, Integer> map = new HashMap<Integer,Integer>();
        for(int i = 0; i < numbers.length; i++) {
            Integer index1 = map.get(target-numbers[i]);
            if (index1 != null) {                                //第一次进循环,可以从这里先看
                return new int[]{index1,i+1};
            }
            map.put(numbers[i], i+1);
        }
        return null;
}

  

时间: 2024-11-04 18:04:03

第一个leetcode题目: two sum的相关文章

leetcode题目:Sum Root to Leaf Numbers和Longest Consecutive Sequence

题目一: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 /

LeetCode:Range Sum Query - Immutable - 数组指定区间内的元素和

1.题目名称 Range Sum Query(数组指定区间内的元素和) 2.题目地址 https://leetcode.com/problems/range-sum-query-immutable/ 3.题目内容 英文:Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. 中文:给定一个数组nums,求出索引i和j之间元素的和,i一定是小于或等于j

[leetCode][013] Two Sum 2

题目: Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where inde

[LeetCode] 039. Combination Sum (Medium) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 039. Combination Sum (Medium) 链接: 题目:https://leetcode.com/problems/combination-sum/ 代码(github):https://github.com/illuz/leetcode 题意: 给出一些正整数集合,以及一个目标数,从集合中选择一

[leetcode] 040. Combination Sum II (Medium) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 040. Combination Sum II (Medium) 链接: 题目:https://leetcode.com/problems/combination-sum-ii/ 代码(github):https://github.com/illuz/leetcode 题意: 跟 039 一样(给出一些正整数集合,

leetcode题目思路以及部分解答(二)

又刷了30题了,这速度还不错.因为还有别的东西要复习,所以进度并不快.感觉还是能学到很多新东西的.早知道这个就不用去其他地方刷了.这个难度不高,还可以知道哪些情况没考虑.比其他OJ那种封闭式的好多了.还是进入正题吧. 1.Rotate Image 这个做过两三次了,但每次还是得重新开始推导..这次又推导了很久..不过好在做过,代码也写得比较简洁. 主要思路就是第一层循环按层次深入.第二层把旋转后对应替代的4个位置循环更新.swap就是用来更新用的.做完发现讨论里的最高票代码就是我这样子= =  

[array] leetcode - 39. Combination Sum - Medium

leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from

LeetCode --- 1. Two Sum

题目链接:Two Sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Plea

LeetCode:Path Sum - 树的根节点到叶节点的数字之和

1.题目名称 Path Sum(树的根节点到叶节点的数字之和) 2.题目地址 https://leetcode.com/problems/path-sum/ 3.题目内容 英文:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. 中文:给定一颗二叉树,如