题解 | #两数之和#

两数之和

https://www.nowcoder.com/practice/20ef0972485e41019e39543e8e895b7f

using System;
using System.Collections.Generic;
using System.Collections;

class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param numbers int整型一维数组 
     * @param target int整型 
     * @return int整型一维数组
     */
    public List<int> twoSum (List<int> numbers, int target) {
        List<int> res = new List<int>();
        Hashtable hash = new Hashtable();
        for(int i = 0; i < numbers.Count; i++){
            if(hash.ContainsKey(target - numbers[i])){
                res.Add(Convert.ToInt32(hash[target - numbers[i]]));
                res.Add(i + 1);
                return res;
            }
            else {
                if(!hash.ContainsKey(numbers[i])){
                    hash.Add(numbers[i], i + 1);
                }
            }
        }
        return res;
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-02 18:35
简历上把1个月实习写成了3个月,会进行背调吗?
码农索隆:一个月有一个月的实习经历,三个月有三个月的实习经历
点赞 评论 收藏
分享
VirtualBoo...:都去逗他了?
点赞 评论 收藏
分享
自学java狠狠赚一...:骗你点star的,港卵公司,记得把star收回去
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务