-
-
Notifications
You must be signed in to change notification settings - Fork 195
[byteho0n] - Week 1 #639
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
+124
−0
Merged
[byteho0n] - Week 1 #639
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,49 @@ | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/* | ||
start 7:06~7:14 PASS | ||
input : integer array | ||
output : return if input contains duplicate | ||
constraint : | ||
1) empty array? | ||
nope. at least one | ||
2) size? | ||
[1, 10^5] | ||
3) sorted? | ||
nope. | ||
4) range of elements in the array? | ||
[-10^9, 10^9] >> max 2 * 10*9 +1 | ||
|
||
brute force: | ||
ds : array. algo : just nested for-loop | ||
iterate through the array, for index i 0 to n. n indicates the size of input | ||
nested loop fo r index j from i+1 to n | ||
if nums[j] == nums[i] return true; | ||
|
||
return false; | ||
|
||
time : O(n^2), space : O(1) | ||
|
||
better: | ||
ds : hashset. algo : one for-loop | ||
iterate through the array: | ||
if set contains current value: | ||
return true; | ||
else | ||
add current value to set | ||
|
||
return false; | ||
time : O(n) space :O(n) | ||
|
||
*/ | ||
class Solution { | ||
public boolean containsDuplicate(int[] nums) { | ||
Set<Integer> set = new HashSet<>(); | ||
for(int num : nums) { | ||
if(set.contains(num)) return true; | ||
set.add(num); | ||
} | ||
return false; | ||
} | ||
} |
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,75 @@ | ||
/* | ||
start : 7:18 ~ | ||
input : String s | ||
output : return if given s is valid palindrome | ||
constraint: | ||
1) valid palindrome? | ||
convert all uppercase letter to lowercase, | ||
remove non-alphanumeric chars, | ||
reads same forward and backward. | ||
ex) abba, aba, a, aacbcaa | ||
2) length of the input string | ||
[1, 2*10^5] | ||
3) does input string contains non-alphanumeric chars? such as whitespace | ||
yes, but only ASCII chars | ||
|
||
------- | ||
|
||
brute force: | ||
read each character of input string. | ||
create a new string that only contains | ||
lowercase letters and numbers << O(n), when n is length of string s | ||
|
||
create string which one is reversed. < O(n) | ||
|
||
compare. < O(n) | ||
|
||
time : O(n)-3loops, space : O(n) | ||
|
||
------- | ||
better : two-pointer | ||
|
||
same as brute force. build new string. | ||
|
||
use two pointer left and right | ||
|
||
while left <= right | ||
if s[left] != s[right] return false; | ||
|
||
return true; | ||
------- | ||
optimal : | ||
|
||
use two pointer left and right | ||
while left <= right | ||
if s[left] is non-alpha left++ | ||
elif s[right] is non-alpha right-- | ||
elif s[left] != s[right] return false | ||
else left++ right-- | ||
|
||
return true | ||
time : O(n) space : O(1) | ||
*/ | ||
class Solution { | ||
public boolean isPalindrome(String s) { | ||
int left = 0; | ||
int right = s.length() - 1; | ||
char leftC; | ||
char rightC; | ||
while(left <= right) { | ||
leftC = s.charAt(left); | ||
rightC = s.charAt(right); | ||
if(!Character.isLetterOrDigit(leftC)) { | ||
left++; | ||
} else if (!Character.isLetterOrDigit(rightC)) { | ||
right--; | ||
} else if (Character.toLowerCase(leftC) != Character.toLowerCase(rightC)) { | ||
return false; | ||
} else { | ||
left++; | ||
right--; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
Oops, something went wrong.
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.
투포인터 활용으로 시간 복잡도의 효율을 높이고, 자바 Charater의 기본 메소드를 사용해서 이해하기 쉬운 코드인것 같아요 :)