Skip to content

Commit 4886c86

Browse files
authored
Merge pull request #1753 from std-freejia/main
[std-freejia] week 02 solutions
2 parents ef98955 + c8d979a commit 4886c86

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

3sum/std-freejia.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public List<List<Integer>> threeSum(int[] nums) {
3+
4+
List<List<Integer>> answer = new ArrayList<>();
5+
Arrays.sort(nums);
6+
int len = nums.length;
7+
8+
for (int i = 0; i < len - 2; i++) {
9+
// 인접한 같은 수라면, 지나감
10+
if (i > 0 && nums[i] == nums[i-1]) continue;
11+
12+
int L = i + 1, H = len - 1;
13+
14+
while(L < H) {
15+
if (nums[L] + nums[H] + nums[i] > 0) {
16+
H--;
17+
} else if (nums[L] + nums[H] + nums[i] < 0) {
18+
L++;
19+
} else {
20+
answer.add(Arrays.asList(nums[i], nums[L], nums[H]));
21+
// 중복을 제거
22+
while (L < H && nums[L] == nums[L+1]) L++;
23+
while (L < H && nums[H] == nums[H-1]) H--;
24+
L++;
25+
H--;
26+
}
27+
}
28+
}
29+
return answer;
30+
}
31+
}

climbing-stairs/std-freejia.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int climbStairs(int n) {
3+
int[] dp = new int [n + 1];
4+
5+
if (n == 1) return 1;
6+
dp[1] = 1;
7+
dp[2] = 2;
8+
9+
for (int i = 3; i <= n; i++) {
10+
dp[i] = dp[i - 1] + dp[i - 2];
11+
}
12+
return dp[n];
13+
}
14+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int[] productExceptSelf(int[] nums) {
3+
int len = nums.length;
4+
int[] res = new int[len];
5+
int num = 1;
6+
7+
for (int i = 0; i < len; i++) {
8+
res[i] = num;
9+
num *= nums[i];
10+
}
11+
num = 1;
12+
for (int i = len -1; i>=0; i--) {
13+
res[i] *= num;
14+
num *= nums[i];
15+
}
16+
17+
for (int i = 0; i < len; i++) {
18+
System.out.print( res[i] + " ");
19+
}
20+
21+
return res;
22+
}
23+
}

valid-anagram/std-freejia.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public boolean isAnagram(String s, String t) {
3+
if (s.length() != t.length()) return false;
4+
5+
Map<Character, Integer> mapS = new HashMap<>();
6+
Map<Character, Integer> mapT = new HashMap<>();
7+
8+
for (int i = 0; i < s.length(); i++) {
9+
mapS.put(s.charAt(i), mapS.getOrDefault(s.charAt(i), 0) + 1);
10+
mapT.put(t.charAt(i), mapT.getOrDefault(t.charAt(i), 0) + 1);
11+
}
12+
return mapS.equals(mapT);
13+
}
14+
}

0 commit comments

Comments
 (0)