-
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 #828 from GotPrgmer/main
[Gotprgmer] Week4
- Loading branch information
Showing
4 changed files
with
122 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,37 @@ | ||
// ์์ ํ์์ ํตํด ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๊ตฌํ๊ธฐ ์ํด ๋ ธ๋ ฅํ์์ง๋ง ์๊ฐ์ด๊ณผ๊ฐ ๋ฐ์ํ์์ต๋๋ค. | ||
// dfs๋ฅผ ํตํด ํ์ดํ๋ ค๊ณ ํ์ง๋ง O(2^N)์ ์๊ฐ๋ณต์ก๋๋ก ์ธํด ์๊ฐ์ด๊ณผ๊ฐ ๋ฐ์ํ์์ต๋๋ค. | ||
// ์ดํ dp๋ก ํ์ด๋ฅผ ์์ํ์๊ณ ์ด๋ ต์ง ์๊ฒ ํ์ดํ์์ต๋๋ค. | ||
// dp[i] = dp[i-1] + dp[i-2]๋ก ํ์ดํ์์ต๋๋ค. | ||
// ์ด๋ i๋ฒ์งธ ๋ฌธ์์ด์ 1์๋ฆฌ๋ก ์ทจ๊ธํ ์ง 2์๋ฆฌ๋ก ์ทจ๊ธํ ์ง์ ๋ฐ๋ผ ๊ฒฝ์ฐ์ ์๊ฐ ๋ฌ๋ผ์ง๋๋ค. | ||
// 1์๋ฆฌ๋ก ์ทจ๊ธํ ๊ฒฝ์ฐ 1~9๊น์ง ๊ฐ๋ฅํ๊ณ | ||
// 2์๋ฆฌ๋ก ์ทจ๊ธํ ๊ฒฝ์ฐ 10~26๊น์ง ๊ฐ๋ฅํฉ๋๋ค. | ||
|
||
// ์๊ฐ๋ณต์ก๋ : O(N) | ||
// ๊ณต๊ฐ๋ณต์ก๋ : O(N) | ||
class SolutionGotprgmer { | ||
public int numDecodings(String s) { | ||
// ์์ธ ์ฒ๋ฆฌ: ๋ฌธ์์ด์ด "0"์ผ๋ก ์์ํ๊ฑฐ๋ ๋น ๋ฌธ์์ด์ด๋ฉด | ||
if (s == null || s.length() == 0 || s.charAt(0) == '0') { | ||
return 0; | ||
} | ||
int[] dp = new int[s.length()+1]; | ||
dp[0] = 1; | ||
for(int i=0;i<s.length();i++){ | ||
int ith = s.charAt(i)-'0'; | ||
if(ith != 0){ | ||
dp[i+1] = dp[i]; | ||
} | ||
if(i>0){ | ||
String twoDigitStr = s.substring(i-1,i+1); | ||
int twoDigitNum = Integer.valueOf(twoDigitStr); | ||
if(twoDigitNum>=10 && twoDigitNum <27){ | ||
dp[i+1] += dp[i-1]; | ||
} | ||
} | ||
|
||
} | ||
return dp[s.length()]; | ||
} | ||
|
||
|
||
} |
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,18 @@ | ||
// ๋จ์ํ๊ฒ ์ ๋ ฌํด์ ์ผ์นํ์ง ์์ผ๋ฉด ์ถ๋ ฅํ๊ณ ๋ฆฌ์คํธ๋ฅผ ๋ฒ์ด๋๋ฉด ๊ทธ๋๋ก checkNum์ ์ถ๋ ฅํ๋ ๋ฐฉ์ | ||
// ์๊ฐ๋ณต์ก๋ : O(NlogN) | ||
// ๊ณต๊ฐ๋ณต์ก๋ : O(1) | ||
|
||
class SolutionGotprgmer { | ||
public int missingNumber(int[] nums) { | ||
Arrays.sort(nums); | ||
int checkNum = 0; | ||
for(int i=0;i<nums.length;i++){ | ||
if(nums[i] != checkNum){ | ||
return checkNum; | ||
} | ||
checkNum += 1; | ||
} | ||
return checkNum; | ||
|
||
} | ||
} |
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,13 @@ | ||
// ์ฒ์ ๋ฌธ์ ๋ฅผ ๋ดค์๋๋ ์ดํด๊ฐ ์ ๊ฐ์ง ์์์ง๋ง, | ||
// ๋นํธ๋ค์ ๋ค์ง์ผ๋ผ๋ ์ค๋ช ์ผ๋ก ํ์๋ ๊ฒ ๊ฐ๋ค. | ||
// Integer.reverse() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ํ์๋ค. | ||
// ์งํผํฐ์ ๋์์ผ๋ก Integer.reverse()๋ฅผ ์ฌ์ฉํ๋ผ๋ ํํธ๋ฅผ ์ป์๋ค. | ||
// ์ฐพ์๋ณด๋ reverse(N)๋ N์ 2์ ๋ณด์ ๋นํธ๋ก ๋ฐ๊พธ๊ณ ๊ทธ๊ฒ์ ๋ค์ง๋ ๋ฐฉ์์ด์๋ค. | ||
// ์๊ฐ๋ณต์ก๋ : O(1) -> Integer๊ฐ 32๋นํธ ๊ณ ์ ์ด๋ผ์ O(1) | ||
// ๊ณต๊ฐ๋ณต์ก๋ : O(1) -> 32๋นํธ ๊ณ ์ | ||
public class Solution { | ||
// you need treat n as an unsigned value | ||
public int reverseBits(int n) { | ||
return Integer.reverse(n); | ||
} | ||
} |
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,54 @@ | ||
// ๋ฐฐ์ด์ ์ ๋ ฌํ์ฌ ํฌํฌ์ธํฐ๋ก ์ ๊ทผํ์ฌ ํ์์ต๋๋ค. | ||
// ์ ๋ ฌ๋ ๋ฐฐ์ด์ ์ธ๋ฑ์ค๋ฅผ ์ฐพ๊ธฐ ์ํด indexOf ๋ฉ์๋๋ฅผ ๋ง๋ค์ด์ ์ฌ์ฉํ์ต๋๋ค. | ||
|
||
// ์๊ฐ๋ณต์ก๋ : O(NlogN) -> ์ ๋ ฌ์ ์ํด O(NlogN) + ํฌํฌ์ธํฐ๋ก O(N)์ด๋ฏ๋ก O(NlogN) | ||
// ๊ณต๊ฐ๋ณต์ก๋ : O(N) -> ์ ๋ ฌ์ ์ํด ๋ณต์ฌํ ๋ฐฐ์ด์ด ํ์ํ๋ฏ๋ก O(N) | ||
class SolutionGotprgmer { | ||
public int[] twoSum(int[] nums, int target) { | ||
int[] original = new int[nums.length]; | ||
|
||
for(int i=0;i<nums.length;i++){ | ||
original[i] = nums[i]; | ||
} | ||
Arrays.sort(nums); | ||
|
||
int l = 0; | ||
int r = nums.length-1; | ||
while (l<r){ | ||
int lV = nums[l]; | ||
int rV = nums[r]; | ||
int total = lV + rV; | ||
if(total > target){ | ||
r -= 1; | ||
} | ||
else if(total < target){ | ||
l += 1; | ||
} | ||
else{ | ||
int[] ans = indexOf(lV,rV,original); | ||
l = ans[0]; | ||
r = ans[1]; | ||
break; | ||
} | ||
} | ||
return new int[] {l,r}; | ||
} | ||
|
||
public int[] indexOf(int l,int r, int[] nums){ | ||
int lIdx = -1; | ||
int rIdx = -1; | ||
for(int i = 0;i<nums.length;i++){ | ||
if(nums[i] == l){ | ||
lIdx = i; | ||
break; | ||
} | ||
} | ||
for(int i = nums.length-1;i>-1;i--){ | ||
if(nums[i] == r){ | ||
rIdx = i; | ||
break; | ||
} | ||
} | ||
return new int[] {lIdx,rIdx}; | ||
} | ||
} |