Skip to content
This repository has been archived by the owner on Oct 21, 2019. It is now read-only.

leetcode第一题解题报告 #12

Open
BLF2 opened this issue Jul 18, 2019 · 0 comments
Open

leetcode第一题解题报告 #12

BLF2 opened this issue Jul 18, 2019 · 0 comments
Labels
algorithm LeetCode 或者 Codeforces 比赛

Comments

@BLF2
Copy link

BLF2 commented Jul 18, 2019

地址:https://leetcode-cn.com/problems/two-sum/
`public int[] twoSum(int[] nums, int target) {

    int[] re = new int[2];
    Map<Integer,Integer> map = new HashMap<>();
    for(int i = 0;i < nums.length;i++){
        map.put(nums[i],i + 1);
    }

    for(int i = 0;i < nums.length;i++){
        int sub = target - nums[i];
        if(map.containsKey(sub)){
            re[0] = i;
            re[1] = map.get(sub) - 1;
            if(re[0] == re[1]){
                continue;
            }
            break;
        }
    }
    return re;
}`

本题可以暴力求解,也可以用Map思想来解题,一开始有一种情形没考虑到,导致一次失误,
[3,2,4] 6
期望值是[1,2],而我的结果是:[0,0],还需要多多考虑一些特殊情况啊。

@BLF2 BLF2 added the algorithm LeetCode 或者 Codeforces 比赛 label Jul 18, 2019
@zwwhdls zwwhdls added this to the 2019 年 7 月第三周 milestone Jul 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
algorithm LeetCode 或者 Codeforces 比赛
Projects
None yet
Development

No branches or pull requests

2 participants