File tree Expand file tree Collapse file tree 1 file changed +22
-6
lines changed
ConatainsDuplicate/ConatainsDuplicate Expand file tree Collapse file tree 1 file changed +22
-6
lines changed Original file line number Diff line number Diff line change @@ -29,15 +29,31 @@ static void Main(string[] args)
29
29
30
30
static bool ContainsDupliacate ( int [ ] nums )
31
31
{
32
- /*方法一:采用Dictionary 实现*/
33
- Dictionary < int , int > dictionary = new Dictionary < int , int > ( ) ;
34
- foreach ( int n in nums )
32
+ ///*方法一:采用Dictionary 实现*/
33
+ //Dictionary<int, int> dictionary = new Dictionary<int, int>();
34
+ //foreach (int n in nums)
35
+ //{
36
+ // if (!dictionary.ContainsKey(n)) dictionary.Add(n, 1);
37
+ // else return true;
38
+ //}
39
+ //return false;
40
+
41
+ /*方法二:采用数组实现(位操作)*/
42
+ // 15000 暂定
43
+ byte [ ] table = new byte [ 15000 ] ;
44
+ foreach ( var item in nums )
35
45
{
36
- if ( ! dictionary . ContainsKey ( n ) ) dictionary . Add ( n , 1 ) ;
37
- else return true ;
46
+ // 为什么除以8 ? 考虑到 byte 是8位
47
+ int i = item / 8 ;
48
+ int j = item % 8 ;
49
+ byte check = ( byte ) ( 1 << j ) ;
50
+ if ( ( table [ i ] & check ) != 0 )
51
+ {
52
+ return true ;
53
+ }
54
+ table [ i ] |= check ;
38
55
}
39
56
return false ;
40
-
41
57
}
42
58
}
43
59
}
You can’t perform that action at this time.
0 commit comments