-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac9036a
commit a9c22f6
Showing
2 changed files
with
79 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,54 @@ | ||
// ํฌํฌ์ธํฐ ๋ฐฉ์์ผ๋ก ๊ตฌํ ๊ธฐ๋ณธ ์๊ฐ๋ณต์ก๋๊ฐ O(2^n)์ด๋ฏ๋ก ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ๋ O(NlogN)์ ๋ง์๊ป ์ฌ์ฉํด๋ ๋๋ค. | ||
public List<List<Integer>> threeSum(int[] nums) { | ||
List<List<Integer>> result = new ArrayList<>(); | ||
Arrays.sort(nums); | ||
for(int i =0; i< nums.length-2;i++) { | ||
if (i > 0 && nums[i] == nums[i - 1]) continue; | ||
int left = i + 1, right = nums.length - 1; | ||
while (left < right) { | ||
int sum = nums[i] + nums[left] + nums[right]; | ||
|
||
if (sum == 0) { | ||
result.add(Arrays.asList(nums[i], nums[left], nums[right])); | ||
|
||
while (left < right && nums[left] == nums[left + 1]) left++; | ||
while (left < right && nums[right] == nums[right - 1]) right--; | ||
|
||
left++; | ||
right--; | ||
} else if (sum < 0) { | ||
left++; | ||
} else { | ||
right--; | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
|
||
// dfs ํ์ด๋ฅผ ์๊ฐํ์ผ๋ dfs ๋ฐฉ์์ ๋ฌด์กฐ๊ฑด O(2^n)์ ๊ฒฐ๊ณผ๊ฐ ๋์จ๋ค. | ||
// ํ์ ์์ ์ค๋ต | ||
public class Solution { | ||
public List<List<Integer>> threeSum(int[] nums) { | ||
Set<List<Integer>> answerSet = new HashSet<>(); | ||
nums = Arrays.stream(nums).sorted().toArray(); | ||
dfs(nums, 0, new ArrayList<>(), answerSet); | ||
return new ArrayList<>(answerSet); | ||
} | ||
|
||
public void dfs(int[] nums, int index, List<Integer> temp, Set<List<Integer>> answerSet) { | ||
if (temp.size() == 3) { | ||
int result = 0; | ||
for (int element : temp) result += element; | ||
if (result == 0) answerSet.add(new ArrayList<>(temp)); | ||
return; | ||
} | ||
|
||
for (int i = index; i < nums.length; i++) { | ||
temp.add(nums[i]); | ||
dfs(nums, i + 1, temp, answerSet); | ||
temp.remove(temp.size() - 1); | ||
} | ||
} | ||
} |
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,25 @@ | ||
// ๋ฉ์ธ ๋ก์ง์ ๋ง์๋๋ฐ, 0๋ฒ์งธ 1๋ฒ์งธ ๋๋ฌธ์ ๋น๋น ๋์๋ ๋ฌธ์ | ||
// dp[0] = 1์ด ์์ด์ผ ๋์๋ฆฌ ์ ๋์ฝ๋ฉ์ ์ด๊ธฐ๊ฐ์ด ์ ์์ ์ผ๋ก ๊ณ์ฐ๋๋ค. | ||
class Solution { | ||
public int numDecodings(String s) { | ||
if (s == null || s.length() == 0) return 0; | ||
|
||
int n = s.length(); | ||
int[] dp = new int[n + 1]; | ||
|
||
dp[0] = 1; | ||
dp[1] = s.charAt(0) != '0' ? 1 : 0; | ||
|
||
for (int i = 2; i <= n; i++) { | ||
if (s.charAt(i - 1) != '0') { | ||
dp[i] += dp[i - 1]; | ||
} | ||
int twoDigit = Integer.parseInt(s.substring(i - 2, i)); | ||
if (twoDigit >= 10 && twoDigit <= 26) { | ||
dp[i] += dp[i - 2]; | ||
} | ||
} | ||
|
||
return dp[n]; | ||
} | ||
} |