-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #775 from dalpang81/main
[WonYeong] Week3
- Loading branch information
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* 시간복잡도: O(n) | ||
* 공간복잡도: O(1) | ||
* */ | ||
class Solution { | ||
public int[] productExceptSelf(int[] nums) { | ||
int n = nums.length; | ||
int[] answer = new int[n]; | ||
|
||
answer[0] = 1; | ||
for (int i = 1; i < n; i++) { | ||
answer[i] = answer[i - 1] * nums[i - 1]; | ||
} | ||
|
||
int suffixProduct = 1; | ||
for (int i = n - 1; i >= 0; i--) { | ||
answer[i] *= suffixProduct; | ||
suffixProduct *= nums[i]; | ||
} | ||
|
||
return answer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* 시간복잡도: O(1) | ||
* 공간복잡도: O(1) | ||
* */ | ||
public class Solution { | ||
// you need treat n as an unsigned value | ||
public int reverseBits(int n) { | ||
int result = 0; | ||
for (int i = 0; i < 32; i++) { | ||
result <<= 1; | ||
result |= (n & 1); | ||
n >>= 1; | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* 시간복잡도 : O(n) | ||
* 공간복잡도 : O(n) | ||
* */ | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
class Solution { | ||
public int[] twoSum(int[] nums, int target) { | ||
Map<Integer, Integer> map = new HashMap<>(); | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
int complement = target - nums[i]; | ||
|
||
if (map.containsKey(complement)) { | ||
return new int[] { map.get(complement), i }; | ||
} | ||
map.put(nums[i], i); | ||
} | ||
return null; | ||
} | ||
} |