File tree 5 files changed +121
-0
lines changed
5 files changed +121
-0
lines changed Original file line number Diff line number Diff line change
1
+ - https://leetcode.com/problems/counting-bits/
2
+ - time complexity : O(n \* log n), logn 이 붙는 이유는 bit는 log를 따라 수가 결정되기 때문
3
+ - space complexity : O(n)
4
+ - https://algorithm.jonghoonpark.com/2024/04/23/leetcode-338
5
+
6
+ ``` java
7
+ public int [] countBits(int n) {
8
+ int result[] = new int [n + 1 ];
9
+ for (int i = 0 ; i <= n; i++ ) {
10
+ int num = i;
11
+ int count = 0 ;
12
+ while (num > 0 ) {
13
+ count += num & 1 ;
14
+ num = num >> 1 ;
15
+ }
16
+ result[i] = count;
17
+ }
18
+ return result;
19
+ }
20
+ ```
Original file line number Diff line number Diff line change
1
+ - https://leetcode.com/problems/group-anagrams/
2
+ - time complexity : O(n \* m log m), 여기서 m은 str 배열(strs)의 각 str의 평균이다.
3
+ - space complexity : O(n \* m)
4
+ - https://algorithm.jonghoonpark.com/2024/05/25/leetcode-49
5
+
6
+ ``` java
7
+ public List<List<String > > groupAnagrams(String [] strs) {
8
+ List<List<String > > result = new ArrayList<> ();
9
+ HashMap<String , Integer > map = new HashMap<> ();
10
+
11
+ for (String str: strs) {
12
+ char [] temp = str. toCharArray();
13
+ Arrays . sort(temp);
14
+ String sorted = String . valueOf(temp);
15
+ if (map. containsKey(sorted)) {
16
+ result. get(map. get(sorted)). add(str);
17
+ } else {
18
+ int newIndex = result. size();
19
+ List<String > newArrayList = new ArrayList<> ();
20
+ result. add(newArrayList);
21
+ newArrayList. add(str);
22
+ map. put(sorted, newIndex);
23
+ }
24
+ }
25
+
26
+ return result;
27
+ }
28
+ ```
29
+
30
+ ## TC, SC
31
+
32
+ 시간 복잡도는 O(n \* m log m)이고, 공간 복잡도는 O(n \* m)이다.
33
+ 여기서 m은 str 배열(strs)의 각 str의 평균이다.
Original file line number Diff line number Diff line change
1
+ - https://leetcode.com/problems/missing-number/
2
+ - time complexity : O(n)
3
+ - space complexity : O (n)
4
+ - https://algorithm.jonghoonpark.com/2024/05/25/leetcode-268
5
+
6
+ ``` java
7
+ public int missingNumber(int [] nums) {
8
+ int [] counts = new int [nums. length + 1 ];
9
+
10
+ for (int num : nums) {
11
+ counts[num] = 1 ;
12
+ }
13
+
14
+ for (int i = 0 ; i < counts. length; i ++ ){
15
+ if (counts[i] == 0 ) {
16
+ return i;
17
+ }
18
+ }
19
+
20
+ return - 1 ;
21
+ }
22
+ ```
23
+
24
+ 등차수열로 푸는 방법도 있는 재밌는 문제.
25
+
26
+ ``` java
27
+ public int missingNumber(int [] arr) {
28
+ int sum = 0 ;
29
+ int max = (arr. length * (arr. length + 1 )) / 2 ;
30
+ for (int i = 0 ; i < arr. length; i++ ) {
31
+ sum += arr[i];
32
+ }
33
+ return max - sum;
34
+ }
35
+ ```
Original file line number Diff line number Diff line change
1
+ - https://leetcode.com/problems/number-of-1-bits/
2
+ - time complexity : O(logn)
3
+ - space complexity : O(1)
4
+ - https://algorithm.jonghoonpark.com/2024/02/20/leetcode-191
5
+
6
+ ``` java
7
+ public int hammingWeight(int n) {
8
+ int count = 0 ;
9
+ while (n != 0 ) {
10
+ count += n % 2 ;
11
+ n = n >> 1 ;
12
+ }
13
+ return count;
14
+ }
15
+ ```
Original file line number Diff line number Diff line change
1
+ - https://leetcode.com/problems/reverse-bits/
2
+ - time complexity : O(1)
3
+ - space complexity : O(1)
4
+ - https://algorithm.jonghoonpark.com/2024/04/23/leetcode-190
5
+
6
+ ``` java
7
+ public class Solution {
8
+ public int reverseBits (int n ) {
9
+ int ans = 0 ;
10
+ for (int i = 0 ; i < 32 ; i++ ) {
11
+ ans << = 1 ;
12
+ ans |= (n & 1 );
13
+ n >> = 1 ;
14
+ }
15
+ return ans;
16
+ }
17
+ }
18
+ ```
You can’t perform that action at this time.
0 commit comments