Skip to content

Commit cfefa8e

Browse files
committed
issue #11 169-Done
1 parent 1d505f8 commit cfefa8e

2 files changed

Lines changed: 40 additions & 4 deletions

File tree

‎src/leetcodeEasyLevel/_0136SingleNumber.java‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
public class _0136SingleNumber {
88
public static int singleNumber(int[] nums) {
99
//sol1 => Runtime 1 ms Memory 47.9 MB
10-
// 비트 연산자(^) : 대응되는 비트가 서로 같으면 0을, 다르면 1을 반환함. (비트 XOR 연산)
10+
// 비트 연산자(^) : 대응되는 비트가 서로 같으면 0을, 다르면 1을 반환한 뒤 2진수에서 10진수로 변환하여 출력 (비트 XOR 연산)
1111
// http://www.tcpschool.com/c/c_operator_bitwise
12-
/*
12+
// https://www.softwaretestinghelp.com/logical-operators-java/
13+
/*
1314
int val=0;
1415
for(int i=0;i<nums.length;i++){
1516
val^=nums[i];
1617
}
1718
return val;
18-
*/
19+
*/
1920

2021
//sol2 => Runtime 10 ms Memory 40 MB
2122
// HashSet : contains()을 통해 있으면 remove, 없으면 add해서 남은
@@ -30,11 +31,12 @@ public static int singleNumber(int[] nums) {
3031
}
3132
}
3233
return box.stream().findFirst().get();
34+
3335
}
3436

3537
public static void main(String[] args) {
3638
int[] nums =// {2, 2, 1};
37-
//{4,1,2,1,2};
39+
//{4,1,2,1,2}; //=>4
3840
//{1};
3941
//{-1,-1,-2}; //=> -2
4042
//{1,3,1,-1,3};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package leetcodeEasyLevel;
2+
3+
import java.util.Arrays;
4+
5+
public class _0169MajorityElement {
6+
public static int majorityElement(int[] nums) {
7+
//sol1 : for each Runtime 2261 ms Memory 54.4 MB
8+
/*
9+
int majorityCount = nums.length/2;
10+
for(int n : nums){
11+
int count = 0;
12+
for(int elem : nums){
13+
if(elem == n){
14+
count +=1;
15+
}
16+
}
17+
if(count > majorityCount){
18+
return n;
19+
}
20+
}
21+
return -1;
22+
*/
23+
24+
//sol2 : Arrays
25+
Arrays.sort(nums);
26+
return nums[nums.length / 2];
27+
}
28+
29+
public static void main(String[] args) {
30+
int[] nums = {3,2,3}; //3
31+
// {2,2,1,1,1,2,2}; //2
32+
System.out.println(majorityElement(nums));
33+
}
34+
}

0 commit comments

Comments
 (0)