Skip to content

[wonYeong] Week2 #710

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 3 commits into from
Dec 20, 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
47 changes: 47 additions & 0 deletions 3sum/dalpang81.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 시간복잡도 : O(N^2)
* 공간복잡도 : O(1)
* */
import java.util.*;

class Solution {
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;
int right = nums.length - 1;

while (left < right) {
int sum = nums[i] + nums[left] + nums[right];

if (sum == 0)
{
// 합이 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;
}
}
19 changes: 19 additions & 0 deletions climbing-stairs/dalpang81.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
시간복잡도 : O(n)
공간복잡도 : O(n)
*/
class Solution {
public int climbStairs(int n) {
if (n <= 2) return n;

int[] dp = new int[n + 1];
dp[1] = 1;
dp[2] = 2;

for(int i = 3; i <= n; i++)
dp[i] = dp[i - 1] + dp[i - 2];

return dp[n];

}
}
26 changes: 26 additions & 0 deletions valid-anagram/dalpang81.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
시간복잡도 : O(N)
공간복잡도 : O(1)
*/

class Solution {
public boolean isAnagram(String s, String t) {
if(s.length() != t.length()) return false;

int[] character = new int[26];

for(int i = 0; i < s.length(); i++) {
character[s.charAt(i) - 'a']++;
character[t.charAt(i) - 'a']--;
}
Comment on lines +12 to +15
Copy link
Contributor

Choose a reason for hiding this comment

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

알파벳 크기의 배열 하나에 0을 맞춰가는 식의 풀이가 참신한것 같아요!



for(int num : character) {
if(num != 0)
return false;
}

return true;

}
}
Loading