Skip to content

Commit 6312b1a

Browse files
committed
Added Golang solutions of 'Two Pointers' tasks
Signed-off-by: Slava Lysunkin <lysunkin@gmail.com>
1 parent 118d17a commit 6312b1a

6 files changed

+206
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"unicode"
7+
)
8+
9+
func isAlnum(r rune) bool {
10+
return unicode.IsLetter(r) || unicode.IsDigit(r)
11+
}
12+
13+
func isPalindromeValid(input string) bool {
14+
s := []rune(strings.ToLower(input))
15+
left, right := 0, len(s)-1
16+
17+
for left < right {
18+
for !isAlnum(s[left]) && left < right {
19+
left++
20+
}
21+
for !isAlnum(s[right]) && left < right {
22+
right--
23+
}
24+
25+
if s[left] != s[right] {
26+
return false
27+
}
28+
29+
left++
30+
right--
31+
}
32+
33+
return true
34+
}
35+
36+
func main() {
37+
fmt.Println(isPalindromeValid("A dog! A panic in a pagoda.")) // -> true
38+
fmt.Println(isPalindromeValid("abc123")) // -> false
39+
}

go/Two Pointers/largest_container.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func largestContainer(heights []int) int {
8+
left, right := 0, len(heights)-1
9+
maxVol := 0
10+
11+
for left < right {
12+
width := right - left
13+
height := heights[left]
14+
if heights[right] < height {
15+
height = heights[right]
16+
}
17+
currentVol := width * height
18+
if currentVol > maxVol {
19+
maxVol = currentVol
20+
}
21+
22+
if heights[left] < heights[right] {
23+
left++
24+
} else {
25+
right--
26+
}
27+
}
28+
29+
return maxVol
30+
}
31+
32+
func main() {
33+
fmt.Println(largestContainer([]int{2, 7, 8, 3, 7, 6})) // -> 24
34+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func reverse(s []rune, start int) {
8+
end := len(s) - 1
9+
for start < end {
10+
s[start], s[end] = s[end], s[start]
11+
start++
12+
end--
13+
}
14+
}
15+
16+
func nextLexicographicalSequence(s string) string {
17+
letters := []rune(s)
18+
n := len(letters)
19+
pivot := n - 2
20+
21+
for pivot >= 0 && letters[pivot] >= letters[pivot+1] {
22+
pivot--
23+
}
24+
25+
if pivot == -1 {
26+
reverse(letters, 0)
27+
return string(letters)
28+
}
29+
30+
successor := n - 1
31+
for letters[successor] <= letters[pivot] {
32+
successor--
33+
}
34+
35+
letters[pivot], letters[successor] = letters[successor], letters[pivot]
36+
37+
reverse(letters, pivot+1)
38+
39+
return string(letters)
40+
}
41+
42+
func main() {
43+
fmt.Println(nextLexicographicalSequence("abcd")) // -> abdc
44+
fmt.Println(nextLexicographicalSequence("dcba")) // -> abcd
45+
}

go/Two Pointers/pair_sum_sorted.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func pairSumSorted(nums []int, target int) []int {
6+
left, right := 0, len(nums)-1
7+
for left < right {
8+
sum := nums[left] + nums[right]
9+
if sum == target {
10+
return []int{left, right}
11+
}
12+
if sum < target {
13+
left++
14+
} else {
15+
right--
16+
}
17+
}
18+
return []int{}
19+
}
20+
21+
func main() {
22+
fmt.Println(pairSumSorted([]int{-5, -2, 3, 4, 6}, 7)) // -> [2 3]
23+
fmt.Println(pairSumSorted([]int{1, 1, 1}, 2)) // -> [0 2]
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func shiftZerosToTheEnd(nums []int) {
6+
left := 0
7+
for right := 0; right < len(nums); right++ {
8+
if nums[right] != 0 {
9+
nums[left], nums[right] = nums[right], nums[left]
10+
left++
11+
}
12+
}
13+
}
14+
15+
func main() {
16+
nums := []int{0, 1, 0, 3, 2}
17+
shiftZerosToTheEnd(nums)
18+
fmt.Println(nums) // -> [1 3 2 0 0]
19+
}

go/Two Pointers/triplet_sum.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
)
7+
8+
func tripletSum(nums []int) [][]int {
9+
result := [][]int{}
10+
11+
sort.Ints(nums)
12+
n := len(nums)
13+
14+
for i := 0; i < n-2; i++ {
15+
if i > 0 && nums[i] == nums[i-1] {
16+
continue
17+
}
18+
19+
left, right := i+1, n-1
20+
for left < right {
21+
sum := nums[i] + nums[left] + nums[right]
22+
if sum == 0 {
23+
result = append(result, []int{nums[i], nums[left], nums[right]})
24+
left++
25+
right--
26+
for left < right && nums[left] == nums[left-1] {
27+
left++
28+
}
29+
for left < right && nums[right] == nums[right+1] {
30+
right--
31+
}
32+
} else if sum < 0 {
33+
left++
34+
} else {
35+
right--
36+
}
37+
}
38+
}
39+
40+
return result
41+
}
42+
43+
func main() {
44+
fmt.Println(tripletSum([]int{0, -1, 2, -3, 1})) // -> [[-3 1 2] [-1 0 1]]
45+
}

0 commit comments

Comments
 (0)