Skip to content

[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
merged 2 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions contains-duplicate/ekgns33.java
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;
}
}
75 changes: 75 additions & 0 deletions valid-palindrome/ekgns33.java
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--;
}
}
Comment on lines +59 to +72
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

투포인터 활용으로 시간 복잡도의 효율을 높이고, 자바 Charater의 기본 메소드를 사용해서 이해하기 쉬운 코드인것 같아요 :)

return true;
}
}
Loading