-
-
Notifications
You must be signed in to change notification settings - Fork 195
[PDKhan] WEEK 02 solutions #1208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
372dc63
valid anagram solution
PDKhan c2aa005
climbing stairs solution
PDKhan ebd571e
product of array except self solution
PDKhan 9c1260c
3sum solution
PDKhan 6297829
validate binary search tree solution
PDKhan 74b31dc
add space
PDKhan 2e8dbf7
add new line
PDKhan 3ab1cc5
merge left tree result and right tree result
PDKhan cc44155
remove divide operation
PDKhan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,36 @@ | ||
class Solution { | ||
public: | ||
vector<vector<int>> threeSum(vector<int>& nums) { | ||
vector<vector<int>> result; | ||
sort(nums.begin(), nums.end()); | ||
|
||
for(int i = 0; i < nums.size(); i++){ | ||
if(i > 0 && nums[i] == nums[i-1]) | ||
continue; | ||
|
||
int left = i + 1; | ||
int right = nums.size() - 1; | ||
|
||
while(left < right){ | ||
int sum = nums[i] + nums[left] + nums[right]; | ||
|
||
if(sum == 0){ | ||
result.push_back({nums[i], nums[left], nums[right]}); | ||
left++; | ||
right--; | ||
|
||
while(left < right && nums[left] == nums[left-1]) | ||
left++; | ||
|
||
while(left < right && nums[right] == nums[right+1]) | ||
right--; | ||
}else if(sum < 0) | ||
left++; | ||
else | ||
right--; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
}; |
This file contains hidden or 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,20 @@ | ||
class Solution { | ||
public: | ||
int climbStairs(int n) { | ||
if(n < 2) | ||
return 1; | ||
|
||
int curr = 0; | ||
int prev1 = 1; | ||
int prev2 = 1; | ||
|
||
for(int i = 2; i <= n; i++){ | ||
curr = prev1 + prev2; | ||
prev2 = prev1; | ||
prev1 = curr; | ||
} | ||
|
||
return curr; | ||
} | ||
}; | ||
|
This file contains hidden or 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,22 @@ | ||
class Solution { | ||
public: | ||
vector<int> productExceptSelf(vector<int>& nums) { | ||
int n = nums.size(); | ||
vector<int> result(n, 1); | ||
|
||
int left = 1; | ||
|
||
for(int i = 0; i < n; i++){ | ||
result[i] = left; | ||
left *= nums[i]; | ||
} | ||
|
||
int right = 1; | ||
for(int i = n - 1; i >= 0; i--){ | ||
result[i] *= right; | ||
right *= nums[i]; | ||
} | ||
|
||
return result; | ||
} | ||
}; |
This file contains hidden or 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,21 @@ | ||
class Solution { | ||
public: | ||
bool isAnagram(string s, string t) { | ||
unordered_map<char, int> map; | ||
|
||
if(s.length() != t.length()) | ||
return false; | ||
|
||
for(int i = 0; i < s.length(); i++) | ||
map[s[i]]++; | ||
|
||
for(int i = 0; i < t.length(); i++){ | ||
if(map[t[i]] == 0) | ||
return false; | ||
|
||
map[t[i]]--; | ||
} | ||
|
||
return true; | ||
} | ||
}; |
This file contains hidden or 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 @@ | ||
class Solution { | ||
public: | ||
bool search(TreeNode* root, long min, long max){ | ||
if(root == NULL) | ||
return true; | ||
|
||
if(root->val <= min || root->val >= max) | ||
return false; | ||
|
||
return search(root->left, min, root->val) && search(root->right, root->val, max); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 깔끔한 풀이인 것 같습니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 처음에는 left 따로 right 따로 리턴 값 체크했는데 하나로 합치는게 더 깔끔해보이더라고요 감사합니다. |
||
} | ||
|
||
bool isValidBST(TreeNode* root) { | ||
return search(root, (long)INT_MIN-1, (long)INT_MAX+1); | ||
} | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
전 set을 사용해서 중복을 제거했는데 이런 방법으로도 할 수 있네요! 한 수 배워갑니다
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
리뷰 감사합니다!!!