Skip to content

Commit 0b88304

Browse files
authored
Merge pull request #1757 from yhkee0404/week2
[yhkee0404] WEEK 02 solutions
2 parents 78de64d + f9cc5ca commit 0b88304

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

3sum/yhkee0404.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
fun threeSum(nums: IntArray): List<List<Int>> {
3+
val ans = mutableListOf<List<Int>>()
4+
nums.sort()
5+
for (i in 0 until nums.size) {
6+
if (i != 0 && nums[i] == nums[i - 1]) {
7+
continue
8+
}
9+
var j = i + 1
10+
var k = nums.size - 1
11+
while (j < k) {
12+
if (j != i + 1 && nums[j] == nums[j - 1]) {
13+
j++
14+
continue
15+
}
16+
val u = nums[i] + nums[j]
17+
while (j < k && u > - nums[k]) {
18+
k--
19+
}
20+
if (j >= k) {
21+
break
22+
}
23+
if (u == - nums[k]) {
24+
ans.add(listOf(nums[i], nums[j], nums[k]))
25+
}
26+
j++
27+
}
28+
}
29+
return ans
30+
}
31+
}

climbing-stairs/yhkee0404.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
object Solution {
2+
def climbStairs(n: Int): Int = {
3+
val dp = Array.ofDim[Int](n + 1)
4+
dp(0) = 1
5+
dp(1) = 1
6+
for (i <- 2 to n) {
7+
dp(i) = dp(i - 1) + dp(i - 2)
8+
}
9+
dp(n)
10+
}
11+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn product_except_self(nums: Vec<i32>) -> Vec<i32> {
3+
let mut ans = vec![1; nums.len()];
4+
let mut a = 1;
5+
for i in (0..nums.len()).rev() {
6+
ans[i] = a * nums[i];
7+
a = ans[i];
8+
}
9+
a = 1;
10+
for i in 0..nums.len() {
11+
ans[i] = a * (if i == nums.len() - 1 {1} else {ans[i + 1]});
12+
a *= nums[i];
13+
}
14+
return ans
15+
}
16+
}

valid-anagram/yhkee0404.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func isAnagram(s string, t string) bool {
2+
sRune := []rune(s)
3+
tRune := []rune(t)
4+
sort.Slice(sRune, func(i, j int) bool {
5+
return sRune[i] < sRune[j]
6+
})
7+
sort.Slice(tRune, func(i, j int) bool {
8+
return tRune[i] < tRune[j]
9+
})
10+
return string(sRune) == string(tRune)
11+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function isValidBST(root: TreeNode | null, left = - (1 << 30) * 2, right = - (left + 1)): boolean {
16+
return root === null || root.val >= left && root.val <= right
17+
&& (root.left === null || root.val != (- (1 << 30) * 2) && isValidBST(root.left, left, root.val - 1))
18+
&& (root.right === null || root.val != - (- (1 << 30) * 2 + 1) && isValidBST(root.right, root.val + 1, right))
19+
};

0 commit comments

Comments
 (0)