LeetCode -- Single Number

来源:互联网 发布:手淘宝网 编辑:程序博客网 时间:2024/06/05 19:03
题目描述:


Given an array of integers, every element appears twice except for one. Find that single one.


Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?


在一个数列中找到没有重复的那一个。


思路:哈希表统计次数




实现代码:

public class Solution {    public int SingleNumber(int[] nums) {        var hash = new Dictionary<int,int>();        for(var i = 0;i < nums.Length; i++){            if(!hash.ContainsKey(nums[i])){                hash.Add(nums[i],1);            }            else{                hash[nums[i]]++;            }        }                foreach(var k in hash.Keys){            if(hash[k] == 1){                return k;            }        }                return -1;    }}


1 0
原创粉丝点击