Skip to content
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
51 changes: 51 additions & 0 deletions 3sum/youngDaLee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package youngDaLee

import (
"fmt"
"slices"
)

func threeSum(nums []int) [][]int {
// sort nums
slices.Sort(nums)

// unique-ify nums
uniqueKeys := make(map[string]bool)

var result [][]int
left := 0
for left < len(nums)-2 {
// init pivot, right
pivot := left + 1
right := len(nums) - 1

for pivot < right {
sum := nums[left] + nums[pivot] + nums[right]
unikeyKey := fmt.Sprintf("%d%d%d", nums[left], nums[pivot], nums[right])
Copy link
Contributor

Choose a reason for hiding this comment

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

uniqueKeys map을 사용하신 게 중복을 건너뛰기 위해서이실까요?

그렇다면 이미 배열을 오름차순으로 정렬했고, left, pivot, right 포인터 모두 for 루프에서 중복을 건너뛰기 때문에 제거해도 될 것 같습니다!

if sum == 0 {
if _, ok := uniqueKeys[unikeyKey]; !ok {
uniqueKeys[unikeyKey] = true
result = append(result, []int{nums[left], nums[pivot], nums[right]})
}
for pivot < right-1 && nums[pivot] == nums[pivot+1] {
pivot += 1
}
for pivot < right-1 && nums[right] == nums[right-1] {
right -= 1
}
pivot += 1
right -= 1
} else if sum < 0 {
pivot += 1
} else if sum > 0 {
right -= 1
}
}

left += 1
for left < len(nums)-2 && nums[left] == nums[left-1] {
left += 1
}
}
return result
}
24 changes: 24 additions & 0 deletions validate-binary-search-tree/youngDaLee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package youngDaLee

import "math"

type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}

func isValidBST(root *TreeNode) bool {
return dfs(root, math.MinInt, math.MaxInt)
}

func dfs(node *TreeNode, min, max int) bool {
if node == nil {
return true
}
if (min < node.Val && node.Val < max) == false {
return false
}

return dfs(node.Left, min, node.Val) && dfs(node.Right, node.Val, max)
Copy link
Contributor

Choose a reason for hiding this comment

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

코드가 깔끔해서 풀이가 직관적으로 이해되는 것 같아요!

}