-
-
Notifications
You must be signed in to change notification settings - Fork 195
[sora0319] WEEK 02 solutions #1226
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
6 commits
Select commit
Hold shift + click to select a range
60d1865
valid anagram solved
sora0319 543a879
climbing stairs solved
sora0319 5947aa6
add newline
sora0319 065fdd0
product of array Except self solved
sora0319 9bf3c3b
3sum solved
sora0319 bbb73a4
validate binary search tree solved
sora0319 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,37 @@ | ||
import java.util.*; | ||
|
||
class Solution { | ||
public List<List<Integer>> threeSum(int[] nums) { | ||
List<List<Integer>> result = new ArrayList<>(); | ||
Arrays.sort(nums); | ||
int n = nums.length; | ||
|
||
for (int i = 0; i < n - 2; i++) { | ||
if (i > 0 && nums[i] == nums[i - 1]) continue; | ||
|
||
int left = i + 1; | ||
int right = n - 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; | ||
} | ||
} | ||
|
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,14 @@ | ||
class Solution { | ||
public int climbStairs(int n) { | ||
int[] stairs = new int[n+1]; | ||
|
||
stairs[0] = 1; | ||
stairs[1] = 1; | ||
|
||
for(int i = 2; i <= n; i++){ | ||
stairs[i] = stairs[i-1] + stairs[i-2]; | ||
} | ||
return stairs[n]; | ||
} | ||
} | ||
|
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 int[] productExceptSelf(int[] nums) { | ||
int[] answer = new int[nums.length]; | ||
int multi = nums[0]; | ||
answer[0] = 1; | ||
|
||
for(int i = 1; i < nums.length; i++){ | ||
answer[i] = multi; | ||
multi *= nums[i]; | ||
} | ||
|
||
multi = nums[nums.length-1]; | ||
for(int i = nums.length-2; i >= 0; i--){ | ||
answer[i] *= multi; | ||
multi *= nums[i]; | ||
} | ||
|
||
return answer; | ||
} | ||
} | ||
|
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 boolean isAnagram(String s, String t) { | ||
int[] character = new int[26]; | ||
if(s.length() != t.length()) return false; | ||
|
||
for(int i = 0; i < t.length(); i++){ | ||
int place = t.charAt(i) - 'a'; | ||
character[place]++; | ||
} | ||
|
||
for(int i = 0; i < s.length(); i++){ | ||
int place = s.charAt(i) - 'a'; | ||
if(character[place] <= 0) return false; | ||
character[place]--; | ||
} | ||
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,13 @@ | ||
class Solution { | ||
public boolean isValidBST(TreeNode root) { | ||
if(root==null||(root.left==null&&root.right==null)) return true; | ||
return isvalid(root,Long.MIN_VALUE,Long.MAX_VALUE); | ||
} | ||
public boolean isvalid(TreeNode root,long min,long max) | ||
{ | ||
if(root==null) return true; | ||
if(root.val<=min||root.val>=max) return false; | ||
return isvalid(root.left,min,root.val)&&isvalid(root.right,root.val,max); | ||
} | ||
} | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.