Skip to content

Commit d92cbba

Browse files
authored
Merge pull request neetcode-gh#29 from blurout/main
Added Golang solutions + fixed format inconsistencies
2 parents 7485f20 + b0fe750 commit d92cbba

File tree

4 files changed

+156
-0
lines changed

4 files changed

+156
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
func levelOrder(root *TreeNode) [][]int {
2+
// nil check
3+
if root == nil {
4+
return nil
5+
}
6+
7+
var result [][]int
8+
level := 0
9+
q := make([]*TreeNode,1)
10+
q[0] = root
11+
for len(q) > 0 {
12+
curLen := len(q)
13+
//add elements to the queue per level
14+
for i := 0; i < curLen; i++ {
15+
//queue implementation : delete element from front(1st element of slice)
16+
node := q[0]
17+
q = q[1:]
18+
19+
if len(result) <= level {
20+
//create new slice with first value of the level and append to result matrix
21+
result = append(result,[]int{node.Val})
22+
}else {
23+
//add values to the slice created per level
24+
result[level] = append(result[level],node.Val)
25+
}
26+
27+
//queue implementation : add element at Back( after last element of slice by append)
28+
//add left node at queue
29+
if node.Left != nil {
30+
q = append(q,node.Left)
31+
}
32+
//add Right node at queue
33+
if node.Right != nil{
34+
q = append(q,node.Right)
35+
}
36+
}
37+
//increment the level after adding current elements to result
38+
level++
39+
}
40+
return result
41+
42+
}

go/141-Linked-List-Cycle.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func hasCycle(head *ListNode) bool {
2+
if head == nil || head.Next == nil {
3+
return false
4+
}
5+
slow := head
6+
fast := head
7+
for fast != nil && fast.Next != nil {
8+
slow = slow.Next
9+
fast = fast.Next.Next
10+
if slow == fast {
11+
return true
12+
}
13+
}
14+
return false
15+
}

go/15-3Sum.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
func threeSum(nums []int) [][]int {
2+
n := len(nums)
3+
4+
// Sort the given array
5+
sort.Ints(nums)
6+
7+
var result [][]int
8+
for num1Idx := 0; num1Idx < n-2; num1Idx++ {
9+
// Skip all duplicates from left
10+
// num1Idx>0 ensures this check is made only from 2nd element onwards
11+
if num1Idx > 0 && nums[num1Idx] == nums[num1Idx-1] {
12+
continue
13+
}
14+
15+
num2Idx := num1Idx + 1
16+
num3Idx := n - 1
17+
for num2Idx < num3Idx {
18+
sum := nums[num2Idx] + nums[num3Idx] + nums[num1Idx]
19+
if sum == 0 {
20+
// Add triplet to result
21+
result = append(result, []int{nums[num1Idx], nums[num2Idx], nums[num3Idx]})
22+
23+
num3Idx--
24+
25+
// Skip all duplicates from right
26+
for num2Idx < num3Idx && nums[num3Idx] == nums[num3Idx+1] {
27+
num3Idx--
28+
}
29+
} else if sum > 0 {
30+
// Decrement num3Idx to reduce sum value
31+
num3Idx--
32+
} else {
33+
// Increment num2Idx to increase sum value
34+
num2Idx++
35+
}
36+
}
37+
}
38+
return result
39+
}

go/155-Min-Stack.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
type MinStack struct {
2+
top *StackNode
3+
min int
4+
}
5+
6+
type StackNode struct {
7+
data int
8+
next *StackNode
9+
lastmin int
10+
}
11+
12+
var mystack MinStack = MinStack{top: nil}
13+
var newtop *StackNode
14+
15+
func Constructor() MinStack {
16+
return mystack
17+
}
18+
19+
func (this *MinStack) Push(val int) {
20+
if this.top == nil {
21+
newtop = &StackNode{data: val, next: this.top}
22+
this.min = val
23+
} else {
24+
newtop = &StackNode{data: val, next: this.top, lastmin: this.min}
25+
}
26+
this.top = newtop
27+
if this.top.data < this.min {
28+
this.min = this.top.data
29+
}
30+
}
31+
32+
33+
func (this *MinStack) Pop() {
34+
if this.top.next == nil {
35+
this.top = nil
36+
return
37+
}
38+
this.min = this.top.lastmin
39+
*this.top = *this.top.next
40+
}
41+
42+
43+
func (this *MinStack) Top() int {
44+
return this.top.data
45+
}
46+
47+
48+
func (this *MinStack) GetMin() int {
49+
return this.min;
50+
}
51+
52+
53+
/**
54+
* Your MinStack object will be instantiated and called as such:
55+
* obj := Constructor();
56+
* obj.Push(val);
57+
* obj.Pop();
58+
* param_3 := obj.Top();
59+
* param_4 := obj.GetMin();
60+
*/

0 commit comments

Comments
 (0)