Skip to content

Commit 3ec6435

Browse files
authored
Merge pull request AlgoStudyGroup#55 from sylviadream/sylviadream-patch-1
leetcode 0605
2 parents b2b95cc + 86fbc22 commit 3ec6435

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class Solution {
2+
public int FindComplement(int num) {
3+
string tempStr = Convert.ToString(num,2);
4+
int help = (int)Math.Pow(2,tempStr.Length) - 1;
5+
return num ^ help;
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// I use dictionary in this solution but this is not the best solution
2+
3+
public class Solution {
4+
public int MajorityElement(int[] nums) {
5+
Dictionary<int, int> dicElement = new Dictionary<int, int>();
6+
int i = 0;
7+
int n = 0;
8+
while (i<nums.Length)
9+
{
10+
if (dicElement.ContainsKey(nums[i]))
11+
{
12+
dicElement[nums[i]]++;
13+
if (dicElement[nums[i]] >= (nums.Length+1)/2)
14+
{
15+
return nums[i];
16+
}
17+
}
18+
else
19+
{
20+
dicElement.Add(nums[i],1);
21+
}
22+
23+
i++;
24+
25+
}
26+
return nums[i-1];
27+
}
28+
}

0 commit comments

Comments
 (0)