├── .gitignore ├── solutions ├── 292.go ├── 237.go ├── 258.go ├── 231.go ├── 326.go ├── 319.go ├── 371.go ├── 201.go ├── 222.go ├── 136.go ├── 172.go ├── 268.go ├── 344.go ├── 190.go ├── 137.go ├── 338.go ├── 171.go ├── 198.go ├── 191.go ├── 461.go ├── 458.go ├── 342.go ├── 55.go ├── 122.go ├── 206.go ├── 226.go ├── 389.go ├── 100.go ├── 35.go ├── 397.go ├── 96.go ├── 459.go ├── 58.go ├── 343.go ├── 434.go ├── 9.go ├── 283.go ├── 69.go ├── 217.go ├── 7.go ├── 112.go ├── 83.go ├── 88.go ├── 205.go ├── 413.go ├── 151.go ├── 274.go ├── 387.go ├── 1.go ├── 104.go ├── 62.go ├── 27.go ├── 404.go ├── 53.go ├── 278.go ├── 377.go ├── 26.go ├── 462.go ├── 50.go ├── 111.go ├── 203.go ├── 168.go ├── 223.go ├── 279.go ├── 287.go ├── 116.go ├── 121.go ├── 169.go ├── 19.go ├── 453.go ├── 66.go ├── 70.go ├── 167.go ├── 33.go ├── 390.go ├── 309.go ├── 357.go ├── 80.go ├── 89.go ├── 235.go ├── 275.go ├── 330.go ├── 365.go ├── 409.go ├── 442.go ├── 466.go ├── 125.go ├── 263.go ├── 383.go ├── 108.go ├── 145.go ├── 374.go ├── 455.go ├── 392.go ├── 110.go ├── 153.go ├── 441.go ├── 260.go ├── 405.go ├── 74.go ├── 28.go ├── 367.go ├── 82.go ├── 141.go ├── 396.go ├── 242.go ├── 350.go ├── 230.go ├── 307.go ├── 162.go ├── types.go ├── 101.go ├── 14.go ├── 189.go ├── 209.go ├── 11.go ├── 421.go ├── 424.go ├── 123.go ├── 463.go ├── 454.go ├── 456.go ├── 204.go ├── 347.go ├── 41.go ├── 48.go ├── 114.go ├── 119.go ├── 12.go ├── 91.go ├── 139.go ├── 337.go ├── 118.go ├── 134.go ├── 303.go ├── 349.go ├── 98.go ├── 124.go ├── 179.go ├── 105.go ├── 334.go ├── 144.go ├── 152.go ├── 412.go ├── 416.go ├── 61.go ├── 94.go ├── 435.go ├── 78.go ├── 300.go ├── 398.go ├── 75.go ├── 115.go ├── 429.go ├── 92.go ├── 25.go ├── 451.go ├── 452.go ├── 57.go ├── 142.go ├── 199.go ├── 233.go ├── 138.go ├── 324.go ├── 382.go ├── 219.go ├── 345.go ├── 264.go ├── 5.go ├── 71.go ├── 21.go ├── 448.go ├── 52.go ├── 22.go ├── 86.go ├── 216.go ├── 437.go ├── 438.go ├── 106.go ├── 49.go ├── 120.go ├── 238.go ├── 3.go ├── 406.go ├── 109.go ├── 46.go ├── 384.go ├── 129.go ├── 24.go ├── 42.go ├── 378.go ├── 372.go ├── 213.go ├── 232.go ├── 60.go ├── 228.go ├── 20.go ├── 386.go ├── 414.go ├── 443.go ├── 77.go ├── 95.go ├── 128.go ├── 13.go ├── 160.go ├── 32.go ├── 328.go ├── 6.go ├── 97.go ├── 313.go ├── 132.go ├── 225.go ├── 447.go ├── 312.go ├── 375.go ├── 331.go ├── 90.go ├── 202.go ├── 240.go ├── 64.go ├── 415.go ├── 400.go ├── 56.go ├── 16.go ├── 2.go ├── 154.go ├── 257.go ├── 234.go ├── 67.go ├── 84.go ├── 395.go ├── 43.go ├── 47.go ├── 221.go ├── 403.go ├── 17.go ├── 200.go ├── 187.go ├── 188.go ├── 316.go ├── 290.go ├── 38.go ├── 39.go ├── 430.go ├── 354.go ├── 81.go ├── 117.go ├── 284.go ├── 464.go ├── 87.go ├── 173.go ├── 299.go ├── 436.go ├── 410.go ├── 419.go ├── 376.go ├── 388.go ├── 393.go ├── 45.go ├── 131.go ├── 147.go ├── 29.go ├── 113.go ├── 215.go ├── helpers.go ├── 102.go ├── 322.go ├── 93.go ├── 140.go ├── 34.go ├── 23.go ├── 241.go ├── 335.go ├── 318.go ├── 236.go ├── 402.go ├── 107.go ├── 133.go ├── 31.go ├── 174.go ├── 59.go ├── 103.go ├── 155.go ├── 135.go ├── 207.go ├── 341.go ├── 44.go ├── 332.go ├── 394.go ├── 310.go ├── 73.go ├── 165.go ├── 227.go ├── 79.go ├── 99.go ├── 76.go ├── 72.go ├── 368.go ├── 130.go ├── 433.go └── 40.go └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | .idea -------------------------------------------------------------------------------- /solutions/292.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func canWinNim(n int) bool { 4 | return n % 4 != 0 5 | } 6 | -------------------------------------------------------------------------------- /solutions/237.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func deleteNode(node *ListNode) { 4 | *node = *node.Next 5 | } 6 | -------------------------------------------------------------------------------- /solutions/258.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func addDigits(num int) int { 4 | return (num - 1) % 9 + 1 5 | } 6 | -------------------------------------------------------------------------------- /solutions/231.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func isPowerOfTwo(n int) bool { 4 | return n > 0 && n & (n - 1) == 0 5 | } -------------------------------------------------------------------------------- /solutions/326.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func isPowerOfThree(n int) bool { 4 | for ; n > 1 && n % 3 == 0; n /= 3 {} 5 | 6 | return n == 1 7 | } 8 | -------------------------------------------------------------------------------- /solutions/319.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func bulbSwitch(n int) int { 4 | i := 1 5 | 6 | for i * i <= n { 7 | i++ 8 | } 9 | 10 | return i - 1 11 | } 12 | -------------------------------------------------------------------------------- /solutions/371.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func getSum(a int, b int) int { 4 | if b == 0 { 5 | return a 6 | } 7 | 8 | return getSum(a^b, (a & b) << 1) 9 | } 10 | -------------------------------------------------------------------------------- /solutions/201.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func rangeBitwiseAnd(m int, n int) int { 4 | for n > m && n != 0 { 5 | n = n & (n - 1) 6 | } 7 | 8 | return n 9 | } 10 | -------------------------------------------------------------------------------- /solutions/222.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func countNodes(root *TreeNode) int { 4 | if root == nil { 5 | return 0 6 | } 7 | 8 | return 1 + countNodes(root.Left) + countNodes(root.Right) 9 | } 10 | -------------------------------------------------------------------------------- /solutions/136.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func singleNumber(nums []int) int { 4 | var result int 5 | 6 | for number := range nums { 7 | result ^= nums[number] 8 | } 9 | 10 | return result 11 | } 12 | -------------------------------------------------------------------------------- /solutions/172.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func trailingZeroes(n int) int { 4 | result := 0 5 | 6 | for n != 0 { 7 | result += n / 5 8 | 9 | n /= 5 10 | } 11 | 12 | return result 13 | } 14 | -------------------------------------------------------------------------------- /solutions/268.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func missingNumber(nums []int) int { 4 | result := len(nums) 5 | 6 | for i, value := range nums { 7 | result += i - value 8 | } 9 | 10 | return result 11 | } 12 | -------------------------------------------------------------------------------- /solutions/344.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func reverseString(s []byte) { 4 | length := len(s) 5 | 6 | for i := 0; i < length / 2; i++ { 7 | s[i], s[length - 1 - i] = s[length - 1 - i], s[i] 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /solutions/190.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func reverseBits(num uint32) uint32 { 4 | var result uint32 5 | 6 | for i := 0; i < 32; i++ { 7 | result += num >> i << 31 >> i 8 | } 9 | 10 | return result 11 | } 12 | -------------------------------------------------------------------------------- /solutions/137.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func singleNumber(nums []int) int { 4 | a, b := 0, 0 5 | 6 | for _, num := range nums { 7 | a = a ^ num & ^b 8 | b = b ^ num & ^a 9 | } 10 | 11 | return a | b 12 | } 13 | -------------------------------------------------------------------------------- /solutions/338.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func countBits(num int) []int { 4 | result := make([]int, num + 1) 5 | 6 | for i := 1; i <= num; i++ { 7 | result[i] = result[i & (i - 1)] + 1 8 | } 9 | 10 | return result 11 | } 12 | -------------------------------------------------------------------------------- /solutions/171.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func titleToNumber(title string) int { 4 | var result int 5 | 6 | for _, character := range title { 7 | result = result * 26 + int(character - 'A' + 1) 8 | } 9 | 10 | return result 11 | } 12 | -------------------------------------------------------------------------------- /solutions/198.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func rob(nums []int) int { 4 | rob, dontRob := 0, 0 5 | 6 | for _, number := range nums { 7 | rob, dontRob = number + dontRob, max(dontRob, rob) 8 | } 9 | 10 | return max(dontRob, rob) 11 | } 12 | -------------------------------------------------------------------------------- /solutions/191.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func hammingWeight(num uint32) int { 4 | result := 0 5 | 6 | for i := 0; i < 32; i++ { 7 | if (num & (1 << i)) >> i == 1 { 8 | result++ 9 | } 10 | } 11 | 12 | return result 13 | } 14 | -------------------------------------------------------------------------------- /solutions/461.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func hammingDistance(x int, y int) int { 4 | distance, result := x^y, 0 5 | 6 | for distance != 0 { 7 | if distance & 1 == 1 { 8 | result++ 9 | } 10 | 11 | distance >>= 1 12 | } 13 | 14 | return result 15 | } 16 | -------------------------------------------------------------------------------- /solutions/458.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "math" 5 | ) 6 | 7 | func poorPigs(buckets int, minutesToDie int, minutesToTest int) int { 8 | return int(math.Ceil(math.Log(float64(buckets)) / math.Log(float64(minutesToTest / minutesToDie + 1)))) 9 | } 10 | -------------------------------------------------------------------------------- /solutions/342.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "math" 5 | ) 6 | 7 | func isPowerOfFour(num int) bool { 8 | if num <= 0 { 9 | return false 10 | } 11 | 12 | a := math.Log(float64(num)) / math.Log(4) 13 | 14 | return math.Floor(a) == a 15 | } 16 | -------------------------------------------------------------------------------- /solutions/55.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func canJump(nums []int) bool { 4 | previous := len(nums) - 1 5 | 6 | for i := previous - 1; i >= 0; i-- { 7 | if nums[i] >= previous - i { 8 | previous = i 9 | } 10 | } 11 | 12 | return previous == 0 13 | } 14 | -------------------------------------------------------------------------------- /solutions/122.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func maxProfit(prices []int) int { 4 | profit := 0 5 | 6 | for i := 1; i < len(prices); i++ { 7 | if prices[i] > prices[i - 1] { 8 | profit += prices[i] - prices[i - 1] 9 | } 10 | } 11 | 12 | return profit 13 | } 14 | -------------------------------------------------------------------------------- /solutions/206.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func reverseList(head *ListNode) *ListNode { 4 | var result *ListNode 5 | 6 | for head != nil { 7 | temp := head.Next 8 | head.Next = result 9 | result = head 10 | head = temp 11 | } 12 | 13 | return result 14 | } 15 | -------------------------------------------------------------------------------- /solutions/226.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func invertTree(root *TreeNode) *TreeNode { 4 | if root == nil { 5 | return nil 6 | } 7 | 8 | root.Left, root.Right = root.Right, root.Left 9 | 10 | invertTree(root.Left) 11 | invertTree(root.Right) 12 | 13 | return root 14 | } 15 | -------------------------------------------------------------------------------- /solutions/389.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func findTheDifference(s string, t string) byte { 4 | var difference byte 5 | 6 | for i := range s { 7 | difference ^= s[i] 8 | difference ^= t[i] 9 | } 10 | 11 | difference ^= t[len(t) - 1] 12 | 13 | return difference 14 | } 15 | -------------------------------------------------------------------------------- /solutions/100.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func isSameTree(p *TreeNode, q *TreeNode) bool { 4 | if p == nil || q == nil { 5 | return p == q 6 | } 7 | 8 | if p.Val != q.Val { 9 | return false 10 | } 11 | 12 | return isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right) 13 | } 14 | -------------------------------------------------------------------------------- /solutions/35.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func searchInsert(nums []int, target int) int { 4 | if target < nums[0] { 5 | return 0 6 | } 7 | 8 | for index, value := range nums { 9 | if target <= value { 10 | return index 11 | } 12 | } 13 | 14 | return len(nums) 15 | } -------------------------------------------------------------------------------- /solutions/397.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func integerReplacement(n int) int { 4 | if n == 1 { 5 | return 0 6 | } 7 | 8 | if n % 2 == 0 { 9 | return integerReplacement(n / 2) + 1 10 | } else { 11 | return min(integerReplacement(n + 1), integerReplacement(n - 1)) + 1 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /solutions/96.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func numTrees(n int) int { 4 | result := make([]int, n + 1) 5 | result[0] = 1 6 | 7 | for i := 1; i <= n; i++ { 8 | for j := 1; j <= i; j++ { 9 | result[i] += result[j - 1] * result[i - j] 10 | } 11 | } 12 | 13 | return result[n] 14 | } 15 | -------------------------------------------------------------------------------- /solutions/459.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "strings" 5 | ) 6 | 7 | func repeatedSubstringPattern(s string) bool { 8 | if len(s) == 0 { 9 | return false 10 | } 11 | 12 | size := len(s) 13 | substring := (s + s)[1: size * 2 - 1] 14 | 15 | return strings.Contains(substring, s) 16 | } 17 | -------------------------------------------------------------------------------- /solutions/58.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func lengthOfLastWord(s string) int { 4 | space, result := 0, 0 5 | 6 | for i, char := range s { 7 | if char == ' ' { 8 | space = i + 1 9 | } else { 10 | result = i + 1 - space 11 | } 12 | } 13 | 14 | return result 15 | } 16 | -------------------------------------------------------------------------------- /solutions/343.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func integerBreak(n int) int { 4 | if n == 2 { 5 | return 1 6 | } 7 | 8 | if n == 3 { 9 | return 2 10 | } 11 | 12 | product := 1 13 | 14 | for n > 4 { 15 | product *= 3 16 | n -= 3 17 | } 18 | 19 | return product * n 20 | } 21 | -------------------------------------------------------------------------------- /solutions/434.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func countSegments(s string) int { 4 | count := 0 5 | previous := ' ' 6 | 7 | for _, current := range s { 8 | if previous == ' ' && current != ' ' { 9 | count++ 10 | } 11 | 12 | previous = current 13 | } 14 | 15 | return count 16 | } 17 | -------------------------------------------------------------------------------- /solutions/9.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func isPalindrome(x int) bool { 4 | if x < 0 { 5 | return false 6 | } 7 | 8 | source := x 9 | var reversed int 10 | 11 | for x > 0 { 12 | reversed = (reversed * 10) + (x % 10) 13 | 14 | x = x / 10 15 | } 16 | 17 | return reversed == source 18 | } 19 | -------------------------------------------------------------------------------- /solutions/283.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func moveZeroes(nums []int) { 4 | count := 0 5 | 6 | for i := 0; i < len(nums); i++ { 7 | if nums[i] != 0 { 8 | nums[count] = nums[i] 9 | count++ 10 | } 11 | } 12 | 13 | for i := count; i < len(nums); i++ { 14 | nums[i] = 0 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /solutions/69.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func mySqrt(x int) int { 4 | if x == 0 { 5 | return 0 6 | } 7 | 8 | result := 0.5 * float64(x) 9 | 10 | for i := 0 ; i < 20; i++ { 11 | approximation := 0.5 * (result + float64(x) / result) 12 | result = approximation 13 | } 14 | 15 | return int(result) 16 | } 17 | -------------------------------------------------------------------------------- /solutions/217.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func containsDuplicate(nums []int) bool { 4 | set := make(map[int]struct{}) 5 | 6 | for _, value := range nums { 7 | if _, ok := set[value]; ok { 8 | return true 9 | } else { 10 | set[value] = struct{}{} 11 | } 12 | } 13 | 14 | return false 15 | } 16 | -------------------------------------------------------------------------------- /solutions/7.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "math" 5 | ) 6 | 7 | func reverse(x int) int { 8 | var result int 9 | 10 | for x != 0 { 11 | if result > math.MaxInt32 / 10 || result < math.MinInt32 / 10 { 12 | return 0 13 | } 14 | 15 | result = (result * 10) + (x % 10) 16 | 17 | x = x / 10 18 | } 19 | 20 | return result 21 | } 22 | -------------------------------------------------------------------------------- /solutions/112.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func hasPathSum(root *TreeNode, sum int) bool { 4 | if root == nil { 5 | return false 6 | } 7 | 8 | if root.Left == nil && root.Right == nil { 9 | return sum == root.Val 10 | } 11 | 12 | return hasPathSum(root.Left, sum - root.Val) || hasPathSum(root.Right, sum - root.Val) 13 | } 14 | -------------------------------------------------------------------------------- /solutions/83.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func deleteDuplicates(head *ListNode) *ListNode { 4 | node := head 5 | 6 | for head != nil && head.Next != nil { 7 | if head.Val == head.Next.Val { 8 | head.Next = head.Next.Next 9 | } else { 10 | head = head.Next 11 | } 12 | } 13 | 14 | return node 15 | } 16 | -------------------------------------------------------------------------------- /solutions/88.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func merge(nums1 []int, m int, nums2 []int, n int) { 4 | for n > 0 { 5 | if m > 0 && nums1[m - 1] > nums2[n - 1] { 6 | nums1[m + n - 1] = nums1[m - 1] 7 | 8 | m-- 9 | } else { 10 | nums1[m + n - 1] = nums2[n - 1] 11 | 12 | n-- 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /solutions/205.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func isIsomorphic(s string, t string) bool { 4 | first, second := make([]int, 128), make([]int, 128) 5 | 6 | for i := 0; i < len(s); i++ { 7 | if first[s[i]] != second[t[i]] { 8 | return false 9 | } 10 | 11 | first[s[i]], second[t[i]] = i + 1, i + 1 12 | } 13 | 14 | return true 15 | } -------------------------------------------------------------------------------- /solutions/413.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func numberOfArithmeticSlices(A []int) int { 4 | dp, result := 0, 0 5 | 6 | for i := 2; i < len(A); i++ { 7 | if A[i] - A[i - 1] == A[i - 1] - A[i - 2] { 8 | dp = dp + 1 9 | result += dp 10 | } else { 11 | dp = 0 12 | } 13 | } 14 | 15 | return result 16 | } 17 | -------------------------------------------------------------------------------- /solutions/151.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "strings" 5 | ) 6 | 7 | func reverseWords(s string) string { 8 | words := strings.Fields(s) 9 | 10 | i, j := 0, len(words) - 1 11 | 12 | for i < j { 13 | words[i], words[j] = words[j], words[i] 14 | 15 | i++ 16 | j-- 17 | } 18 | 19 | return strings.Join(words, " ") 20 | } 21 | -------------------------------------------------------------------------------- /solutions/274.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func hIndex(citations []int) int { 8 | result := 0 9 | 10 | sort.Ints(citations) 11 | 12 | for i := 0; i < len(citations); i++ { 13 | if citations[len(citations) - 1 - i] >= i + 1 { 14 | result = i + 1 15 | } 16 | } 17 | 18 | return result 19 | } 20 | -------------------------------------------------------------------------------- /solutions/387.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func firstUniqChar(s string) int { 4 | letters := make([]int, 26) 5 | 6 | for _, character := range s { 7 | letters[character - 'a']++ 8 | } 9 | 10 | for i, character := range s { 11 | if letters[character - 'a'] == 1 { 12 | return i 13 | } 14 | } 15 | 16 | return -1 17 | } 18 | -------------------------------------------------------------------------------- /solutions/1.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func twoSum(nums []int, target int) []int { 4 | lookupMap := make(map[int]int) 5 | 6 | for i, value := range nums { 7 | j, complement := lookupMap[-value] 8 | 9 | if complement { 10 | return []int{j, i} 11 | } 12 | 13 | lookupMap[value - target] = i 14 | } 15 | 16 | return []int{} 17 | } -------------------------------------------------------------------------------- /solutions/104.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func maxDepth(root *TreeNode) int { 4 | if root == nil { 5 | return 0 6 | } 7 | 8 | if isLeaf(root) { 9 | return 1 10 | } 11 | 12 | return 1 + max(maxDepth(root.Left), maxDepth(root.Right)) 13 | } 14 | 15 | func isLeaf(node *TreeNode) bool { 16 | return node.Left == nil && node.Right == nil 17 | } 18 | -------------------------------------------------------------------------------- /solutions/62.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func uniquePaths(m int, n int) int { 4 | result := make([]int, m) 5 | 6 | for i := 0; i < m; i++ { 7 | result[i] = 1 8 | } 9 | 10 | for j := 1; j < n; j++ { 11 | for i := 1; i < m; i++ { 12 | result[i] = result[i] + result[i - 1] 13 | } 14 | } 15 | 16 | return result[m - 1] 17 | } 18 | -------------------------------------------------------------------------------- /solutions/27.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func removeElement(nums []int, val int) int { 4 | if nums == nil || len(nums) == 0 { 5 | return 0 6 | } 7 | 8 | index := 0 9 | 10 | for i := 0; i < len(nums); i++ { 11 | if nums[i] != val { 12 | nums[index] = nums[i] 13 | index++ 14 | } 15 | } 16 | 17 | return index 18 | } 19 | -------------------------------------------------------------------------------- /solutions/404.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func sumOfLeftLeaves(root *TreeNode) int { 4 | if root == nil { 5 | return 0 6 | } 7 | 8 | value := 0 9 | 10 | if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil { 11 | value = root.Left.Val 12 | } 13 | 14 | return value + sumOfLeftLeaves(root.Left) + sumOfLeftLeaves(root.Right) 15 | } 16 | -------------------------------------------------------------------------------- /solutions/53.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func maxSubArray(nums []int) int { 4 | max, sum := nums[0],nums[0] 5 | 6 | for _, value := range nums[1:] { 7 | if sum < 0 { 8 | sum = value 9 | } else { 10 | sum += value 11 | } 12 | 13 | if max < sum { 14 | max = sum 15 | } 16 | } 17 | 18 | return max 19 | } 20 | -------------------------------------------------------------------------------- /solutions/278.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func firstBadVersion(n int) int { 4 | i, j := 1, n 5 | 6 | for i + 1 < j { 7 | middle := (i + j) / 2 8 | if isBadVersion(middle) { 9 | j = middle 10 | } else { 11 | i = middle 12 | } 13 | } 14 | 15 | if isBadVersion(i) { 16 | return i 17 | } 18 | 19 | return j 20 | } 21 | -------------------------------------------------------------------------------- /solutions/377.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func combinationSum4(nums []int, target int) int { 4 | dp := make([]int, target + 1) 5 | dp[0] = 1 6 | 7 | for i := 1; i <= target; i++ { 8 | for _, value := range nums { 9 | if i - value >= 0 { 10 | dp[i] += dp[i - value] 11 | } 12 | } 13 | } 14 | 15 | return dp[target] 16 | } 17 | -------------------------------------------------------------------------------- /solutions/26.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func removeDuplicates(nums []int) int { 4 | length := len(nums) 5 | 6 | if length <= 1 { 7 | return length 8 | } 9 | 10 | unique, previous := 1, nums[0] 11 | 12 | for i := 1; i < length; i++ { 13 | if nums[i] != previous { 14 | nums[unique] = nums[i] 15 | previous = nums[i] 16 | 17 | unique++ 18 | } 19 | } 20 | 21 | return unique 22 | } 23 | -------------------------------------------------------------------------------- /solutions/462.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "math" 5 | "sort" 6 | ) 7 | 8 | func minMoves2(nums []int) int { 9 | sort.Slice(nums, func(i, j int) bool { 10 | return nums[i] < nums[j] 11 | }) 12 | 13 | var moves = 0 14 | middle := nums[len(nums) / 2] 15 | 16 | for _, value := range nums { 17 | moves += int(math.Abs(float64(middle - value))) 18 | } 19 | 20 | return moves 21 | } 22 | -------------------------------------------------------------------------------- /solutions/50.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func myPow(x float64, n int) float64 { 4 | if n == 0 { 5 | return 1 6 | } 7 | 8 | if n < 0 { 9 | x, n = 1 / x, -n 10 | } 11 | 12 | result := 1.0 13 | 14 | for n > 0 { 15 | if n % 2 == 1 { 16 | result *= x 17 | } 18 | 19 | x, n = x * x, n >> 1 20 | } 21 | 22 | return result 23 | } -------------------------------------------------------------------------------- /solutions/111.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func minDepth(root *TreeNode) int { 4 | if root == nil { 5 | return 0 6 | } 7 | 8 | left := minDepth(root.Left) 9 | right := minDepth(root.Right) 10 | 11 | if left == 0 { 12 | return right + 1 13 | } 14 | 15 | if right == 0 || left < right { 16 | return left + 1 17 | } 18 | 19 | return right + 1 20 | } 21 | -------------------------------------------------------------------------------- /solutions/203.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func removeElements(head *ListNode, val int) *ListNode { 4 | dummy := &ListNode{} 5 | head, dummy.Next = dummy, head 6 | 7 | for dummy.Next != nil { 8 | if dummy.Next.Val == val { 9 | dummy.Next = dummy.Next.Next 10 | } else { 11 | dummy = dummy.Next 12 | } 13 | } 14 | 15 | return head.Next 16 | } 17 | -------------------------------------------------------------------------------- /solutions/168.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func convertToTitle(number int) string { 4 | title := "" 5 | 6 | for number > 0 { 7 | value := number % 26 8 | number = number / 26 9 | 10 | if value == 0 { 11 | value = 26 12 | number -= 1 13 | } 14 | 15 | title = string('A' + byte(value - 1)) + title 16 | } 17 | 18 | return title 19 | } 20 | -------------------------------------------------------------------------------- /solutions/223.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func computeArea(A int, B int, C int, D int, E int, F int, G int, H int) int { 4 | sum1 := (C - A) * (D - B) 5 | sum2 := (H - F) * (G - E) 6 | 7 | if E >= C || A >= G || F >= D || B >= H { 8 | return sum1 + sum2 9 | } 10 | 11 | intersect := (min(C, G) - max(A, E)) * (min(D, H) - max(B, F)) 12 | 13 | return sum1 + sum2 - intersect 14 | } 15 | -------------------------------------------------------------------------------- /solutions/279.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "math" 5 | ) 6 | 7 | func numSquares(n int) int { 8 | dp := make([]int, n + 1) 9 | dp[0], dp[1] = 0, 1 10 | 11 | for i := 2; i <= n; i++ { 12 | dp[i] = math.MaxInt32 13 | 14 | for j := 1; i - j * j >= 0; j++ { 15 | dp[i] = min(dp[i], dp[i - j * j] + 1) 16 | } 17 | } 18 | 19 | return dp[n] 20 | } 21 | -------------------------------------------------------------------------------- /solutions/287.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func findDuplicate(nums []int) int { 4 | slow, fast := nums[0], nums[0] 5 | 6 | for ok := true; ok; ok = slow != fast { 7 | slow = nums[slow] 8 | fast = nums[nums[fast]] 9 | } 10 | 11 | fast = nums[0] 12 | 13 | for fast != slow { 14 | fast = nums[fast] 15 | slow = nums[slow] 16 | } 17 | 18 | return fast 19 | } 20 | -------------------------------------------------------------------------------- /solutions/116.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func connect(root *Node) *Node { 4 | if root == nil { 5 | return nil 6 | } 7 | 8 | if root.Left != nil { 9 | root.Left.Next = root.Right 10 | 11 | if root.Next != nil { 12 | root.Right.Next = root.Next.Left 13 | } 14 | } 15 | 16 | connect(root.Left) 17 | connect(root.Right) 18 | 19 | return root 20 | } 21 | -------------------------------------------------------------------------------- /solutions/121.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "math" 5 | ) 6 | 7 | func maxProfit(prices []int) int { 8 | low := math.MaxInt32 9 | profit := 0 10 | 11 | for i := 0; i < len(prices); i++ { 12 | if prices[i] < low { 13 | low = prices[i] 14 | } else { 15 | profit = max(profit, prices[i] - low) 16 | } 17 | } 18 | 19 | return profit 20 | } 21 | -------------------------------------------------------------------------------- /solutions/169.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func majorityElement(nums []int) int { 4 | count, result := 0, 0 5 | 6 | for i := 0; i < len(nums); i++ { 7 | if count == 0 { 8 | result = nums[i] 9 | count++ 10 | } else if nums[i] == result { 11 | count++ 12 | } else { 13 | count-- 14 | } 15 | } 16 | 17 | return result 18 | } 19 | -------------------------------------------------------------------------------- /solutions/19.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func removeNthFromEnd(head *ListNode, n int) *ListNode { 4 | dummy := &ListNode{Next: head} 5 | 6 | for n > 0 { 7 | head = head.Next 8 | n-- 9 | } 10 | 11 | prev := dummy 12 | 13 | for head != nil { 14 | head, prev = head.Next, prev.Next 15 | } 16 | 17 | prev.Next = prev.Next.Next 18 | 19 | return dummy.Next 20 | } 21 | -------------------------------------------------------------------------------- /solutions/453.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "math" 5 | ) 6 | 7 | func minMoves(nums []int) int { 8 | min := math.MaxInt64 9 | 10 | for _, value := range nums { 11 | if value < min { 12 | min = value 13 | } 14 | } 15 | 16 | result := 0 17 | 18 | for _, value := range nums { 19 | result += value - min 20 | } 21 | 22 | return result 23 | } 24 | -------------------------------------------------------------------------------- /solutions/66.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func plusOne(digits []int) []int { 4 | for i := len(digits); i > 0; i-- { 5 | if digits[i - 1] < 9 { 6 | digits[i - 1] += 1 7 | 8 | return digits 9 | } 10 | 11 | digits[i - 1] = 0 12 | 13 | if i == 1 { 14 | digits = append([]int{1}, digits...) 15 | } 16 | } 17 | 18 | return digits 19 | } 20 | -------------------------------------------------------------------------------- /solutions/70.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func climbStairs(n int) int { 4 | if n == 1 { 5 | return 1 6 | } 7 | 8 | if n == 2 { 9 | return 2 10 | } 11 | 12 | next := 0 13 | first := 1 14 | second := 2 15 | 16 | for i := 2; i < n; i++ { 17 | next = first + second 18 | first = second 19 | second = next 20 | } 21 | 22 | return next 23 | } 24 | -------------------------------------------------------------------------------- /solutions/167.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func twoSum(numbers []int, target int) []int { 4 | left, right := 0, len(numbers) - 1 5 | 6 | for { 7 | if numbers[left] + numbers[right] > target { 8 | right-- 9 | } else if numbers[left] + numbers[right] < target { 10 | left++ 11 | } else { 12 | return []int{left + 1, right + 1} 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /solutions/33.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func search(nums []int, target int) int { 4 | left := 0 5 | right := len(nums) - 1 6 | 7 | for left <= right { 8 | if nums[left] == target { 9 | return left 10 | } 11 | 12 | if nums[right] == target { 13 | return right 14 | } 15 | 16 | left++ 17 | right-- 18 | } 19 | 20 | return -1 21 | } 22 | -------------------------------------------------------------------------------- /solutions/390.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func lastRemaining(n int) int { 4 | i, gap, start := 1, 1, 1 5 | max := n 6 | 7 | for { 8 | if i % 2 == 1 || n % 2 == 1 { 9 | start = start + gap 10 | 11 | if start > max { 12 | return start - gap 13 | } 14 | } 15 | 16 | gap = gap << 1 17 | n = n >> 1 18 | i++ 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /solutions/309.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func maxProfit(prices []int) int { 4 | buy, sell := -int(^uint(0) >> 1) - 1, -int(^uint(0) >> 1) - 1 5 | nothing := 0 6 | 7 | for i := 0; i < len(prices); i++ { 8 | temp := nothing 9 | 10 | nothing = max(nothing, sell) 11 | buy = max(buy, temp - prices[i]) 12 | 13 | sell = buy + prices[i] 14 | } 15 | 16 | return max(nothing, sell) 17 | } 18 | -------------------------------------------------------------------------------- /solutions/357.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func countNumbersWithUniqueDigits(n int) int { 4 | result := 1 5 | 6 | if n > 10 { 7 | n = 10 8 | } 9 | 10 | for i := 1; i <= n; i++ { 11 | j, base, end := i, 9, 9 12 | 13 | for j > 1 { 14 | base *= end 15 | end-- 16 | j-- 17 | } 18 | 19 | result += base 20 | } 21 | 22 | return result 23 | } 24 | -------------------------------------------------------------------------------- /solutions/80.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func removeDuplicates(nums []int) int { 4 | if len(nums) <= 2 { 5 | return len(nums) 6 | } 7 | 8 | result := 2 9 | 10 | for i := 2; i < len(nums); i++ { 11 | if nums[result - 1] != nums[result - 2] || nums[result - 1] != nums[i] { 12 | nums[result] = nums[i] 13 | 14 | result++ 15 | } 16 | } 17 | 18 | return result 19 | } 20 | -------------------------------------------------------------------------------- /solutions/89.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func grayCode(n int) []int { 4 | if n < 0 { 5 | return nil 6 | } 7 | 8 | result := []int{0} 9 | 10 | if n == 0 { 11 | return result 12 | } 13 | 14 | for i := 0; i < n; i++ { 15 | for j := len(result) - 1; j >= 0; j-- { 16 | result = append(result, result[j] | (1 << uint(i))) 17 | } 18 | } 19 | 20 | return result 21 | } 22 | -------------------------------------------------------------------------------- /solutions/235.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { 4 | if root == nil { 5 | return nil 6 | } 7 | 8 | if root.Val > p.Val && root.Val > q.Val { 9 | return lowestCommonAncestor(root.Left, p, q) 10 | } 11 | 12 | if root.Val < p.Val && root.Val < q.Val { 13 | return lowestCommonAncestor(root.Right, p, q) 14 | } 15 | 16 | return root 17 | } 18 | -------------------------------------------------------------------------------- /solutions/275.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func hIndex(citations []int) int { 4 | length := len(citations) 5 | left, right := 0, length - 1 6 | 7 | for left <= right { 8 | middle := left + (right - left) / 2 9 | 10 | if citations[middle] < length-middle { 11 | left = middle + 1 12 | } else { 13 | right = middle - 1 14 | } 15 | } 16 | 17 | return length - left 18 | } 19 | -------------------------------------------------------------------------------- /solutions/330.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func minPatches(nums []int, n int) int { 4 | length := len(nums) 5 | maxValue, result, i := 0, 0, 0 6 | 7 | for maxValue < n { 8 | if i < length && nums[i] <= maxValue+ 1 { 9 | maxValue += nums[i] 10 | i++ 11 | } else { 12 | maxValue += maxValue + 1 13 | result++ 14 | } 15 | } 16 | 17 | return result 18 | } 19 | -------------------------------------------------------------------------------- /solutions/365.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func canMeasureWater(x int, y int, z int) bool { 4 | if x + y < z { 5 | return false 6 | } 7 | 8 | if z == 0 { 9 | return true 10 | } 11 | 12 | return z % greatestCommonDivisor(x, y) == 0 13 | } 14 | 15 | func greatestCommonDivisor(x int, y int) int { 16 | if y == 0 { 17 | return x 18 | } 19 | 20 | return greatestCommonDivisor(y, x % y) 21 | } 22 | -------------------------------------------------------------------------------- /solutions/409.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func longestPalindrome(s string) int { 4 | cache := make(map[byte]struct{}) 5 | 6 | for i := range s { 7 | if _, ok := cache[s[i]]; ok { 8 | delete(cache, s[i]) 9 | } else { 10 | cache[s[i]] = struct{}{} 11 | } 12 | } 13 | 14 | if len(cache) == 0 { 15 | return len(s) 16 | } 17 | 18 | return len(s) - len(cache) + 1 19 | } 20 | -------------------------------------------------------------------------------- /solutions/442.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "math" 5 | ) 6 | 7 | func findDuplicates(nums []int) []int { 8 | result := make([]int, 0) 9 | 10 | for i := range nums { 11 | index := int(math.Abs(float64(nums[i])) - 1) 12 | 13 | if nums[index] < 0 { 14 | result = append(result, index + 1) 15 | } 16 | 17 | nums[index] = -nums[index] 18 | } 19 | 20 | return result 21 | } 22 | -------------------------------------------------------------------------------- /solutions/466.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func getMaxRepetitions(s1 string, n1 int, s2 string, n2 int) int { 4 | i1, i2 := 0, 0 5 | p1, p2 := 0, 0 6 | 7 | for i1 < n1 { 8 | if s1[p1] == s2[p2] { 9 | p1++ 10 | p2++ 11 | } else { 12 | p1++ 13 | } 14 | 15 | if p1 >= len(s1) { 16 | i1++ 17 | p1 = 0 18 | } 19 | 20 | if p2 >= len(s2) { 21 | i2++ 22 | p2 = 0 23 | } 24 | } 25 | 26 | return i2 / n2 27 | } 28 | -------------------------------------------------------------------------------- /solutions/125.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func isPalindrome(s string) bool { 4 | for i, j := 0, len(s) - 1; i < j; i, j = i + 1, j - 1 { 5 | for ; i < j && !isChar(s[i]); { 6 | i++ 7 | } 8 | 9 | for ; i < j && !isChar(s[j]); { 10 | j-- 11 | } 12 | 13 | if toLower(s[i]) != toLower(s[j]) { 14 | return false 15 | } 16 | } 17 | 18 | return true 19 | } 20 | -------------------------------------------------------------------------------- /solutions/263.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func isUgly(num int) bool { 4 | if num == 0 { 5 | return false 6 | } 7 | 8 | for num != 1 { 9 | if num % 2 == 0 { 10 | num /= 2 11 | } else if num % 5 == 0 { 12 | num /= 5 13 | } else if num % 3 == 0 { 14 | num /= 3 15 | } else { 16 | return false 17 | } 18 | } 19 | 20 | return true 21 | } 22 | -------------------------------------------------------------------------------- /solutions/383.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func canConstruct(ransomNote string, magazine string) bool { 4 | letters := make([]int, 26) 5 | 6 | for _, value := range magazine { 7 | letters[value - 'a']++ 8 | } 9 | 10 | for _, value := range ransomNote { 11 | letters[value - 'a']-- 12 | 13 | if letters[value - 'a'] < 0 { 14 | return false 15 | } 16 | } 17 | 18 | return true 19 | } 20 | -------------------------------------------------------------------------------- /solutions/108.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func sortedArrayToBST(nums []int) *TreeNode { 4 | if len(nums) == 0 { 5 | return nil 6 | } 7 | 8 | middle := len(nums) / 2 9 | result := new(TreeNode) 10 | result.Val = nums[middle] 11 | 12 | if middle != 0 { 13 | result.Left = sortedArrayToBST(nums[:middle]) 14 | result.Right = sortedArrayToBST(nums[middle + 1:]) 15 | } 16 | 17 | return result 18 | } 19 | -------------------------------------------------------------------------------- /solutions/145.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func postorderTraversal(root *TreeNode) []int { 4 | var result []int 5 | 6 | postorderTraverse(root, &result) 7 | 8 | return result 9 | } 10 | 11 | func postorderTraverse(root *TreeNode,output *[]int) { 12 | if root != nil { 13 | postorderTraverse(root.Left, output) 14 | postorderTraverse(root.Right, output) 15 | 16 | *output = append(*output, root.Val) 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /solutions/374.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func guessNumber(n int) int { 4 | left, right := 1, n 5 | 6 | for left <= right { 7 | middle := (left + right) / 2 8 | result := guess(middle) 9 | 10 | if result == 0 { 11 | return middle 12 | } else if result == -1 { 13 | right = middle -1 14 | } else { 15 | left = middle +1 16 | } 17 | } 18 | 19 | return -1 20 | } 21 | -------------------------------------------------------------------------------- /solutions/455.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func findContentChildren(g []int, s []int) int { 8 | sort.Ints(g) 9 | sort.Ints(s) 10 | 11 | result, i, j := 0, 0, 0 12 | 13 | for i < len(g) && j < len(s) { 14 | if g[i] <= s[j] { 15 | result++ 16 | i++ 17 | j++ 18 | } else { 19 | j++ 20 | } 21 | } 22 | 23 | return result 24 | } 25 | -------------------------------------------------------------------------------- /solutions/392.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func isSubsequence(s string, t string) bool { 4 | currentIndex, maxIndex := 0, len(s) 5 | 6 | if maxIndex == 0 { 7 | return true 8 | } 9 | 10 | for i := 0; i < len(t); i++ { 11 | if t[i] == s[currentIndex] { 12 | currentIndex++ 13 | } 14 | 15 | if currentIndex == maxIndex { 16 | return true 17 | } 18 | } 19 | 20 | return false 21 | } 22 | -------------------------------------------------------------------------------- /solutions/110.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func isBalanced(root *TreeNode) bool { 4 | return isBalancedTree(root) != -1 5 | } 6 | 7 | func isBalancedTree(node *TreeNode) int { 8 | if node == nil { 9 | return 0 10 | } 11 | 12 | left, right := isBalancedTree(node.Left), isBalancedTree(node.Right) 13 | 14 | if left == -1 || right == -1 || abs(left - right) > 1 { 15 | return -1 16 | } 17 | 18 | return 1 + max(left, right) 19 | } 20 | -------------------------------------------------------------------------------- /solutions/153.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func findMin(nums []int) int { 4 | left, right := 0, len(nums) - 1 5 | 6 | for left < right { 7 | middle := (right-left) / 2 + left 8 | 9 | if nums[middle] < nums[0] { 10 | right = middle 11 | } else if nums[middle] > nums[right] { 12 | left = middle + 1 13 | } else { 14 | return nums[left] 15 | } 16 | } 17 | 18 | return nums[left] 19 | } 20 | -------------------------------------------------------------------------------- /solutions/441.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func arrangeCoins(n int) int { 4 | n = n * 2 5 | low, high := 0, n 6 | 7 | for low <= high { 8 | middle := (high + low) / 2 9 | value := middle * (middle + 1) 10 | 11 | if value == n { 12 | return middle 13 | } else if value < n { 14 | low = middle + 1 15 | } else { 16 | high = middle - 1 17 | } 18 | } 19 | 20 | return high 21 | } -------------------------------------------------------------------------------- /solutions/260.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func singleNumber(nums []int) []int { 4 | xor := 0 5 | 6 | for _, value := range nums { 7 | xor = xor ^ value 8 | } 9 | 10 | result, lastBit := make([]int, 2), xor & (-xor) 11 | 12 | for _, value := range nums { 13 | if value & lastBit == 0 { 14 | result[0] ^= value 15 | } else { 16 | result[1] ^= value 17 | } 18 | } 19 | 20 | return result 21 | } 22 | -------------------------------------------------------------------------------- /solutions/405.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func toHex(num int) string { 4 | if num == 0 { 5 | return "0" 6 | } 7 | 8 | hex := "0123456789abcdef" 9 | sum, result := 0, "" 10 | 11 | for i := 31; i >= 0; i-- { 12 | sum = sum << 1 + (num >> i) & 1 13 | 14 | if i % 4 == 0 && !(len(result) == 0 && sum == 0) { 15 | result += string(hex[sum]) 16 | sum = 0 17 | } 18 | } 19 | 20 | return result 21 | } 22 | -------------------------------------------------------------------------------- /solutions/74.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func searchMatrix(matrix [][]int, target int) bool { 4 | row := len(matrix) - 1 5 | column := 0 6 | 7 | for row >= 0 && column < len(matrix[0]) { 8 | if matrix[row][column] == target { 9 | return true 10 | } 11 | 12 | if matrix[row][column] < target { 13 | column++ 14 | 15 | continue 16 | } 17 | 18 | row-- 19 | } 20 | 21 | return false 22 | } 23 | -------------------------------------------------------------------------------- /solutions/28.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func strStr(haystack string, needle string) int { 4 | if len(needle) == 0 || haystack == needle { 5 | return 0 6 | } 7 | 8 | if len(haystack) == 0 { 9 | return -1 10 | } 11 | 12 | for i := 0; i < (len(haystack) - len(needle) + 1); i++ { 13 | word := haystack[i:len(needle) + i] 14 | 15 | if (word) == needle{ 16 | return i 17 | } 18 | } 19 | 20 | return -1 21 | } 22 | -------------------------------------------------------------------------------- /solutions/367.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func isPerfectSquare(num int) bool { 4 | low, high := 1, num 5 | 6 | for low <= high { 7 | medium := (low + high) >> 1 8 | current := medium * medium 9 | 10 | if num == current { 11 | return true 12 | } 13 | 14 | if current < num { 15 | low = medium + 1 16 | } else { 17 | high = medium - 1 18 | } 19 | } 20 | 21 | return false 22 | } 23 | -------------------------------------------------------------------------------- /solutions/82.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func deleteDuplicates(head *ListNode) *ListNode { 4 | node := &head 5 | 6 | for *node != nil && (*node).Next != nil { 7 | value := (*node).Val 8 | 9 | if (*node).Next.Val != value { 10 | node = &((*node).Next) 11 | 12 | continue 13 | } 14 | 15 | for *node != nil && (*node).Val == value { 16 | *node = (*node).Next 17 | } 18 | } 19 | 20 | return head 21 | } -------------------------------------------------------------------------------- /solutions/141.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func hasCycle(head *ListNode) bool { 4 | if head == nil || head.Next == nil { 5 | return false 6 | } 7 | 8 | fast, slow := head, head 9 | 10 | for fast != nil { 11 | fast = fast.Next 12 | 13 | if fast != nil { 14 | fast = fast.Next 15 | } 16 | 17 | slow = slow.Next 18 | 19 | if slow == fast { 20 | return true 21 | } 22 | } 23 | 24 | return false 25 | } -------------------------------------------------------------------------------- /solutions/396.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func maxRotateFunction(A []int) int { 4 | summary, base := 0, 0 5 | 6 | for index, value := range A { 7 | summary += value 8 | base += index * value 9 | } 10 | 11 | max := base 12 | 13 | for i := 0; i < len(A)-1; i++ { 14 | base += summary 15 | base -= len(A) * A[len(A) - 1 - i] 16 | 17 | if base > max { 18 | max = base 19 | } 20 | } 21 | 22 | return max 23 | } 24 | -------------------------------------------------------------------------------- /solutions/242.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func isAnagram(s string, t string) bool { 4 | if len(s) != len(t) { 5 | return false 6 | } 7 | 8 | alphabet := make([]int, 26) 9 | 10 | for _, char := range s { 11 | alphabet[int(char - 'a')]++ 12 | } 13 | 14 | for _, char := range t { 15 | if alphabet[int(char - 'a')] <= 0 { 16 | return false 17 | } 18 | 19 | alphabet[int(char - 'a')]-- 20 | } 21 | 22 | return true 23 | } 24 | -------------------------------------------------------------------------------- /solutions/350.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func intersect(nums1 []int, nums2 []int) []int { 4 | hashMap := make(map[int]int, len(nums1)) 5 | var result []int 6 | 7 | for _, number := range nums1 { 8 | hashMap[number]++ 9 | } 10 | 11 | for _, number := range nums2 { 12 | if value, ok := hashMap[number]; ok && value > 0 { 13 | result = append(result, number) 14 | hashMap[number]-- 15 | } 16 | } 17 | 18 | return result 19 | } 20 | -------------------------------------------------------------------------------- /solutions/230.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func kthSmallest(root *TreeNode, k int) int { 4 | var stack []TreeNode 5 | 6 | for { 7 | for root != nil { 8 | stack = append(stack, *root) 9 | root = root.Left 10 | } 11 | 12 | root = &stack[len(stack) - 1] 13 | stack = stack[: len(stack) - 1] 14 | 15 | k-- 16 | 17 | if k == 0 { 18 | return root.Val 19 | } 20 | 21 | root = root.Right 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /solutions/307.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | type NumArray struct { 4 | items []int 5 | } 6 | 7 | func Constructor(nums []int) NumArray { 8 | return NumArray{items: nums} 9 | } 10 | 11 | func (this *NumArray) Update(i int, value int) { 12 | this.items[i] = value 13 | } 14 | 15 | func (this *NumArray) SumRange(i int, j int) int { 16 | subArray, sum := this.items[i: j + 1], 0 17 | 18 | for _, item := range subArray { 19 | sum += item 20 | } 21 | 22 | return sum 23 | } 24 | -------------------------------------------------------------------------------- /solutions/162.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func findPeakElement(nums []int) int { 4 | return searchPeakElement(nums, 0, len(nums) - 1) 5 | } 6 | 7 | func searchPeakElement(nums []int, left int, right int) int { 8 | if left == right { 9 | return left 10 | } 11 | 12 | middle := left + (right - left) / 2 13 | 14 | if nums[middle] > nums[middle + 1] { 15 | return searchPeakElement(nums, left, middle) 16 | } 17 | 18 | return searchPeakElement(nums, middle + 1, right) 19 | } -------------------------------------------------------------------------------- /solutions/types.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | type BSTIterator struct { 4 | stack []*TreeNode 5 | } 6 | 7 | type GraphNode struct { 8 | Val int 9 | Neighbors []*GraphNode 10 | } 11 | 12 | type ListNode struct { 13 | Val int 14 | Next *ListNode 15 | } 16 | 17 | type TreeNode struct { 18 | Val int 19 | Left *TreeNode 20 | Right *TreeNode 21 | } 22 | 23 | type Node struct { 24 | Val int 25 | Left *Node 26 | Right *Node 27 | Next *Node 28 | Random *Node 29 | } 30 | -------------------------------------------------------------------------------- /solutions/101.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func isSymmetric(root *TreeNode) bool { 4 | if root == nil { 5 | return true 6 | } 7 | 8 | return isSymetricRecursive(root.Left, root.Right) 9 | } 10 | 11 | func isSymetricRecursive(p, q *TreeNode) bool { 12 | if p == nil || q == nil { 13 | return p == q 14 | } 15 | 16 | if p.Val != q.Val { 17 | return false 18 | } 19 | 20 | return isSymetricRecursive(p.Left, q.Right) && isSymetricRecursive(p.Right, q.Left) 21 | } 22 | -------------------------------------------------------------------------------- /solutions/14.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func longestCommonPrefix(strs []string) string { 4 | if strs == nil || len(strs) == 0 { 5 | return "" 6 | } 7 | 8 | first := strs[0] 9 | fromSecond := strs[1:] 10 | 11 | for i := 0; i < len(first); i++ { 12 | letter := first[i] 13 | 14 | for _, s := range fromSecond { 15 | if len(s) <= i || letter != s[i] { 16 | return first[:i] 17 | } 18 | } 19 | } 20 | 21 | return first; 22 | } 23 | -------------------------------------------------------------------------------- /solutions/189.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func rotate(nums []int, k int) { 4 | if len(nums) == 0 || k == 0 { 5 | return 6 | } 7 | 8 | k = k % len(nums) 9 | 10 | reverseArray(nums, 0, len(nums) - 1) 11 | reverseArray(nums, 0, k - 1) 12 | reverseArray(nums, k, len(nums) - 1) 13 | } 14 | 15 | func reverseArray(nums []int, start, end int) { 16 | for start < end { 17 | nums[start], nums[end] = nums[end], nums[start] 18 | 19 | start++ 20 | end-- 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /solutions/209.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func minSubArrayLen(s int, nums []int) int { 4 | var i, j, sum int 5 | 6 | min := 1<<31 - 1 7 | 8 | for j < len(nums) { 9 | sum += nums[j] 10 | j++ 11 | 12 | for sum >= s { 13 | if min > j - i { 14 | min = j - i 15 | } 16 | 17 | sum -= nums[i] 18 | i++ 19 | } 20 | } 21 | 22 | if min == 1<<31 - 1 { 23 | return 0 24 | } 25 | 26 | return min 27 | } 28 | -------------------------------------------------------------------------------- /solutions/11.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func maxArea(height []int) int { 4 | maxArea := 0 5 | i, j := 0, len(height) - 1 6 | 7 | for { 8 | if i == j { 9 | break 10 | } 11 | 12 | minHeight := height[i] 13 | 14 | if height[j] < height[i] { 15 | minHeight = height[j] 16 | } 17 | 18 | area := (j - i) * minHeight 19 | 20 | if area > maxArea { 21 | maxArea = area 22 | } 23 | 24 | if height[i] < height[j] { 25 | i++ 26 | } else { 27 | j-- 28 | } 29 | } 30 | 31 | return maxArea 32 | } 33 | -------------------------------------------------------------------------------- /solutions/421.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func findMaximumXOR(nums []int) int { 4 | result := 0 5 | 6 | for i := 31; i >= 0; i-- { 7 | result <<= 1 8 | pre := make(map[int]bool) 9 | 10 | for _, number := range nums { 11 | pre[number >> i] = true 12 | } 13 | 14 | for p := range pre { 15 | if pre[result ^ 1 ^ p] { 16 | result += 1 17 | break 18 | } 19 | } 20 | } 21 | 22 | return result 23 | } 24 | -------------------------------------------------------------------------------- /solutions/424.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func characterReplacement(s string, k int) int { 4 | counts := make([]int, 26) 5 | maxCount, result := 0, 0 6 | 7 | for i, j := 0, 0; j < len(s); j++ { 8 | counts[s[j] - 'A']++ 9 | maxCount = max(maxCount, counts[s[j] - 'A']) 10 | 11 | for i <= j && j - i + 1 - maxCount > k { 12 | counts[s[i] - 'A']-- 13 | i++ 14 | } 15 | 16 | result = max(result, j - i + 1) 17 | } 18 | 19 | return result 20 | } 21 | -------------------------------------------------------------------------------- /solutions/123.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "math" 5 | ) 6 | 7 | func maxProfit(prices []int) int { 8 | firstBuy, secondBuy := math.MinInt32, math.MinInt32 9 | firstSell, secondSell := 0, 0 10 | 11 | for _, price := range prices { 12 | secondSell = max(secondSell, secondBuy + price) 13 | secondBuy = max(secondBuy, firstSell - price) 14 | firstSell = max(firstSell, firstBuy + price) 15 | firstBuy = max(firstBuy, -price) 16 | } 17 | 18 | return secondSell 19 | } 20 | -------------------------------------------------------------------------------- /solutions/463.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func islandPerimeter(grid [][]int) int { 4 | var lands, neighbours int 5 | 6 | for i := 0; i < len(grid); i++ { 7 | for j := 0; j < len(grid[0]); j++ { 8 | if grid[i][j] == 0 { 9 | continue 10 | } 11 | 12 | lands++ 13 | 14 | if i != len(grid) - 1 && grid[i + 1][j] == 1 { 15 | neighbours++ 16 | } 17 | 18 | if j != len(grid[0]) - 1 && grid[i][j + 1] == 1 { 19 | neighbours++ 20 | } 21 | } 22 | } 23 | 24 | return 4 * lands - 2 * neighbours 25 | } 26 | -------------------------------------------------------------------------------- /solutions/454.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func fourSumCount(A []int, B []int, C []int, D []int) int { 4 | dictionary := make(map[int]int) 5 | length := len(A) 6 | result := 0 7 | 8 | for i := 0; i < length; i++ { 9 | for j := 0; j < length; j++ { 10 | dictionary[A[i] + B[j]]++ 11 | } 12 | } 13 | 14 | for i := 0; i < length; i++ { 15 | for j := 0; j < length; j++ { 16 | result += dictionary[-C[i]-D[j]] 17 | } 18 | } 19 | 20 | return result 21 | } 22 | -------------------------------------------------------------------------------- /solutions/456.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func find132pattern(nums []int) bool { 4 | stack := make([]int, 0) 5 | max := -1 << 31 6 | 7 | for i := len(nums) - 1; i >= 0; i-- { 8 | if nums[i] < max { 9 | return true 10 | } 11 | 12 | for len(stack) > 0 && stack[len(stack) - 1] < nums[i] { 13 | max = stack[len(stack) - 1] 14 | stack = stack[:len(stack) - 1] 15 | } 16 | 17 | stack = append(stack, nums[i]) 18 | } 19 | 20 | return false 21 | } 22 | -------------------------------------------------------------------------------- /solutions/204.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func countPrimes(n int) int { 4 | if n < 3 { 5 | return 0 6 | } 7 | 8 | count := n / 2 9 | composite := make([]bool, n) 10 | 11 | for i := 3; i * i < n; i += 2 { 12 | if composite[i] { 13 | continue 14 | } 15 | 16 | for j := i * i; j < n; j += 2 * i { 17 | if !composite[j] { 18 | composite[j] = true 19 | count-- 20 | } 21 | } 22 | } 23 | 24 | return count 25 | } 26 | -------------------------------------------------------------------------------- /solutions/347.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func topKFrequent(nums []int, k int) []int { 8 | dictionary := make(map[int]int) 9 | 10 | for _, value := range nums { 11 | dictionary[value]++ 12 | } 13 | 14 | var keys []int 15 | 16 | for key := range dictionary { 17 | keys = append(keys, key) 18 | } 19 | 20 | sort.Slice(keys, func (i int, j int) bool { 21 | return dictionary[keys[i]] > dictionary[keys[j]] 22 | }) 23 | 24 | return keys[:k] 25 | } 26 | -------------------------------------------------------------------------------- /solutions/41.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func firstMissingPositive(nums []int) int { 4 | length := len(nums) 5 | 6 | for i := 0; i < length; { 7 | if nums[i] > 0 && nums[i] <= length && nums[i] != nums[nums[i] - 1] { 8 | nums[i], nums[nums[i] - 1] = nums[nums[i] - 1], nums[i] 9 | } else { 10 | i++ 11 | } 12 | } 13 | 14 | for i := 0; i < length; i++ { 15 | if nums[i] != i + 1 { 16 | return i + 1 17 | } 18 | } 19 | 20 | return length + 1 21 | } -------------------------------------------------------------------------------- /solutions/48.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func rotate(matrix [][]int) { 4 | n := len(matrix) - 1 5 | 6 | for row := 0; row < (n + 1) / 2; row++ { 7 | for column := row; column < n - row; column++ { 8 | temp := matrix[row][column] 9 | matrix[row][column] = matrix[n - column][row] 10 | matrix[n - column][row] = matrix[n - row][n - column] 11 | matrix[n - row][n - column] = matrix[column][n - row] 12 | matrix[column][n - row] = temp 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /solutions/114.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func flatten(root *TreeNode) { 4 | if root == nil { 5 | return 6 | } 7 | 8 | if root.Left != nil { 9 | flatten(root.Left) 10 | 11 | node := root.Left 12 | 13 | for node.Right != nil { 14 | node = node.Right 15 | } 16 | 17 | flatten(root.Right) 18 | 19 | node.Right = root.Right 20 | root.Right = root.Left 21 | root.Left = nil 22 | 23 | return 24 | } 25 | 26 | flatten(root.Right) 27 | } 28 | -------------------------------------------------------------------------------- /solutions/119.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func getRow(rowIndex int) []int { 4 | result := make([][]int, 2) 5 | 6 | for i := 0; i < 2; i++ { 7 | result[i] = make([]int, rowIndex+1) 8 | } 9 | 10 | result[1][0] = 1 11 | 12 | for i := 0; i < rowIndex; i++ { 13 | result[i % 2][0], result[i % 2][i + 1] = 1, 1 14 | 15 | for j := 1; j <= i; j++ { 16 | result[i % 2][j] = result[1 - i % 2][j] + result[1 - i % 2][j - 1] 17 | } 18 | } 19 | 20 | return result[1 - rowIndex % 2] 21 | } 22 | -------------------------------------------------------------------------------- /solutions/12.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func intToRoman(num int) string { 4 | roman := []string{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"} 5 | values := []int{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1} 6 | 7 | var result string 8 | 9 | for key, value := range values { 10 | var multiply = num / value 11 | 12 | for i := 0; i < multiply; i++ { 13 | num -= value 14 | 15 | result += roman[key] 16 | } 17 | } 18 | 19 | return result 20 | } -------------------------------------------------------------------------------- /solutions/91.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func numDecodings(s string) int { 4 | if len(s) == 0 || s[0] == '0' { 5 | return 0 6 | } 7 | 8 | result := make([]int, len(s) + 1) 9 | result[0] = 1 10 | 11 | for i := 1; i <= len(s); i++ { 12 | if s[i - 1] != '0' { 13 | result[i] = result[i - 1] 14 | } 15 | 16 | if i >= 2 && (s[i - 2] == '1' || (s[i - 2] == '2' && s[i - 1] <= '6')) { 17 | result[i] += result[i - 2] 18 | } 19 | } 20 | 21 | return result[len(s)] 22 | } -------------------------------------------------------------------------------- /solutions/139.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func wordBreak(s string, wordDict []string) bool { 4 | result := make([]bool, len(s) + 1) 5 | result[0] = true 6 | 7 | for i := 0 ; i < len(s); i++ { 8 | if result[i] == false { 9 | continue 10 | } 11 | 12 | for _, word := range wordDict { 13 | j := i + len(word) 14 | 15 | if j <= len(s) && s[i: j] == word { 16 | result[j] = true 17 | } 18 | } 19 | } 20 | 21 | return result[len(s)] 22 | } 23 | -------------------------------------------------------------------------------- /solutions/337.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func rob(root *TreeNode) int { 4 | result := searchHouses(root) 5 | 6 | return max(result[0], result[1]) 7 | } 8 | 9 | func searchHouses(root *TreeNode) [2]int { 10 | if root == nil { 11 | return [2]int{0, 0} 12 | } 13 | 14 | left, right := searchHouses(root.Left), searchHouses(root.Right) 15 | 16 | result := [2]int{0, 0} 17 | result[0] = max(left[0], left[1]) + max(right[0], right[1]) 18 | result[1] = root.Val + left[0] + right[0] 19 | 20 | return result 21 | } 22 | -------------------------------------------------------------------------------- /solutions/118.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func generate(numRows int) [][]int { 4 | result := make([][]int, numRows) 5 | 6 | for i := 0; i < numRows; i += 1 { 7 | level := make([]int, i + 1) 8 | level[0], level[i] = 1, 1 9 | 10 | if i > 1 { 11 | for j := 1; j <= i / 2; j++ { 12 | level[j] = result[i - 1][j - 1] + result[i - 1][j] 13 | level[len(level) - 1 - j] = level[j] 14 | } 15 | } 16 | 17 | result[i] = level 18 | } 19 | 20 | return result 21 | } -------------------------------------------------------------------------------- /solutions/134.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func canCompleteCircuit(gas []int, cost []int) int { 4 | totalTank, currentTank, startStation := 0, 0, 0 5 | 6 | for i, value := range gas { 7 | difference := value - cost[i] 8 | 9 | totalTank += difference 10 | currentTank += difference 11 | 12 | if currentTank < 0 { 13 | startStation = i + 1 14 | currentTank = 0 15 | } 16 | } 17 | 18 | if totalTank > -1 { 19 | return startStation 20 | } 21 | 22 | return -1 23 | } 24 | -------------------------------------------------------------------------------- /solutions/303.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | type NumArray struct { 4 | sum []int 5 | } 6 | 7 | func Constructor(nums []int) NumArray { 8 | if len(nums) == 0 { 9 | return NumArray{} 10 | } 11 | 12 | sum := make([]int, len(nums) + 1) 13 | sum[0], sum[1] = 0, nums[0] 14 | 15 | for i := 2; i < len(sum); i++ { 16 | sum[i] = sum[i - 1] + nums[i - 1] 17 | } 18 | 19 | return NumArray{sum: sum} 20 | } 21 | 22 | func (this *NumArray) SumRange(i int, j int) int { 23 | return this.sum[j + 1] - this.sum[i] 24 | } 25 | -------------------------------------------------------------------------------- /solutions/349.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func intersection(nums1 []int, nums2 []int) []int { 4 | hashMap := make(map[int]bool) 5 | var result []int 6 | 7 | for i := range nums1 { 8 | hashMap[nums1[i]] = false 9 | } 10 | 11 | for i := range nums2 { 12 | if value, ok := hashMap[nums2[i]]; ok { 13 | if value { 14 | continue 15 | } 16 | 17 | result = append(result, nums2[i]) 18 | hashMap[nums2[i]] = true 19 | } 20 | } 21 | 22 | return result 23 | } 24 | -------------------------------------------------------------------------------- /solutions/98.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func isValidBST(root *TreeNode) bool { 4 | if root == nil{ 5 | return true 6 | } 7 | 8 | return validateBST(root, nil, nil) 9 | } 10 | 11 | func validateBST(root *TreeNode, min, max interface{}) bool { 12 | if root == nil { 13 | return true 14 | } 15 | 16 | if (min != nil && root.Val <= min.(int)) || (max != nil && root.Val >= max.(int)) { 17 | return false 18 | } 19 | 20 | return validateBST(root.Left, min, root.Val) && validateBST(root.Right, root.Val, max) 21 | } 22 | -------------------------------------------------------------------------------- /solutions/124.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "math" 5 | ) 6 | 7 | func maxPathSum(root *TreeNode) int { 8 | result := math.MinInt32 9 | 10 | sumMaxPath(root, &result) 11 | 12 | return result 13 | } 14 | 15 | func sumMaxPath(root *TreeNode, result *int) int { 16 | if root == nil { 17 | return 0 18 | } 19 | 20 | left := sumMaxPath(root.Left, result) 21 | right := sumMaxPath(root.Right, result) 22 | 23 | *result = max(left + right + root.Val, *result) 24 | 25 | return max(0, max(left, right) + root.Val) 26 | } 27 | -------------------------------------------------------------------------------- /solutions/179.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "sort" 5 | "strconv" 6 | ) 7 | 8 | func largestNumber(nums []int) string { 9 | var result string 10 | 11 | sort.Slice(nums, func(i, j int) bool { 12 | first, second := strconv.Itoa(nums[i]), strconv.Itoa(nums[j]) 13 | 14 | return first + second >= second + first 15 | }) 16 | 17 | for _, number := range nums { 18 | result += strconv.Itoa(number) 19 | } 20 | 21 | if result[0] == '0' { 22 | return result[: 1] 23 | } 24 | 25 | return result 26 | } -------------------------------------------------------------------------------- /solutions/105.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func buildTree(preorder []int, inorder []int) *TreeNode { 4 | if len(preorder) == 0 || len(inorder) == 0 { 5 | return nil 6 | } 7 | 8 | position := -1 9 | 10 | for i, number := range inorder { 11 | if preorder[0] == number { 12 | position = i 13 | } 14 | } 15 | 16 | return &TreeNode{ 17 | preorder[0], 18 | buildTree(preorder[1: position + 1], inorder[0: position]), 19 | buildTree(preorder[position + 1:], inorder[position + 1:]), 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /solutions/334.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "math" 5 | ) 6 | 7 | func increasingTriplet(nums []int) bool { 8 | if nums == nil || len(nums) < 3 { 9 | return false 10 | } 11 | 12 | first, second := nums[0], math.MaxInt16 13 | 14 | for i := 1; i < len(nums); i++ { 15 | if nums[i] <= first{ 16 | first = nums[i] 17 | } else if second != math.MaxInt16 && nums[i] > second { 18 | return true 19 | } else { 20 | second = nums[i] 21 | } 22 | } 23 | 24 | return false 25 | } 26 | -------------------------------------------------------------------------------- /solutions/144.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func preorderTraversal(root *TreeNode) []int { 4 | var result []int 5 | var stack []*TreeNode 6 | current := root 7 | 8 | for current != nil || len(stack) != 0 { 9 | for current != nil { 10 | stack = append(stack, current) 11 | result = append(result, current.Val) 12 | current = current.Left 13 | } 14 | 15 | current = stack[len(stack) - 1] 16 | stack = stack[: len(stack) - 1] 17 | current = current.Right 18 | } 19 | 20 | return result 21 | } 22 | -------------------------------------------------------------------------------- /solutions/152.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func maxProduct(nums []int) int { 4 | if nums == nil || len(nums) == 0 { 5 | return 0 6 | } 7 | 8 | result, lastMax, lastMin := nums[0], 1, 1 9 | 10 | for i := 0; i < len(nums); i++ { 11 | if nums[i] < 0 { 12 | lastMax, lastMin = lastMin, lastMax 13 | } 14 | 15 | lastMax, lastMin = max(lastMax * nums[i], nums[i]), min(lastMin * nums[i], nums[i]) 16 | 17 | if lastMax > result { 18 | result = lastMax 19 | } 20 | } 21 | 22 | return result 23 | } 24 | -------------------------------------------------------------------------------- /solutions/412.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "strconv" 5 | ) 6 | 7 | func fizzBuzz(n int) []string { 8 | var result []string 9 | 10 | for i := 1; i <= n; i++ { 11 | if i % 3 == 0 && i % 5 == 0 { 12 | result = append(result, "FizzBuzz") 13 | } else if i % 3 == 0 { 14 | result = append(result, "Fizz") 15 | } else if i % 5 == 0 { 16 | result = append(result, "Buzz") 17 | } else { 18 | result = append(result, strconv.Itoa(i)) 19 | } 20 | } 21 | 22 | return result 23 | } 24 | -------------------------------------------------------------------------------- /solutions/416.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func canPartition(nums []int) bool { 4 | var sum int 5 | 6 | for _, value := range nums { 7 | sum += value 8 | } 9 | 10 | if sum % 2 == 1 { 11 | return false 12 | } 13 | 14 | sum = sum / 2 15 | dp := make([]bool, sum + 1) 16 | dp[0] = true 17 | 18 | for _, value := range nums { 19 | for j := sum; j > 0; j-- { 20 | if j >= value && dp[j - value] { 21 | dp[j] = dp[j - value] 22 | } 23 | } 24 | } 25 | 26 | return dp[sum] 27 | } 28 | -------------------------------------------------------------------------------- /solutions/61.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func rotateRight(head *ListNode, k int) *ListNode { 4 | if head == nil || head.Next == nil { 5 | return head 6 | } 7 | 8 | current := head 9 | var length = 1 10 | 11 | for current.Next != nil { 12 | current = current.Next 13 | length++ 14 | } 15 | 16 | current.Next = head 17 | j := length - (k % length) 18 | 19 | for i := 1; i <= j; i++ { 20 | current = current.Next 21 | } 22 | 23 | head = current.Next 24 | current.Next = nil 25 | 26 | return head 27 | } 28 | -------------------------------------------------------------------------------- /solutions/94.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func inorderTraversal(root *TreeNode) []int { 4 | var stack []*TreeNode 5 | var result []int 6 | 7 | current := root 8 | 9 | for current != nil || len(stack) > 0 { 10 | for current != nil { 11 | stack = append(stack, current) 12 | current = current.Left 13 | } 14 | 15 | current = stack[len(stack) - 1] 16 | stack = stack[: len(stack) - 1] 17 | result = append(result, current.Val) 18 | current = current.Right 19 | } 20 | 21 | return result 22 | } 23 | -------------------------------------------------------------------------------- /solutions/435.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func eraseOverlapIntervals(intervals [][]int) int { 8 | result, previous := 0, 0 9 | 10 | sort.Slice(intervals, func(a, b int) bool { return intervals[a][0] < intervals[b][0] }) 11 | 12 | for i := 1; i < len(intervals); i++ { 13 | if intervals[i][0] < intervals[previous][1] { 14 | if intervals[i][1] < intervals[previous][1] { previous = i } 15 | result++ 16 | } else { 17 | previous = i 18 | } 19 | } 20 | 21 | return result 22 | } 23 | -------------------------------------------------------------------------------- /solutions/78.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func subsets(nums []int) [][]int { 4 | if len(nums) == 0 { 5 | return [][]int{{}} 6 | } 7 | 8 | result := make([][]int, 0) 9 | 10 | backtrackSubsets(nums, []int{}, &result) 11 | 12 | return result 13 | } 14 | 15 | func backtrackSubsets(nums []int, path []int, result *[][]int) { 16 | *result = append(*result, path) 17 | 18 | for i := 0; i < len(nums); i++ { 19 | nextPath := append([]int{}, path...) 20 | 21 | backtrackSubsets(nums[i + 1:], append(nextPath, nums[i]), result) 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /solutions/300.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func lengthOfLIS(nums []int) int { 4 | tails, size := make([]int, len(nums)), 0 5 | 6 | for _, number := range nums { 7 | i, j := 0, size 8 | 9 | for i != j { 10 | middle := (i + j) / 2 11 | 12 | if tails[middle] < number { 13 | i = middle + 1 14 | } else { 15 | j = middle 16 | } 17 | } 18 | 19 | tails[i] = number 20 | 21 | if i == size { 22 | size++ 23 | } 24 | } 25 | 26 | return size 27 | } 28 | -------------------------------------------------------------------------------- /solutions/398.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "math/rand" 5 | ) 6 | 7 | type Solution struct { 8 | list []int 9 | } 10 | 11 | func Constructor(nums []int) Solution { 12 | return Solution{list: nums} 13 | } 14 | 15 | func (this *Solution) Pick(target int) int { 16 | result, count := 0, 0 17 | 18 | for index, value := range this.list { 19 | if value == target { 20 | if rand.Intn(count + 1) == 0 { 21 | result = index 22 | } 23 | 24 | count++ 25 | } 26 | } 27 | 28 | return result 29 | } 30 | -------------------------------------------------------------------------------- /solutions/75.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func sortColors(nums []int) { 4 | low, mid, high := 0, 0, len(nums) - 1 5 | 6 | for mid <= high { 7 | if nums[mid] == 0 { 8 | swapIndexes(nums, low, mid) 9 | 10 | low++ 11 | mid++ 12 | } else if nums[mid] == 2 { 13 | swapIndexes(nums, mid, high) 14 | 15 | high-- 16 | 17 | } else if nums[mid] == 1 { 18 | mid++ 19 | } 20 | } 21 | } 22 | 23 | func swapIndexes(array []int, i, j int) { 24 | array[i], array[j] = array[j], array[i] 25 | } 26 | -------------------------------------------------------------------------------- /solutions/115.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func numDistinct(s string, t string) int { 4 | if len(s) < len(t) { 5 | return 0 6 | } 7 | 8 | if len(t) == 0 { 9 | return 1 10 | } 11 | 12 | result := make([]int, len(t)) 13 | 14 | for i := range s { 15 | previous := 1 16 | 17 | for j := range t { 18 | current := result[j] 19 | 20 | if t[j] == s[i] { 21 | result[j] += previous 22 | } 23 | 24 | previous = current 25 | } 26 | } 27 | 28 | return result[len(t) - 1] 29 | } 30 | -------------------------------------------------------------------------------- /solutions/429.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func levelOrder(root *Node) [][]int { 4 | var result [][]int 5 | 6 | traverseTree(root, 0, &result) 7 | 8 | return result 9 | } 10 | 11 | func traverseTree(root *Node, level int, result *[][]int) { 12 | if root == nil { 13 | return 14 | } 15 | 16 | for _, value := range root.Children { 17 | traverseTree(value, level + 1, result) 18 | } 19 | 20 | for len(*result) <= level { 21 | *result = append(*result, []int{}) 22 | } 23 | 24 | (*result)[level] = append((*result)[level], root.Val) 25 | } 26 | -------------------------------------------------------------------------------- /solutions/92.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func reverseBetween(head *ListNode, m int, n int) *ListNode { 4 | node := &ListNode{ Next: head } 5 | previous := node 6 | 7 | for i := 1; i < m; i++ { 8 | previous = previous.Next 9 | head = head.Next 10 | } 11 | 12 | left := head 13 | right := head.Next 14 | 15 | for i := m; i < n; i++ { 16 | next := right.Next 17 | 18 | right.Next = head 19 | head = right 20 | right = next 21 | } 22 | 23 | left.Next = right 24 | previous.Next = head 25 | 26 | return node.Next 27 | } -------------------------------------------------------------------------------- /solutions/25.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func reverseKGroup(head *ListNode, k int) *ListNode { 4 | if k == 1 { 5 | return head 6 | } 7 | 8 | dummy := head 9 | 10 | for i := 0; i < k; i++ { 11 | if dummy == nil { 12 | return head 13 | } 14 | 15 | dummy = dummy.Next 16 | } 17 | 18 | previous, current, next := reverseKGroup(dummy, k), head, head.Next 19 | 20 | for current != dummy { 21 | next, current.Next = current.Next, previous 22 | previous, current = current, next 23 | } 24 | 25 | return previous 26 | } 27 | -------------------------------------------------------------------------------- /solutions/451.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func frequencySort(s string) string { 8 | dictionary := make([]int, 128) 9 | list := make([]byte, len(s)) 10 | 11 | for i := 0; i < len(s); i++ { 12 | dictionary[s[i]]++ 13 | list[i] = s[i] 14 | } 15 | 16 | sort.Slice(list, func (i int, j int) bool { 17 | if dictionary[list[i]] == dictionary[list[j]] { 18 | return list[i] < list[j] 19 | } 20 | 21 | return dictionary[list[i]] > dictionary[list[j]] 22 | }) 23 | 24 | return string(list) 25 | } 26 | -------------------------------------------------------------------------------- /solutions/452.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func findMinArrowShots(points [][]int) int { 8 | if len(points) == 0 { 9 | return 0 10 | } 11 | 12 | sort.Slice(points, func(i, j int) bool { 13 | return points[i][1] < points[j][1] 14 | }) 15 | 16 | arrows := 1 17 | position := points[0][1] 18 | 19 | for i := 1; i < len(points); i++ { 20 | if points[i][0] <= position { 21 | continue 22 | } 23 | 24 | arrows++ 25 | position = points[i][1] 26 | } 27 | 28 | return arrows 29 | } 30 | -------------------------------------------------------------------------------- /solutions/57.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func insert(intervals [][]int, newInterval []int) [][]int { 4 | var left, right [][]int 5 | 6 | for _, value := range intervals { 7 | if value[1] < newInterval[0] { 8 | left = append(left, value) 9 | } else if value[0] > newInterval[1] { 10 | right = append(right, value) 11 | } else { 12 | newInterval[0] = min(newInterval[0], value[0]) 13 | newInterval[1] = max(newInterval[1], value[1]) 14 | } 15 | } 16 | 17 | return append(append(left, newInterval), right...) 18 | } 19 | -------------------------------------------------------------------------------- /solutions/142.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func detectCycle(head *ListNode) *ListNode { 4 | if head == nil { 5 | return nil 6 | } 7 | 8 | var slow, fast = head, head 9 | 10 | for { 11 | if fast == nil || fast.Next == nil { 12 | return nil 13 | } 14 | 15 | slow = slow.Next 16 | fast = fast.Next.Next 17 | 18 | if slow == fast { 19 | break 20 | } 21 | } 22 | 23 | slow = head 24 | 25 | for slow != fast { 26 | slow = slow.Next 27 | fast = fast.Next 28 | } 29 | 30 | return fast 31 | } 32 | -------------------------------------------------------------------------------- /solutions/199.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func rightSideView(root *TreeNode) []int { 4 | if root == nil { 5 | return nil 6 | } 7 | 8 | var result = make([]int, 0) 9 | 10 | viewRightSide(&result, root, 0) 11 | 12 | return result 13 | } 14 | 15 | func viewRightSide(result *[]int, root *TreeNode, depth int) { 16 | if root == nil { 17 | return 18 | } 19 | 20 | if len(*result) <= depth { 21 | *result = append(*result, root.Val) 22 | } 23 | 24 | viewRightSide(result, root.Right, depth + 1) 25 | viewRightSide(result, root.Left, depth + 1) 26 | } 27 | -------------------------------------------------------------------------------- /solutions/233.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func countDigitOne(n int) int { 4 | if n <= 0 { 5 | return 0 6 | } 7 | 8 | if n <= 9 { 9 | return 1 10 | } 11 | 12 | count := count(n) 13 | current := count 14 | 15 | if n < 2 * count && n >= count { 16 | current = n - count + 1 17 | } 18 | 19 | return n / count * countDigitOne(count - 1) + current + countDigitOne(n % count) 20 | } 21 | 22 | func count(n int) int { 23 | result := 1 24 | 25 | for n > 0 { 26 | result *= 10 27 | n /= 10 28 | } 29 | 30 | return result / 10 31 | } 32 | -------------------------------------------------------------------------------- /solutions/138.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func copyRandomList(head *Node) *Node { 4 | if head == nil { 5 | return head 6 | } 7 | 8 | nodeMap := make(map[*Node]*Node) 9 | dummy := head 10 | 11 | for ; dummy != nil; dummy = dummy.Next { 12 | nodeMap[dummy] = &Node{Val: dummy.Val} 13 | } 14 | 15 | for dummy = head; dummy != nil; dummy = dummy.Next { 16 | newNode, newNextNode, newRandNode := nodeMap[dummy], nodeMap[dummy.Next], nodeMap[dummy.Random] 17 | newNode.Next, newNode.Random = newNextNode, newRandNode 18 | } 19 | 20 | return nodeMap[head] 21 | } 22 | -------------------------------------------------------------------------------- /solutions/324.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func wiggleSort(nums []int) { 8 | if len(nums) <= 1 { 9 | return 10 | } 11 | 12 | sort.Ints(nums) 13 | 14 | middle := len(nums) / 2 + len(nums) % 2 15 | 16 | var result []int 17 | 18 | for i := 0; i < middle; i++ { 19 | result = append(result, nums[middle - i - 1]) 20 | 21 | if middle + i < len(nums){ 22 | result = append(result, nums[len(nums) - i - 1]) 23 | } 24 | } 25 | 26 | for i := 0; i < len(result); i++ { 27 | nums[i] = result[i] 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /solutions/382.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "math/rand" 5 | ) 6 | 7 | type Solution struct { 8 | node *ListNode 9 | } 10 | 11 | func Constructor(head *ListNode) Solution { 12 | return Solution{node: head} 13 | } 14 | 15 | func (this *Solution) GetRandom() int { 16 | result := this.node.Val 17 | pointer := this.node 18 | counter := 1 19 | 20 | for pointer.Next != nil { 21 | if rand.Intn(counter + 1) == 0 { 22 | result = pointer.Next.Val 23 | } 24 | 25 | counter++ 26 | pointer = pointer.Next 27 | } 28 | 29 | return result 30 | } 31 | -------------------------------------------------------------------------------- /solutions/219.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func containsNearbyDuplicate(nums []int, k int) bool { 4 | index := make(map[int]int, k) 5 | 6 | for i, n := range nums { 7 | if i > k { 8 | obsoleteIndex := i - k - 1 9 | 10 | if index[nums[obsoleteIndex]] == obsoleteIndex { 11 | delete(index, nums[obsoleteIndex]) 12 | } 13 | } 14 | 15 | if previous, ok := index[n]; ok { 16 | if i - previous <= k { 17 | return true 18 | } 19 | } 20 | 21 | index[n] = i 22 | } 23 | 24 | return false 25 | } 26 | -------------------------------------------------------------------------------- /solutions/345.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func reverseVowels(s string) string { 4 | result := []byte(s) 5 | 6 | for i, j := 0, len(s) - 1; i < len(s) && j > i ; i++ { 7 | if isVowel(s[i]) { 8 | for j >= i && !isVowel(s[j]) { 9 | j-- 10 | } 11 | 12 | result[i], result[j] = result[j], result[i] 13 | j-- 14 | } 15 | } 16 | 17 | return string(result) 18 | } 19 | 20 | func isVowel(c byte) bool { 21 | return c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' || c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' 22 | } 23 | -------------------------------------------------------------------------------- /solutions/264.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func nthUglyNumber(n int) int { 4 | if n == 1 { 5 | return n 6 | } 7 | 8 | fives, threes, twos := 0, 0, 0 9 | dp := make([]int, n) 10 | dp[0] = 1 11 | 12 | for i := 1; i < n; i++ { 13 | dp[i] = min(dp[twos] * 2, min(dp[threes] * 3, dp[fives] * 5)) 14 | 15 | if dp[i] == dp[twos] * 2 { 16 | twos++ 17 | } 18 | 19 | if dp[i] == dp[threes] * 3 { 20 | threes++ 21 | } 22 | 23 | if dp[i] == dp[fives] * 5 { 24 | fives++ 25 | } 26 | } 27 | 28 | return dp[n - 1] 29 | } 30 | -------------------------------------------------------------------------------- /solutions/5.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func longestPalindrome(s string) string { 4 | length := len(s) 5 | 6 | if length <= 1 { 7 | return s 8 | } 9 | 10 | var begin, max int 11 | 12 | for i := 0; i < length; { 13 | left, right := i, i 14 | 15 | for right < length - 1 && s[right] == s[right + 1] { 16 | right++ 17 | } 18 | 19 | i = right + 1 20 | 21 | for left > 0 && right < length - 1 && s[left - 1] == s[right + 1] { 22 | left-- 23 | right++ 24 | } 25 | 26 | if max < right - left { 27 | begin = left 28 | max = right - left 29 | } 30 | } 31 | 32 | return s[begin : begin + max + 1] 33 | } 34 | -------------------------------------------------------------------------------- /solutions/71.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "strings" 5 | ) 6 | 7 | func simplifyPath(path string) string { 8 | var result []string 9 | parts := strings.Split(path, "/") 10 | 11 | for _, part := range parts { 12 | if part == "" || part == "." { 13 | continue 14 | } 15 | 16 | if part == ".." { 17 | if len(result) > 0 { 18 | result = result[:len(result) - 1] 19 | } 20 | 21 | continue 22 | } 23 | 24 | result = append(result, part) 25 | } 26 | 27 | return "/" + strings.Join(result, "/") 28 | } 29 | -------------------------------------------------------------------------------- /solutions/21.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode { 4 | dummy := new(ListNode) 5 | 6 | for node := dummy; l1 != nil || l2 != nil; node = node.Next { 7 | if l1 == nil { 8 | node.Next = l2 9 | 10 | break 11 | } else if l2 == nil { 12 | node.Next = l1 13 | 14 | break 15 | } else if l1.Val < l2.Val { 16 | node.Next = l1 17 | 18 | l1 = l1.Next 19 | } else { 20 | node.Next = l2 21 | 22 | l2 = l2.Next 23 | } 24 | } 25 | 26 | return dummy.Next 27 | } 28 | -------------------------------------------------------------------------------- /solutions/448.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func findDisappearedNumbers(nums []int) []int { 4 | for i := 0; i < len(nums); i++ { 5 | for nums[i] != i + 1 { 6 | current := nums[i] 7 | 8 | if nums[current - 1] == current { 9 | break 10 | } else { 11 | nums[i], nums[current - 1] = nums[current - 1], nums[i] 12 | } 13 | } 14 | } 15 | 16 | var result []int 17 | 18 | for i, value := range nums { 19 | if value != i + 1 { 20 | result = append(result, i + 1) 21 | } 22 | } 23 | 24 | return result 25 | } 26 | -------------------------------------------------------------------------------- /solutions/52.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func totalNQueens(n int) int { 4 | count := 0 5 | 6 | backtrackQueens(&count, n, 0, 0, 0, 0) 7 | 8 | return count 9 | } 10 | 11 | func backtrackQueens(count *int, n int, row int, column int, left int, right int) { 12 | if row == n { 13 | *count++ 14 | 15 | return 16 | } 17 | 18 | for i := 0; i < n; i++ { 19 | bit := 1 << uint(n - i - 1) 20 | 21 | if column & bit == 0 && left & bit == 0 && right & bit == 0 { 22 | backtrackQueens(count, n, row + 1, column | bit, (left | bit) << 1, (right | bit) >> 1) 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /solutions/22.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func generateParenthesis(n int) []string { 4 | result := make([]string, 0) 5 | 6 | generatePairs("(", n - 1, n, &result) 7 | 8 | return result 9 | } 10 | 11 | func generatePairs(str string, left, right int, result *[]string) { 12 | if left > right { 13 | return 14 | } 15 | 16 | if left > 0 { 17 | generatePairs(str + "(", left - 1, right, result) 18 | } 19 | 20 | if right > 0 { 21 | generatePairs(str + ")", left, right - 1, result) 22 | } 23 | 24 | if left == 0 && right == 0 { 25 | *result = append(*result, str) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /solutions/86.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func partition(head *ListNode, x int) *ListNode { 4 | if head == nil { 5 | return head 6 | } 7 | 8 | smaller, greater := &ListNode{0, nil}, &ListNode{0, nil} 9 | left, right := smaller, greater 10 | 11 | for head != nil { 12 | if head.Val < x { 13 | left.Next = head 14 | left = left.Next 15 | } else { 16 | right.Next = head 17 | right = right.Next 18 | } 19 | 20 | head = head.Next 21 | } 22 | 23 | right.Next = nil 24 | left.Next = greater.Next 25 | 26 | return smaller.Next 27 | } 28 | -------------------------------------------------------------------------------- /solutions/216.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func combinationSum3(k int, n int) [][]int { 4 | var result [][]int 5 | var current []int 6 | 7 | searchCombination(k, n, 1, current, &result) 8 | 9 | return result 10 | } 11 | 12 | func searchCombination(k, n, depth int, current []int, result *[][]int) { 13 | if k == 0 && n == 0 { 14 | *result = append(*result, append([]int{}, current...)) 15 | 16 | return 17 | } 18 | 19 | if k < 0 || n < 0 { 20 | return 21 | } 22 | 23 | for i := depth; i <= 9; i++ { 24 | searchCombination(k - 1, n - i, i + 1, append(current, i), result) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /solutions/437.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func pathSum(root *TreeNode, sum int) int { 4 | return pathSumHelper(root, 0, sum, map[int]int{0: 1}) 5 | } 6 | 7 | func pathSumHelper(node *TreeNode, currentSum, target int, valueMap map[int]int) int { 8 | if node == nil { 9 | return 0 10 | } 11 | 12 | currentSum += node.Val 13 | summary := valueMap[currentSum - target] 14 | valueMap[currentSum]++ 15 | 16 | summary += pathSumHelper(node.Left, currentSum, target, valueMap) 17 | summary += pathSumHelper(node.Right, currentSum, target, valueMap) 18 | 19 | valueMap[currentSum]-- 20 | 21 | return summary 22 | } 23 | -------------------------------------------------------------------------------- /solutions/438.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func findAnagrams(s string, p string) []int { 4 | hash := 0 5 | 6 | for i := 0; i < len(p); i++ { 7 | hash += 1 << (p[i] - 'a') 8 | } 9 | 10 | var result []int 11 | i, j, currentHash := 0, 0, 0 12 | 13 | for j < len(s) { 14 | currentHash += 1 << (s[j] - 'a') 15 | 16 | if j - i + 1 == len(p) { 17 | if currentHash == hash { 18 | result = append(result, i) 19 | } 20 | 21 | currentHash -= 1 << (s[i] - 'a') 22 | i++ 23 | } 24 | 25 | j++ 26 | } 27 | 28 | return result 29 | } 30 | -------------------------------------------------------------------------------- /solutions/106.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func buildTree(inorder []int, postorder []int) *TreeNode { 4 | if len(inorder) == 0 || len(postorder) == 0 { 5 | return nil 6 | } 7 | 8 | value := postorder[len(postorder) - 1] 9 | root := &TreeNode{ 10 | Val: value, 11 | } 12 | 13 | var item int 14 | for item = 0; item < len(inorder); item++ { 15 | if inorder[item] == value { 16 | break 17 | } 18 | } 19 | 20 | root.Left = buildTree(inorder[:item], postorder[:item]) 21 | root.Right = buildTree(inorder[item + 1:], postorder[item: len(postorder) - 1]) 22 | 23 | return root 24 | } 25 | -------------------------------------------------------------------------------- /solutions/49.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func groupAnagrams(words []string) [][]string { 4 | cache := make(map[[26]byte]int) 5 | result := make([][]string, 0) 6 | 7 | for word := range words { 8 | list := [26]byte{} 9 | 10 | for letter := range words[word] { 11 | list[words[word][letter] - 'a']++ 12 | } 13 | 14 | if i, ok := cache[list]; ok { 15 | result[i] = append(result[i], words[word]) 16 | } else { 17 | result = append(result, []string{words[word]}) 18 | 19 | cache[list] = len(result) - 1 20 | } 21 | } 22 | 23 | return result 24 | } 25 | -------------------------------------------------------------------------------- /solutions/120.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func minimumTotal(triangle [][]int) int { 4 | for i := 0; i < len(triangle); i++ { 5 | for j := 0; j <= i; j++ { 6 | if i == 0 && j == 0 { 7 | continue 8 | } 9 | 10 | if j == 0 { 11 | triangle[i][j] += triangle[i - 1][j] 12 | } else if j == i { 13 | triangle[i][j] += triangle[i - 1][j - 1] 14 | } else { 15 | triangle[i][j] += min(triangle[i - 1][j], triangle[i - 1][j - 1]) 16 | } 17 | } 18 | } 19 | 20 | return minInArray(triangle[len(triangle) - 1]) 21 | } 22 | -------------------------------------------------------------------------------- /solutions/238.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func productExceptSelf(nums []int) []int { 4 | forward := make([]int, len(nums)) 5 | backward := make([]int, len(nums)) 6 | result := make([]int, len(nums)) 7 | 8 | for i := range nums { 9 | forward[i], backward[i] = 1, 1 10 | } 11 | 12 | for i := 1; i < len(nums); i++ { 13 | forward[i] = forward[i - 1] * nums[i - 1] 14 | } 15 | 16 | for i := len(nums) - 2; i >= 0; i-- { 17 | backward[i] = backward[i + 1] * nums[i + 1] 18 | } 19 | 20 | for i := range nums { 21 | result[i] = forward[i] * backward[i] 22 | } 23 | 24 | return result 25 | } 26 | -------------------------------------------------------------------------------- /solutions/3.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func lengthOfLongestSubstring(s string) int { 4 | var charPosition [256]int 5 | var maxLength, substringLength, repeatedPosition int 6 | 7 | for i := 0; i < len(s); i++ { 8 | if position := charPosition[s[i]]; position > 0 { 9 | maxLength = max(substringLength, maxLength) 10 | repeatedPosition = max(position, repeatedPosition) 11 | 12 | substringLength = i + 1 - repeatedPosition 13 | } else { 14 | substringLength += 1 15 | } 16 | 17 | charPosition[s[i]] = i + 1 18 | } 19 | 20 | return max(maxLength, substringLength) 21 | } 22 | -------------------------------------------------------------------------------- /solutions/406.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func reconstructQueue(people [][]int) [][]int { 8 | sort.Slice(people, func (i int, j int) bool { 9 | if people[i][0] == people[j][0] { 10 | return people[i][1] < people[j][1] 11 | } 12 | 13 | return people[i][0] > people[j][0] 14 | }) 15 | 16 | result := make([][]int, len(people)) 17 | 18 | for i := 0; i < len(people); i++ { 19 | result[i] = people[i] 20 | 21 | for j := people[i][1]; j < i; j++ { 22 | result[j], result[i] = result[i], result[j] 23 | } 24 | } 25 | 26 | return result 27 | } 28 | -------------------------------------------------------------------------------- /solutions/109.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func sortedListToBST(head *ListNode) *TreeNode { 4 | if head == nil { 5 | return nil 6 | } 7 | 8 | var previous *ListNode 9 | slow, fast := head, head 10 | 11 | for fast.Next != nil && fast.Next.Next != nil { 12 | previous = slow 13 | slow = slow.Next 14 | fast = fast.Next.Next 15 | } 16 | 17 | if previous != nil { 18 | previous.Next = nil 19 | } else { 20 | head = nil 21 | } 22 | 23 | return &TreeNode{ 24 | Val: slow.Val, 25 | Left: sortedListToBST(head), 26 | Right: sortedListToBST(slow.Next), 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /solutions/46.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func permute(nums []int) [][]int { 4 | if len(nums) == 1 { 5 | return [][]int{nums} 6 | } 7 | 8 | first, remaining := nums[0], permute(nums[1:]) 9 | length := len(remaining[0]) + 1 10 | result := make([][]int, len(remaining) * length) 11 | 12 | for i, permutation := range remaining { 13 | for j := 0; j < length; j++ { 14 | temp := append([]int{}, permutation[:j]...) 15 | temp = append(temp, first) 16 | temp = append(temp, permutation[j:]...) 17 | 18 | result[i * length + j] = temp 19 | } 20 | } 21 | 22 | return result 23 | } 24 | -------------------------------------------------------------------------------- /solutions/384.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "math/rand" 5 | ) 6 | 7 | type Solution struct { 8 | list []int 9 | } 10 | 11 | func Constructor(nums []int) Solution { 12 | return Solution{list: nums} 13 | } 14 | 15 | func (this *Solution) Reset() []int { 16 | return this.list 17 | } 18 | 19 | func (this *Solution) Shuffle() []int { 20 | shuffled := make([]int, len(this.list)) 21 | copy(shuffled, this.list) 22 | length := len(shuffled) 23 | 24 | for i := 0; i < length; i++ { 25 | j := rand.Intn(length - i) 26 | shuffled[i], shuffled[j + i] = shuffled[j + i], shuffled[i] 27 | } 28 | 29 | return shuffled 30 | } 31 | -------------------------------------------------------------------------------- /solutions/129.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func sumNumbers(root *TreeNode) int { 4 | return recursiveSumNumbers(root, 0) 5 | } 6 | 7 | func recursiveSumNumbers(node *TreeNode, sum int) int { 8 | if node == nil { 9 | return 0 10 | } 11 | 12 | sum = sum * 10 + node.Val 13 | 14 | if node.Left == nil && node.Right == nil { 15 | return sum 16 | } 17 | 18 | left := 0 19 | 20 | if node.Left != nil { 21 | left = recursiveSumNumbers(node.Left, sum) 22 | } 23 | 24 | right := 0 25 | 26 | if node.Right != nil { 27 | right = recursiveSumNumbers(node.Right, sum) 28 | } 29 | 30 | return left + right 31 | } 32 | -------------------------------------------------------------------------------- /solutions/24.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func swapPairs(head *ListNode) *ListNode { 4 | if head == nil || head.Next == nil { 5 | return head 6 | } 7 | 8 | dummy := head.Next 9 | 10 | var temp *ListNode 11 | var previous *ListNode 12 | 13 | for current := head; current != nil && current.Next != nil; current = current.Next.Next { 14 | temp = current.Next 15 | current.Next = current.Next.Next 16 | temp.Next = current 17 | current = temp 18 | 19 | if previous != nil { 20 | previous.Next = current 21 | } 22 | 23 | previous = current.Next 24 | } 25 | 26 | return dummy 27 | } 28 | -------------------------------------------------------------------------------- /solutions/42.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func trap(h []int) int { 4 | if len(h) < 3 { 5 | return 0 6 | } 7 | 8 | var result, left, right, maxLeft, maxRight int 9 | right = len(h) - 1 10 | 11 | for left < right { 12 | if h[left] > maxLeft { 13 | maxLeft = h[left] 14 | } 15 | 16 | if h[right] > maxRight { 17 | maxRight = h[right] 18 | } 19 | 20 | if maxLeft < maxRight { 21 | result += maxLeft - h[left] 22 | left++ 23 | } else { 24 | result += maxRight - h[right] 25 | right-- 26 | } 27 | } 28 | 29 | return result 30 | } 31 | -------------------------------------------------------------------------------- /solutions/378.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func kthSmallest(matrix [][]int, k int) int { 4 | rows, columns := len(matrix), len(matrix[0]) 5 | low, high := matrix[0][0], matrix[rows - 1][columns - 1] + 1 6 | 7 | for low < high { 8 | middle := low + (high - low) / 2 9 | count, i, j := 0, 0, columns - 1 10 | 11 | for ; i < rows; i++ { 12 | for j >= 0 && middle < matrix[i][j] { 13 | j-- 14 | } 15 | 16 | count += j + 1 17 | } 18 | 19 | if count < k { 20 | low = middle + 1 21 | } else { 22 | high = middle 23 | } 24 | } 25 | 26 | return low 27 | } 28 | -------------------------------------------------------------------------------- /solutions/372.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | var modulo = 1337 4 | 5 | func superPow(a int, b []int) int { 6 | if len(b) == 1 { 7 | return pow(a, b[0]) 8 | } 9 | 10 | result := superPow(a, b[:len(b) - 1]) 11 | result = pow(result, 10) 12 | result = result * pow(a, b[len(b) - 1]) % modulo 13 | 14 | return result 15 | } 16 | 17 | func pow(a int, b int) int { 18 | if a == 0 { 19 | return 0 20 | } 21 | 22 | if b == 0 { 23 | return 1 24 | } 25 | 26 | result := pow(a, b >> 1) 27 | result = result * result % modulo 28 | 29 | if b & 1 == 1 { 30 | result = result * a % modulo 31 | } 32 | 33 | return result 34 | } 35 | -------------------------------------------------------------------------------- /solutions/213.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func rob(nums []int) int { 4 | if len(nums) == 0{ 5 | return 0 6 | } 7 | 8 | if len(nums) == 1 { 9 | return nums[0] 10 | } 11 | 12 | include, exclude, temp := 0,0,0 13 | 14 | for i := 0; i < len(nums) - 1; i++ { 15 | temp = include 16 | include = max(include, exclude + nums[i]) 17 | exclude = temp 18 | } 19 | 20 | currentMax := include 21 | include, exclude, temp = 0,0,0 22 | 23 | for i := 1; i < len(nums); i++ { 24 | temp = include 25 | include = max(include, exclude + nums[i]) 26 | exclude = temp 27 | } 28 | 29 | return max(currentMax, include) 30 | } 31 | -------------------------------------------------------------------------------- /solutions/232.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | type MyQueue struct { 4 | array []int 5 | } 6 | 7 | func Constructor() MyQueue { 8 | return MyQueue{} 9 | } 10 | 11 | func (this *MyQueue) Push(x int) { 12 | this.array = append(this.array, x) 13 | } 14 | 15 | func (this *MyQueue) Pop() int { 16 | if this.Empty() { 17 | return 0 18 | } 19 | 20 | top := this.Peek() 21 | this.array = this.array[1: len(this.array)] 22 | 23 | return top 24 | } 25 | 26 | func (this *MyQueue) Peek() int { 27 | if this.Empty() { 28 | return 0 29 | } 30 | 31 | return this.array[0] 32 | } 33 | 34 | func (this *MyQueue) Empty() bool { 35 | return len(this.array) == 0 36 | } 37 | -------------------------------------------------------------------------------- /solutions/60.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "strconv" 5 | ) 6 | 7 | func getPermutation(n int, k int) string { 8 | list := make([]string, n) 9 | factorials := make([]int, n + 1) 10 | factorials[0] = 1 11 | 12 | for i := 1; i <= n; i++ { 13 | list[i - 1] = strconv.Itoa(i) 14 | 15 | factorials[i] = i * factorials[i - 1] 16 | } 17 | 18 | result := "" 19 | k = k - 1 20 | 21 | for i := 1; i <= n; i++ { 22 | position := k / factorials[n - i] 23 | 24 | result = result + list[position] 25 | list = append(list[:position], list[position + 1:]...) 26 | 27 | k = k % factorials[n - i] 28 | } 29 | 30 | return result 31 | } 32 | -------------------------------------------------------------------------------- /solutions/228.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "strconv" 5 | ) 6 | 7 | func summaryRanges(nums []int) []string { 8 | if len(nums) == 0 { 9 | return nil 10 | } 11 | 12 | var result []string 13 | head := 0 14 | 15 | for i := range nums { 16 | if i < len(nums) - 1 && nums[i] + 1 == nums[i + 1] { 17 | continue 18 | } 19 | 20 | if head == i { 21 | result = append(result, strconv.Itoa(nums[i])) 22 | } else { 23 | current := strconv.Itoa(nums[head]) + "->" + strconv.Itoa(nums[i]) 24 | result = append(result, current) 25 | } 26 | 27 | head = i + 1 28 | } 29 | 30 | return result 31 | } 32 | -------------------------------------------------------------------------------- /solutions/20.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func isValid(s string) bool { 4 | if s == "" { 5 | return true 6 | } 7 | 8 | stack := make([]rune, 0, 1) 9 | brackets := map[rune]rune{ 10 | '}': '{', 11 | ')': '(', 12 | ']': '[', 13 | } 14 | 15 | for _, char := range s { 16 | switch char { 17 | case '(', '[', '{': 18 | stack = append(stack, char) 19 | case ')', ']', '}': 20 | if len(stack) == 0 || stack[len(stack) - 1] != brackets[char] { 21 | return false 22 | } else { 23 | stack = stack[:len(stack) - 1] 24 | } 25 | } 26 | } 27 | 28 | return len(stack) == 0 29 | } 30 | -------------------------------------------------------------------------------- /solutions/386.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func lexicalOrder(n int) []int { 4 | result := []int{1} 5 | number := 1 6 | 7 | for i := 1; i < n; i++ { 8 | number = next(number, n) 9 | result = append(result, number) 10 | } 11 | 12 | return result 13 | } 14 | 15 | func next(i int, n int) int { 16 | if i * 10 <= n { 17 | return i * 10 18 | } 19 | 20 | if i + 1 <= n && i % 10 < 9 { 21 | return i + 1 22 | } 23 | 24 | if i % 10 == 9 { 25 | i++ 26 | } else if i + 1 > n { 27 | i = i / 10 + 1 28 | } else { 29 | panic("screwed") 30 | } 31 | 32 | for i % 10 == 0 { 33 | i = i / 10 34 | } 35 | 36 | return i 37 | } 38 | -------------------------------------------------------------------------------- /solutions/414.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "math" 5 | ) 6 | 7 | func thirdMax(nums []int) int { 8 | max, second, third := math.MinInt64, math.MinInt64, math.MinInt64 9 | 10 | for _, value := range nums { 11 | if value == max || value == second || value == third { 12 | continue 13 | } 14 | 15 | switch { 16 | case value > max: 17 | max, second, third = value, max, second 18 | case value > second: 19 | second, third = value, second 20 | case value > third: 21 | third = value 22 | } 23 | } 24 | 25 | if third == math.MinInt64 { 26 | return max 27 | } 28 | 29 | return third 30 | } 31 | -------------------------------------------------------------------------------- /solutions/443.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func compress(chars []byte) int { 8 | scan, write := 0, 0 9 | length := len(chars) 10 | 11 | for scan < length { 12 | count := 0 13 | chars[write] = chars[scan] 14 | 15 | for scan < length && chars[write] == chars[scan] { 16 | count++ 17 | scan++ 18 | } 19 | 20 | if count > 1 { 21 | current := fmt.Sprintf("%d", count) 22 | 23 | for _, character := range []byte(current) { 24 | write++ 25 | chars[write] = character 26 | } 27 | } 28 | 29 | write++ 30 | } 31 | 32 | return write 33 | } 34 | -------------------------------------------------------------------------------- /solutions/77.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func combine(n int, k int) [][]int { 4 | var result [][]int 5 | 6 | for i := 1; i <= n-k + 1; i++ { 7 | backtrackCombinations(&result, []int{i}, n, k) 8 | } 9 | 10 | return result 11 | } 12 | 13 | func backtrackCombinations(result *[][]int, previous []int, n, k int) { 14 | if len(previous) == k { 15 | *result = append(*result, previous) 16 | 17 | return 18 | } 19 | 20 | if previous[len(previous) - 1] == n { 21 | return 22 | } 23 | 24 | for v := previous[len(previous) - 1]; v < n && n - v >= k - len(previous); v++ { 25 | backtrackCombinations(result, append(append([]int{}, previous...), v + 1), n, k) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /solutions/95.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func generateTrees(n int) []*TreeNode { 4 | if n == 0 { 5 | return []*TreeNode{} 6 | } 7 | 8 | return generateTree(1, n) 9 | } 10 | 11 | func generateTree(start, end int) []*TreeNode { 12 | if start > end { 13 | return []*TreeNode{nil} 14 | } 15 | 16 | var result []*TreeNode 17 | 18 | for s := start; s <= end; s++ { 19 | left := generateTree(start, s - 1) 20 | right := generateTree(s + 1, end) 21 | 22 | for _, l := range left { 23 | for _, r := range right { 24 | result = append(result, &TreeNode{Val: s, Left: l, Right: r}) 25 | } 26 | } 27 | } 28 | 29 | return result 30 | } 31 | -------------------------------------------------------------------------------- /solutions/128.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func longestConsecutive(nums []int) int { 4 | temp := map[int]int{} 5 | result := 0 6 | 7 | for _, number := range nums { 8 | if _, ok := temp[number]; !ok { 9 | left := temp[number - 1] 10 | right := temp[number + 1] 11 | 12 | temp[number] = 1 + left + right 13 | 14 | if left > 0 { 15 | temp[number-left] = temp[number] 16 | } 17 | 18 | if right > 0 { 19 | temp[number+right] = temp[number] 20 | } 21 | 22 | if temp[number] > result { 23 | result = temp[number] 24 | } 25 | } 26 | } 27 | 28 | return result 29 | } 30 | -------------------------------------------------------------------------------- /solutions/13.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func romanToInt(s string) int { 4 | if len(s) == 0 { 5 | return 0 6 | } 7 | 8 | var roman = map[rune]int{ 9 | 'I': 1, 10 | 'V': 5, 11 | 'X': 10, 12 | 'L': 50, 13 | 'C': 100, 14 | 'D': 500, 15 | 'M': 1000, 16 | } 17 | 18 | previous := rune(s[len(s) - 1]) 19 | integer := roman[previous] 20 | 21 | for i := len(s) - 2; i >= 0; i-- { 22 | char := rune(s[i]) 23 | 24 | if roman[previous] <= roman[char] { 25 | integer += roman[char] 26 | } else { 27 | integer -= roman[char] 28 | } 29 | 30 | previous = char 31 | } 32 | 33 | return integer 34 | } 35 | -------------------------------------------------------------------------------- /solutions/160.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func getIntersectionNode(headA, headB *ListNode) *ListNode { 4 | if headA == nil || headB == nil { 5 | return nil 6 | } 7 | 8 | nodeA, nodeB := headA, headB 9 | 10 | for nodeA != nil || nodeB != nil { 11 | if nodeA == nodeB { 12 | return nodeA 13 | } 14 | 15 | if nodeA == nil { 16 | nodeA = headB 17 | nodeB = nodeB.Next 18 | 19 | continue 20 | } 21 | 22 | if nodeB == nil { 23 | nodeB = headA 24 | nodeA = nodeA.Next 25 | 26 | continue 27 | } 28 | 29 | nodeA = nodeA.Next 30 | nodeB = nodeB.Next 31 | } 32 | 33 | return nil 34 | } 35 | -------------------------------------------------------------------------------- /solutions/32.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func longestValidParentheses(s string) int { 4 | var stack = make([]int, len(s) + 1) 5 | currentStart := -1 6 | currentIndex := 0 7 | result := 0 8 | 9 | for i := 0; i < len(s); i++ { 10 | if s[i] == '(' { 11 | stack[currentIndex] = currentStart 12 | currentStart = i 13 | currentIndex++ 14 | } else if currentIndex == 0 { 15 | currentStart = i 16 | } else { 17 | currentIndex-- 18 | currentStart = stack[currentIndex] 19 | 20 | if i - currentStart > result { 21 | result = i - currentStart 22 | } 23 | } 24 | } 25 | 26 | return result 27 | } 28 | -------------------------------------------------------------------------------- /solutions/328.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func oddEvenList(head *ListNode) *ListNode { 4 | if head == nil || head.Next == nil { 5 | return head 6 | } 7 | 8 | odd, even, current := head, head.Next, head.Next.Next 9 | currentOdd, currentEven := odd, even 10 | isOdd := true 11 | 12 | for current != nil { 13 | if isOdd { 14 | currentOdd.Next = current 15 | currentOdd = currentOdd.Next 16 | } else { 17 | currentEven.Next = current 18 | currentEven = currentEven.Next 19 | } 20 | 21 | isOdd = !isOdd 22 | current = current.Next 23 | } 24 | 25 | currentEven.Next = nil 26 | currentOdd.Next = even 27 | 28 | return odd 29 | } 30 | -------------------------------------------------------------------------------- /solutions/6.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "bytes" 5 | ) 6 | 7 | func convert(s string, numRows int) string { 8 | if numRows == 1 { 9 | return s 10 | } 11 | 12 | var result bytes.Buffer 13 | 14 | for i := 0; i < numRows; i++ { 15 | for j := i; j < len(s); { 16 | result.WriteByte(s[j]) 17 | 18 | j += numRows * 2 - 2 19 | 20 | if i == 0 || i == numRows - 1 { 21 | continue 22 | } 23 | 24 | j -= i + i 25 | 26 | if j >= len(s) { 27 | break 28 | } 29 | 30 | result.WriteByte(s[j]) 31 | 32 | j += i + i 33 | } 34 | } 35 | 36 | return result.String() 37 | } 38 | -------------------------------------------------------------------------------- /solutions/97.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func isInterleave(s1, s2, s3 string) bool { 4 | if len(s3) != len(s1) + len(s2) { 5 | return false 6 | } 7 | 8 | var dp [][]bool 9 | 10 | for i := 0; i < len(s3) + 1; i++ { 11 | dp = append(dp, make([]bool, len(s1) + 1)) 12 | } 13 | 14 | dp[0][0] = true 15 | 16 | for i := 1; i <= len(s3); i++ { 17 | for j := 0; j <= i; j++ { 18 | k := i - j 19 | 20 | if j > len(s1) || k > len(s2) { 21 | continue 22 | } 23 | 24 | dp[i][j] = (j-1 >= 0 && dp[i-1][j-1] && s3[i-1] == s1[j-1]) || (k-1 >= 0 && dp[i-1][j] && s3[i-1] == s2[k-1]) 25 | } 26 | } 27 | 28 | return dp[len(s3)][len(s1)] 29 | } 30 | -------------------------------------------------------------------------------- /solutions/313.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "math" 5 | ) 6 | 7 | func nthSuperUglyNumber(n int, primes []int) int { 8 | if n <= 1 || len(primes) == 0 { 9 | return 1 10 | } 11 | 12 | dp := make([]int, n) 13 | dp[0] = 1 14 | numbers := make([]int, len(primes)) 15 | 16 | for i := 1; i < n; i++ { 17 | max := math.MaxInt32 18 | 19 | for index, prime := range primes { 20 | max = min(max, dp[numbers[index]] * prime) 21 | } 22 | 23 | dp[i] = max 24 | 25 | for index, prime := range primes { 26 | if max == dp[numbers[index]] * prime { 27 | numbers[index]++ 28 | } 29 | } 30 | } 31 | 32 | return dp[n - 1] 33 | } 34 | -------------------------------------------------------------------------------- /solutions/132.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func minCut(s string) int { 4 | length := len(s) 5 | cut := make([]int, length) 6 | checked := make([][]bool, length) 7 | 8 | for i := range checked { 9 | checked[i] = make([]bool, length) 10 | } 11 | 12 | for i := 0; i < length; i++ { 13 | cut[i] = i 14 | 15 | for j :=0; j <= i; j++ { 16 | if s[j] == s[i] && (i - j <= 1 || checked[j + 1][i - 1]) { 17 | checked[j][i] = true 18 | 19 | if j > 0 { 20 | cut[i] = min(cut[i],cut[j - 1] + 1) 21 | } else { 22 | cut[i] = 0 23 | } 24 | } 25 | } 26 | } 27 | 28 | return cut[length - 1] 29 | } 30 | -------------------------------------------------------------------------------- /solutions/225.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | type MyStack struct { 4 | queue []int 5 | } 6 | 7 | func Constructor() MyStack { 8 | return MyStack{} 9 | } 10 | 11 | func (this *MyStack) Push(x int) { 12 | this.queue = append(this.queue, x) 13 | 14 | for i := 1; i < len(this.queue); i++ { 15 | top := this.queue[0] 16 | 17 | this.queue = this.queue[1:] 18 | this.queue = append(this.queue, top) 19 | } 20 | } 21 | 22 | func (this *MyStack) Pop() int { 23 | top := this.queue[0] 24 | 25 | this.queue = this.queue[1:] 26 | 27 | return top 28 | } 29 | 30 | func (this *MyStack) Top() int { 31 | return this.queue[0] 32 | } 33 | 34 | func (this *MyStack) Empty() bool { 35 | return len(this.queue) == 0 36 | } 37 | -------------------------------------------------------------------------------- /solutions/447.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func numberOfBoomerangs(points [][]int) int { 4 | result := 0 5 | 6 | for i := 0; i < len(points); i++ { 7 | dictionary := make(map[int]int) 8 | 9 | for j := 0; j < len(points); j++ { 10 | distance := calculateDistance(points[i], points[j]) 11 | 12 | if value, ok := dictionary[distance]; !ok { 13 | dictionary[distance]++ 14 | } else { 15 | result += 2 * value 16 | dictionary[distance]++ 17 | } 18 | } 19 | } 20 | 21 | return result 22 | } 23 | 24 | func calculateDistance(a []int, b []int) int { 25 | x := a[0] - b[0] 26 | y := a[1] - b[1] 27 | 28 | return x * x + y * y 29 | } 30 | -------------------------------------------------------------------------------- /solutions/312.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func maxCoins(nums []int) int { 4 | length := len(nums) 5 | nums = append([]int{1}, append(nums, 1)...) 6 | dp := make([][]int, length + 2) 7 | 8 | for i := range dp { 9 | dp[i] = make([]int, length + 2) 10 | } 11 | 12 | for l := 1; l <= length; l++ { 13 | for i := 1; i <= length - l + 1; i++ { 14 | j := i + l - 1 15 | 16 | for k := i; k <= j; k++ { 17 | dp[i][j] = max(dp[i][j], dp[i][k - 1] + nums[i - 1] * nums[k] * nums[j + 1] + dp[k + 1][j]) 18 | } 19 | } 20 | } 21 | 22 | return dp[1][length] 23 | } 24 | 25 | func max(a, b int) int { 26 | if a > b { 27 | return a 28 | } 29 | 30 | return b 31 | } 32 | -------------------------------------------------------------------------------- /solutions/375.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "math" 5 | ) 6 | 7 | func getMoneyAmount(n int) int { 8 | dp := make([][]int, n + 1) 9 | 10 | for i := 1; i <= n; i ++{ 11 | dp[i] = make([]int, n + 1) 12 | } 13 | 14 | for i := 1; i < n; i ++{ 15 | dp[i][i + 1] = i 16 | } 17 | 18 | for k := 2; k < n; k ++{ 19 | for i := 1; i + k <= n; i ++{ 20 | min := math.MaxInt32 21 | 22 | for j := i + 1; j <= i + k - 1; j ++ { 23 | if current := max(dp[i][j - 1], dp[j + 1][i + k]) + j; current < min { 24 | min = current 25 | } 26 | } 27 | 28 | dp[i][i + k] = min 29 | } 30 | } 31 | 32 | return dp[1][n] 33 | } 34 | -------------------------------------------------------------------------------- /solutions/331.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "strings" 5 | ) 6 | 7 | func isValidSerialization(preorder string) bool { 8 | if len(preorder) == 0 { 9 | return false 10 | } 11 | 12 | nodes := strings.Split(preorder, ",") 13 | currentLevel, nextLevel, current := 1, 0, 0 14 | 15 | for i, value := range nodes { 16 | current++ 17 | 18 | if value != "#" { 19 | nextLevel += 2 20 | } 21 | 22 | if current == currentLevel { 23 | if nextLevel == 0 && i == len(nodes) - 1 { 24 | return true 25 | } 26 | 27 | currentLevel = nextLevel 28 | nextLevel = 0 29 | current = 0 30 | } 31 | } 32 | 33 | return false 34 | } 35 | -------------------------------------------------------------------------------- /solutions/90.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func subsetsWithDup(nums []int) [][]int { 8 | result := make([][]int, 0) 9 | 10 | sort.Ints(nums) 11 | 12 | backtrackSubsetsWithDuplicated(nums, []int{}, &result) 13 | 14 | return result 15 | } 16 | 17 | func backtrackSubsetsWithDuplicated(nums, path []int, result *[][]int) { 18 | *result = append(*result, append([]int{}, path...)) 19 | 20 | for i := 0; i < len(nums); { 21 | path = append(path, nums[i]) 22 | 23 | backtrackSubsetsWithDuplicated(nums[i + 1:], path, result) 24 | 25 | path = path[:len(path) - 1] 26 | 27 | i++ 28 | 29 | for i < len(nums) && nums[i] == nums[i - 1] { 30 | i++ 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /solutions/202.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func isHappy(n int) bool { 4 | set := make(map[int]struct{}) 5 | 6 | return isHappyNumber(set, n) 7 | } 8 | 9 | func isHappyNumber(set map[int]struct{}, number int) bool { 10 | if number == 1 { 11 | return true 12 | } 13 | 14 | if _, ok := set[number]; ok { 15 | return false 16 | } else { 17 | set[number] = struct{}{} 18 | } 19 | 20 | remaining := 0 21 | 22 | for { 23 | if number/ 10 == 0 { 24 | remaining += (number % 10) * (number % 10) 25 | 26 | break 27 | } else { 28 | remaining += (number % 10) * (number % 10) 29 | number /= 10 30 | } 31 | } 32 | 33 | return isHappyNumber(set, remaining) 34 | } 35 | -------------------------------------------------------------------------------- /solutions/240.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func searchMatrix(matrix [][]int, target int) bool { 4 | if len(matrix) == 0 || len(matrix[0]) == 0 { 5 | return false 6 | } 7 | 8 | row, column := len(matrix) - 1, 0 9 | 10 | for { 11 | if !isInbounds(matrix, row, column) { 12 | return false 13 | } 14 | 15 | if value := matrix[row][column]; value == target { 16 | return true 17 | } else if value < target { 18 | column += 1 19 | } else if value > target { 20 | row -= 1 21 | } else { 22 | return false 23 | } 24 | } 25 | } 26 | 27 | func isInbounds(matrix [][]int, row int, column int) bool { 28 | return row >= 0 && column < len(matrix[row]) 29 | } 30 | -------------------------------------------------------------------------------- /solutions/64.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func minPathSum(grid [][]int) int { 4 | rowLength := len(grid) 5 | columnLength := len(grid[0]) 6 | 7 | for i := 1; i < columnLength; i++ { 8 | grid[0][i] += grid[0][i - 1] 9 | } 10 | 11 | for i := 1; i < rowLength; i++ { 12 | grid[i][0] += grid[i - 1][0] 13 | } 14 | 15 | for i := 1; i < rowLength; i++ { 16 | for j := 1; j < columnLength; j++ { 17 | left := grid[i][j - 1] + grid[i][j] 18 | up := grid[i - 1][j] + grid[i][j] 19 | 20 | if left > up { 21 | grid[i][j] = up 22 | } else { 23 | grid[i][j] = left 24 | } 25 | } 26 | } 27 | 28 | return grid[rowLength - 1][columnLength - 1] 29 | } 30 | -------------------------------------------------------------------------------- /solutions/415.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func addStrings(num1 string, num2 string) string { 4 | firstString, secondString := []byte(num1), []byte(num2) 5 | 6 | if len(firstString) < len(secondString) { 7 | firstString, secondString = secondString, firstString 8 | } 9 | 10 | sum := byte(0) 11 | 12 | for i, j := len(firstString) - 1, len(secondString) - 1; i >= 0 || j >= 0; i, sum = i - 1, sum / 10 { 13 | if j >= 0 { 14 | sum += secondString[j] - '0' 15 | j-- 16 | } 17 | 18 | sum += firstString[i] - '0' 19 | firstString[i] = sum % 10 + '0' 20 | } 21 | 22 | if (sum) != byte(0) { 23 | firstString = append([]byte{'1'}, firstString...) 24 | } 25 | 26 | return string(firstString) 27 | } 28 | -------------------------------------------------------------------------------- /solutions/400.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "strconv" 5 | ) 6 | 7 | func findNthDigit(n int) int { 8 | table := make([]int, 10) 9 | table[0] = 9 10 | current := 9 11 | 12 | for i := 1; i < 10; i ++{ 13 | current = current * 10 14 | table[i] = table[i - 1] + current * (i + 1) 15 | } 16 | 17 | length := 0 18 | current = 1 19 | 20 | for ; length < 10; length++{ 21 | if n <= table[length] { 22 | break 23 | } 24 | 25 | current = current * 10 26 | } 27 | 28 | if length == 0 { 29 | return n 30 | } 31 | 32 | last := table[length - 1] 33 | number := strconv.Itoa((n - last- 1) / (length + 1) + current) 34 | 35 | return int(number[(n - last- 1) % (length + 1)] - '0') 36 | } 37 | -------------------------------------------------------------------------------- /solutions/56.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func merge(intervals [][]int) [][]int { 8 | var result [][]int 9 | 10 | sort.Slice(intervals, func(i, j int) bool { 11 | return intervals[i][0] < intervals[j][0] 12 | }) 13 | 14 | for i, interval := range intervals { 15 | if i == 0 { 16 | result = append(result, interval) 17 | 18 | continue 19 | } 20 | 21 | if len(result) == 0 { 22 | break 23 | } 24 | 25 | end := result[len(result) - 1] 26 | 27 | if interval[0] > end[1] { 28 | result = append(result, interval) 29 | } else if interval[1] > end[1] { 30 | end[1] = interval[1] 31 | } 32 | } 33 | 34 | return result 35 | } 36 | -------------------------------------------------------------------------------- /solutions/16.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func threeSumClosest(nums []int, target int) int { 8 | sort.Ints(nums) 9 | 10 | sum := nums[0] + nums[1] + nums[2] 11 | result := sum 12 | difference := abs(result - target) 13 | length := len(nums) 14 | 15 | for i := 0; i < length - 2; i++ { 16 | for j, k := i + 1, length - 1; j < k; { 17 | sum = nums[i] + nums[j] + nums[k] 18 | 19 | if sum - target < 0 { 20 | j++ 21 | } else { 22 | k-- 23 | } 24 | 25 | if difference > abs(sum - target) { 26 | difference = abs(sum - target) 27 | result = sum 28 | } 29 | } 30 | } 31 | 32 | return result 33 | } 34 | -------------------------------------------------------------------------------- /solutions/2.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode { 4 | resultNode := ListNode{0, nil} 5 | currentNode := &resultNode 6 | carry := 0 7 | 8 | for l1 != nil || l2 != nil { 9 | var first, second int 10 | 11 | if l1 != nil { 12 | first = l1.Val 13 | l1 = l1.Next 14 | } 15 | 16 | if l2 != nil { 17 | second = l2.Val 18 | l2 = l2.Next 19 | } 20 | 21 | sum := first + second + carry 22 | carry = sum / 10 23 | currentNode.Next = &ListNode{sum % 10, nil} 24 | currentNode = currentNode.Next 25 | } 26 | 27 | if carry != 0 { 28 | currentNode.Next = &ListNode{1, nil} 29 | } 30 | 31 | return resultNode.Next 32 | } 33 | -------------------------------------------------------------------------------- /solutions/154.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func findMin(nums []int) int { 4 | left, right := 0, len(nums) - 1 5 | 6 | for left < right { 7 | middle := (right + left) / 2 8 | if nums[middle] < nums[right] { 9 | right = middle 10 | } else if nums[middle] > nums[right] { 11 | left = middle + 1 12 | } else if nums[middle] != nums[left] { 13 | right = middle 14 | } else { 15 | return searchMin(nums[left : right + 1]) 16 | } 17 | } 18 | 19 | return nums[left] 20 | } 21 | 22 | func searchMin(nums []int) int { 23 | result := nums[0] 24 | 25 | for i := 1; i < len(nums); i++ { 26 | if nums[i] < result { 27 | result = nums[i] 28 | } 29 | } 30 | 31 | return result 32 | } 33 | -------------------------------------------------------------------------------- /solutions/257.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "strconv" 5 | ) 6 | 7 | func binaryTreePaths(root *TreeNode) []string { 8 | if root == nil { 9 | return nil 10 | } 11 | 12 | var result []string 13 | 14 | traverseTreePaths(root, strconv.Itoa(root.Val), &result) 15 | 16 | return result 17 | } 18 | 19 | func traverseTreePaths(root *TreeNode, str string, result *[]string) { 20 | if root.Left == nil && root.Right == nil { 21 | *result = append(*result, str) 22 | 23 | return 24 | } 25 | 26 | if root.Left != nil { 27 | traverseTreePaths(root.Left, str + "->" + strconv.Itoa(root.Left.Val), result) 28 | } 29 | 30 | if root.Right != nil { 31 | traverseTreePaths(root.Right, str + "->" + strconv.Itoa(root.Right.Val), result) 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /solutions/234.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func isPalindrome(head *ListNode) bool { 4 | if head == nil || head.Next == nil { 5 | return true 6 | } 7 | 8 | middle, end := head, head 9 | 10 | for end.Next != nil { 11 | end = end.Next 12 | 13 | if end.Next == nil { 14 | break 15 | } 16 | 17 | end, middle = end.Next, middle.Next 18 | } 19 | 20 | var previous *ListNode 21 | 22 | current := middle 23 | 24 | for current != nil { 25 | current.Next, previous, current = previous, current, current.Next 26 | } 27 | 28 | middle.Next = nil 29 | 30 | for head != nil { 31 | if end.Val != head.Val { 32 | return false 33 | } 34 | 35 | end, head = end.Next, head.Next 36 | } 37 | 38 | return true 39 | } 40 | -------------------------------------------------------------------------------- /solutions/67.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "strconv" 5 | ) 6 | 7 | func addBinary(a string, b string) string { 8 | if len(a) < len(b) { 9 | a, b = b, a 10 | } 11 | 12 | carry := 0 13 | result := "" 14 | zeroes := "" 15 | 16 | for i := 0; i < len(a)-len(b); i++ { 17 | zeroes += "0" 18 | } 19 | 20 | b = zeroes + b 21 | 22 | for i := len(a) - 1; i >= 0; i-- { 23 | sum := carry 24 | 25 | if string(a[i]) == "1" { 26 | sum++ 27 | } 28 | 29 | if string(b[i]) == "1" { 30 | sum++ 31 | } 32 | 33 | carry = sum / 2 34 | sum = sum % 2 35 | result = strconv.Itoa(sum) + result 36 | } 37 | 38 | if carry > 0 { 39 | result = "1" + result 40 | } 41 | 42 | return result 43 | } 44 | -------------------------------------------------------------------------------- /solutions/84.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func largestRectangleArea(heights []int) int { 4 | area := 0 5 | 6 | for i := 1; i < len(heights); i++ { 7 | if heights[i] >= heights[i - 1] { 8 | continue 9 | } 10 | 11 | for j := i - 1; j >= 0; j-- { 12 | if heights[j] <= heights[i] { 13 | break 14 | } 15 | 16 | currentArea := heights[j] * (i - j) 17 | 18 | if currentArea > area { 19 | area = currentArea 20 | } 21 | 22 | heights[j] = heights[i] 23 | } 24 | } 25 | 26 | for i, height := range heights { 27 | currentArea := (len(heights) - i) * height 28 | 29 | if currentArea > area { 30 | area = currentArea 31 | } 32 | } 33 | 34 | return area 35 | } -------------------------------------------------------------------------------- /solutions/395.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func longestSubstring(s string, k int) int { 4 | dictionary := make([]int, 26) 5 | 6 | for _, character := range s { 7 | dictionary[int(character - 'a')]++ 8 | } 9 | 10 | result, temp := "", "" 11 | 12 | for i := 0; i < len(s); i++ { 13 | if dictionary[int(s[i] - 'a')] < k { 14 | result = maxLength(result, temp) 15 | temp = "" 16 | } else { 17 | temp += string(s[i]) 18 | } 19 | } 20 | 21 | result = maxLength(result, temp) 22 | 23 | if len(result) == len(s) { 24 | return len(result) 25 | } 26 | 27 | return longestSubstring(result, k) 28 | } 29 | 30 | func maxLength(a string, b string) string { 31 | if len(a) > len(b) { 32 | return a 33 | } 34 | 35 | return b 36 | } 37 | -------------------------------------------------------------------------------- /solutions/43.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func multiply(num1 string, num2 string) string { 4 | if num1 == "0" || num2 == "0" { 5 | return "0" 6 | } 7 | 8 | result := make([]byte, len(num1) + len(num2)) 9 | 10 | for i := len(num2) - 1; i >= 0; i-- { 11 | for j := len(num1) - 1; j >= 0; j-- { 12 | current := (num2[i] - '0') * (num1[j] - '0') + result[i + j + 1] 13 | 14 | result[i + j + 1] = current % 10 15 | result[i + j] += current / 10 16 | } 17 | } 18 | 19 | var zero int 20 | 21 | for i := 0; i < len(result) - 1; i++ { 22 | if result[i] != 0 { 23 | break 24 | } 25 | 26 | zero++ 27 | } 28 | 29 | for i := zero; i < len(result); i++ { 30 | result[i] += '0' 31 | } 32 | 33 | return string(result[zero:]) 34 | } 35 | -------------------------------------------------------------------------------- /solutions/47.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func permuteUnique(nums []int) [][]int { 8 | if len(nums) == 0 { 9 | return nil 10 | } 11 | 12 | sort.Ints(nums) 13 | 14 | result := make([][]int, 0) 15 | 16 | backtrackUnique(nums, nil, &result) 17 | 18 | return result 19 | } 20 | 21 | func backtrackUnique(nums []int, previous []int, result *[][]int) { 22 | if len(nums) == 0 { 23 | *result = append(*result, append([]int{}, previous...)) 24 | 25 | return 26 | } 27 | 28 | for i := 0; i < len(nums); i++ { 29 | if i != 0 && nums[i] == nums[i - 1] { 30 | continue 31 | } 32 | 33 | current := append(append([]int{}, nums[0:i]...), nums[i + 1:]...) 34 | 35 | backtrackUnique(current, append(previous, nums[i]), result) 36 | } 37 | } -------------------------------------------------------------------------------- /solutions/221.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func maximalSquare(matrix [][]byte) int { 4 | if len(matrix) == 0 { 5 | return 0 6 | } 7 | 8 | row, column := len(matrix), len(matrix[0]) 9 | previous, maxSize := 0, 0 10 | current := make([]int, column) 11 | 12 | for i := 0; i < row; i++ { 13 | for j := 0; j < column; j++ { 14 | temp := current[j] 15 | 16 | if i == 0 || j == 0 || matrix[i][j] == '0' { 17 | current[j] = int(matrix[i][j] - '0') 18 | } else { 19 | current[j] = min(previous, min(current[j], current[j - 1])) + 1 20 | } 21 | 22 | if maxSize < current[j] { 23 | maxSize = current[j] 24 | } 25 | 26 | previous = temp 27 | } 28 | } 29 | 30 | return maxSize * maxSize 31 | } 32 | -------------------------------------------------------------------------------- /solutions/403.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func canCross(stones []int) bool { 4 | cache := make(map[int]map[int]struct{}) 5 | 6 | for _, stone := range stones { 7 | cache[stone] = make(map[int]struct{}) 8 | } 9 | 10 | cache[0][1] = struct{}{} 11 | 12 | for i := 0; i < len(stones) - 1; i++ { 13 | for step := range cache[stones[i]] { 14 | reach := step + stones[i] 15 | 16 | if reach == stones[len(stones) - 1] { 17 | return true 18 | } 19 | 20 | if set, ok := cache[reach]; ok { 21 | set[step] = struct{}{} 22 | 23 | if step > 1 { 24 | set[step - 1] = struct{}{} 25 | } 26 | 27 | set[step + 1] = struct{}{} 28 | } 29 | } 30 | } 31 | 32 | return false 33 | } 34 | -------------------------------------------------------------------------------- /solutions/17.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func letterCombinations(digits string) []string { 4 | if len(digits) == 0 { 5 | return nil 6 | } 7 | 8 | result := make([]string, 0) 9 | 10 | combinations(&result, digits, "", 0) 11 | 12 | return result 13 | } 14 | 15 | func combinations(result *[]string, digits string, letters string, i int) { 16 | if i == len(digits) { 17 | *result = append(*result, letters) 18 | 19 | return 20 | } 21 | 22 | var hash = map[uint8]string { 23 | '2': "abc", 24 | '3': "def", 25 | '4': "ghi", 26 | '5': "jkl", 27 | '6': "mno", 28 | '7': "pqrs", 29 | '8': "tuv", 30 | '9': "wxyz", 31 | } 32 | 33 | for _, c := range hash[digits[i]] { 34 | combinations(result, digits, letters + string(c), i + 1) 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /solutions/200.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func numIslands(grid [][]byte) int { 4 | if len(grid) == 0 || len(grid[0]) == 0 { 5 | return 0 6 | } 7 | 8 | var count int 9 | 10 | for i := 0; i < len(grid); i++ { 11 | for j := 0; j < len(grid[0]); j++ { 12 | if grid[i][j] != '1' { 13 | continue 14 | } 15 | 16 | count++ 17 | 18 | countIslands(grid, i, j) 19 | } 20 | } 21 | 22 | return count 23 | } 24 | 25 | func countIslands(grid [][]byte, i, j int) { 26 | if i < 0 || j < 0 || i >= len(grid) || j >= len(grid[0]) || grid[i][j] != '1' { 27 | return 28 | } 29 | 30 | grid[i][j]++ 31 | 32 | countIslands(grid, i + 1, j) 33 | countIslands(grid, i, j + 1) 34 | countIslands(grid, i - 1, j) 35 | countIslands(grid, i, j - 1) 36 | } 37 | -------------------------------------------------------------------------------- /solutions/187.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | const ( 4 | None = 0 5 | Seen = 1 6 | Duplicated = 2 7 | ) 8 | 9 | func findRepeatedDnaSequences(s string) []string { 10 | var repeated []string 11 | var current uint32 12 | 13 | seen := make([]byte, 1<<20) 14 | sequence := map[byte]uint32 { 15 | 'A': 0, 16 | 'C': 1, 17 | 'G': 2, 18 | 'T': 3, 19 | } 20 | 21 | for i := 0; i < len(s); i++ { 22 | current = ((current << 2) & 0xFFFFF) | sequence[s[i]] 23 | 24 | if i < 9 { 25 | continue 26 | } 27 | 28 | if seen[current] == None { 29 | seen[current] = Seen 30 | } else if seen[current] == Seen { 31 | repeated = append(repeated, s[i - 9: i + 1]) 32 | 33 | seen[current] = Duplicated 34 | } 35 | } 36 | 37 | return repeated 38 | } 39 | -------------------------------------------------------------------------------- /solutions/188.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func maxProfit(k int, prices []int) int { 4 | if k >= len(prices) / 2 { 5 | var profit int 6 | 7 | for i := 1; i < len(prices); i++ { 8 | if prices[i] > prices[i - 1] { 9 | profit += prices[i] - prices[i - 1] 10 | } 11 | } 12 | 13 | return profit 14 | } 15 | 16 | profit := make([][]int, k + 1) 17 | 18 | for i := 0; i <= k; i++ { 19 | profit[i] = make([]int, len(prices)) 20 | } 21 | 22 | for i := 1; i <= k; i++ { 23 | currentMax := -prices[0] 24 | 25 | for j := 1; j < len(prices); j++ { 26 | profit[i][j] = max(profit[i][j - 1], prices[j] + currentMax) 27 | currentMax = max(currentMax, profit[i - 1][j - 1] - prices[j]) 28 | } 29 | } 30 | 31 | return profit[k][len(prices) - 1] 32 | } 33 | -------------------------------------------------------------------------------- /solutions/316.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func removeDuplicateLetters(s string) string { 4 | lastInstanceOf, seen := [26]int{}, [26]bool{} 5 | 6 | for i := range s { 7 | lastInstanceOf[s[i] - 'a'] = i 8 | } 9 | 10 | var result []rune 11 | 12 | for i, letter := range s { 13 | for len(result) > 0 { 14 | lastRune := result[len(result) - 1] 15 | 16 | if letter <= lastRune && lastInstanceOf[lastRune -'a'] >= i && !seen[letter - 'a'] { 17 | seen[lastRune - 'a'] = false 18 | result = result[:len(result) - 1] 19 | 20 | continue 21 | } 22 | 23 | break 24 | } 25 | 26 | if !seen[letter - 'a'] { 27 | result = append(result, letter) 28 | seen[letter - 'a'] = true 29 | } 30 | } 31 | 32 | return string(result) 33 | } 34 | -------------------------------------------------------------------------------- /solutions/290.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "strings" 5 | ) 6 | 7 | func wordPattern(pattern string, str string) bool { 8 | list := strings.Split(str, " ") 9 | firstDict, secondDict := make(map[byte]string), make(map[string]byte) 10 | 11 | if len(pattern) != len(list) { 12 | return false 13 | } 14 | 15 | for i := 0; i < len(pattern); i++ { 16 | if value, ok := firstDict[pattern[i]]; !ok { 17 | firstDict[pattern[i]] = list[i] 18 | } else { 19 | if value != list[i] { 20 | return false 21 | } 22 | } 23 | 24 | if word, ok := secondDict[list[i]]; !ok { 25 | secondDict[list[i]] = pattern[i] 26 | } else { 27 | if word != pattern[i] { 28 | return false 29 | } 30 | } 31 | } 32 | 33 | return true 34 | } 35 | -------------------------------------------------------------------------------- /solutions/38.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "strconv" 5 | ) 6 | 7 | func countAndSay(n int) string { 8 | result := "1" 9 | 10 | if n < 1 { 11 | return result 12 | } 13 | 14 | for i := 1; i < n; i++ { 15 | sequence := make([]byte, 0) 16 | count := 1 17 | 18 | for i := 1; i < len(result); i++ { 19 | if result[i] == result[i - 1] { 20 | count++ 21 | } else { 22 | sequence = append(sequence, []byte(strconv.Itoa(count))...) 23 | sequence = append(sequence, result[i - 1]) 24 | count = 1 25 | } 26 | } 27 | 28 | sequence = append(sequence, []byte(strconv.Itoa(count))...) 29 | sequence = append(sequence, result[len(result) - 1]) 30 | 31 | result = string(sequence) 32 | } 33 | 34 | return result 35 | } 36 | -------------------------------------------------------------------------------- /solutions/39.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func combinationSum(candidates []int, target int) [][]int { 8 | sort.Ints(candidates) 9 | result := make([][]int, 0) 10 | 11 | sumCandidates(candidates, target, []int{}, &result) 12 | 13 | return result 14 | } 15 | 16 | func sumCandidates(candidates []int, target int, current []int, result *[][]int) { 17 | for i, value := range candidates { 18 | if target - value <= 0 { 19 | if target - value == 0 { 20 | match := make([]int, len(current) + 1) 21 | copy(match, current) 22 | match[len(current)] = value 23 | 24 | *result = append(*result, match) 25 | } 26 | 27 | break 28 | } 29 | 30 | sumCandidates(candidates[i:], target - value, append(current, value), result) 31 | } 32 | } -------------------------------------------------------------------------------- /solutions/430.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func flatten(root *Node) *Node { 4 | if root == nil { 5 | return root 6 | } 7 | 8 | dummyHead := &Node{0, nil, root, nil} 9 | var current, previous *Node = nil, dummyHead 10 | 11 | var stack []*Node 12 | stack = append(stack, root) 13 | 14 | for len(stack) > 0 { 15 | current = stack[len(stack) - 1] 16 | stack = stack[:len(stack) - 1] 17 | 18 | previous.Next = current 19 | current.Prev = previous 20 | 21 | if current.Next != nil { 22 | stack = append(stack, current.Next) 23 | } 24 | 25 | if current.Child != nil { 26 | stack = append(stack, current.Child) 27 | current.Child = nil 28 | } 29 | 30 | previous = current 31 | } 32 | 33 | dummyHead.Next.Prev = nil 34 | 35 | return dummyHead.Next 36 | } 37 | -------------------------------------------------------------------------------- /solutions/354.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func maxEnvelopes(envelopes [][]int) int { 8 | envelopesCopy := envelopes 9 | 10 | sort.Slice(envelopesCopy, func (i int, j int) bool { 11 | if envelopesCopy[i][0] < envelopesCopy[j][0] { 12 | return true 13 | } else if envelopesCopy[i][0] > envelopesCopy[j][0] { 14 | return false 15 | } else { 16 | return envelopesCopy[i][1] > envelopesCopy[j][1] 17 | } 18 | }) 19 | 20 | dp := make([]int, len(envelopesCopy)) 21 | counter := 0 22 | 23 | for i := 0; i < len(envelopesCopy); i++ { 24 | position := sort.SearchInts(dp[:counter], envelopesCopy[i][1]) 25 | dp[position] = envelopesCopy[i][1] 26 | 27 | if position == counter { 28 | counter++ 29 | } 30 | } 31 | 32 | return counter 33 | } 34 | -------------------------------------------------------------------------------- /solutions/81.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func search(nums []int, target int) bool { 4 | left, right := 0, len(nums) - 1 5 | 6 | for left <= right { 7 | middle := (left + right) / 2 8 | 9 | if nums[middle] == target || target == nums[left] || target == nums[right] { 10 | return true 11 | } 12 | 13 | if nums[left] < nums[middle] { 14 | if target < nums[middle] && target >= nums[left] { 15 | right = middle 16 | } else { 17 | left = middle + 1 18 | } 19 | } else if nums[left] > nums[middle] { 20 | if target > nums[middle] && target <= nums[right] { 21 | left = middle + 1 22 | } else { 23 | right = middle 24 | } 25 | } else { 26 | left++ 27 | } 28 | } 29 | 30 | return false 31 | } -------------------------------------------------------------------------------- /solutions/117.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func connect(root *Node) *Node { 4 | if root == nil { 5 | return nil 6 | } 7 | 8 | if root.Left != nil { 9 | if root.Right != nil { 10 | root.Left.Next = root.Right 11 | } else { 12 | root.Left.Next = findNext(root) 13 | } 14 | } 15 | 16 | if root.Right != nil { 17 | root.Right.Next = findNext(root) 18 | } 19 | 20 | connect(root.Right) 21 | connect(root.Left) 22 | 23 | return root 24 | } 25 | 26 | func findNext(root *Node) *Node { 27 | for ; root != nil; root = root.Next { 28 | if root.Next != nil { 29 | if root.Next.Left != nil { 30 | return root.Next.Left 31 | } else if root.Next.Right != nil { 32 | return root.Next.Right 33 | } 34 | } 35 | } 36 | 37 | return nil 38 | } 39 | -------------------------------------------------------------------------------- /solutions/284.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | type PeekingIterator struct { 4 | numbers []int 5 | iterator *Iterator 6 | } 7 | 8 | func Constructor(iterator *Iterator) *PeekingIterator { 9 | return &PeekingIterator{make([]int, 0), iterator} 10 | } 11 | 12 | func (this *PeekingIterator) hasNext() bool { 13 | if len(this.numbers) > 0 || this.iterator.hasNext() { 14 | return true 15 | } 16 | 17 | return false 18 | } 19 | 20 | func (this *PeekingIterator) next() int { 21 | if len(this.numbers) > 0 { 22 | top := this.numbers[0] 23 | this.numbers = this.numbers[1:] 24 | 25 | return top 26 | } 27 | 28 | return this.iterator.next() 29 | } 30 | 31 | func (this *PeekingIterator) peek() int { 32 | if len(this.numbers) == 0 { 33 | this.numbers = append(this.numbers, this.iterator.next()) 34 | } 35 | 36 | return this.numbers[0] 37 | } 38 | -------------------------------------------------------------------------------- /solutions/464.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func canIWin(maxChoosableInteger int, desiredTotal int) bool { 4 | target, max := desiredTotal, maxChoosableInteger 5 | 6 | if max >= target { 7 | return true 8 | } 9 | 10 | if max* (max+ 1) / 2 < target { 11 | return false 12 | } 13 | 14 | dp := make([]int, 1 < 0; i-- { 29 | card := 1 << (i - 1) 30 | 31 | if state & card != 0 { 32 | continue 33 | } 34 | 35 | newState := state | card 36 | 37 | if !searchCards(newState, n, target - i, dp) { 38 | dp[state] = 1 39 | 40 | return true 41 | } 42 | } 43 | 44 | dp[state] = -1 45 | 46 | return false 47 | } 48 | -------------------------------------------------------------------------------- /solutions/87.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func isScramble(s1 string, s2 string) bool { 4 | if s1 == s2 { 5 | return true 6 | } 7 | 8 | if len(s1) != len(s2){ 9 | return false 10 | } 11 | 12 | letters := make([]int, 26) 13 | 14 | for i := 0; i < len(s1); i++ { 15 | letters[int(s1[i]) - int('a')]++ 16 | letters[int(s2[i]) - int('a')]-- 17 | } 18 | 19 | for i := 0; i < len(s1); i++ { 20 | if letters[int(s1[i]) - int('a')] != 0 { 21 | return false 22 | } 23 | } 24 | 25 | for i := 1; i < len(s1); i++ { 26 | if isScramble(s1[0: i], s2[0: i]) && isScramble(s1[i:], s2[i:]) { 27 | return true 28 | } 29 | 30 | if isScramble(s1[0: i], s2[len(s2) - i:]) && isScramble(s1[i:], s2[0: len(s2) - i]) { 31 | return true 32 | } 33 | } 34 | 35 | return false 36 | } 37 | -------------------------------------------------------------------------------- /solutions/173.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func Constructor(root *TreeNode) BSTIterator { 4 | result := BSTIterator{stack: []*TreeNode{}} 5 | 6 | for root != nil { 7 | result.stack = append(result.stack, root) 8 | root = root.Left 9 | } 10 | 11 | return result 12 | } 13 | 14 | /** @return the next smallest number */ 15 | func (this *BSTIterator) Next() int { 16 | top := this.stack[len(this.stack) - 1] 17 | result := top.Val 18 | this.stack = this.stack[: len(this.stack) - 1] 19 | 20 | if top.Right != nil { 21 | top = top.Right 22 | 23 | for top != nil { 24 | this.stack = append(this.stack, top) 25 | top = top.Left 26 | } 27 | } 28 | 29 | return result 30 | } 31 | 32 | /** @return whether we have a next smallest number */ 33 | func (this *BSTIterator) HasNext() bool { 34 | return len(this.stack) != 0 35 | } 36 | -------------------------------------------------------------------------------- /solutions/299.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func getHint(secret string, guess string) string { 8 | var countSecret, countGuess [10]int 9 | bulls, cows := 0,0 10 | 11 | for index := range secret { 12 | secretDigit := int(secret[index] - '0') 13 | guessDigit := int(guess[index] - '0') 14 | 15 | if guessDigit == secretDigit { 16 | bulls++ 17 | continue 18 | } 19 | 20 | if countGuess[secretDigit] > 0 { 21 | cows++ 22 | countGuess[secretDigit]-- 23 | } else { 24 | countSecret[secretDigit]++ 25 | } 26 | 27 | if countSecret[guessDigit] > 0 { 28 | cows++ 29 | countSecret[guessDigit]-- 30 | } else { 31 | countGuess[guessDigit]++ 32 | } 33 | } 34 | 35 | return fmt.Sprintf("%dA%dB", bulls, cows) 36 | } 37 | -------------------------------------------------------------------------------- /solutions/436.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func findRightInterval(intervals [][]int) []int { 8 | a := intervals 9 | 10 | for i := 0; i < len(a); i++ { 11 | a[i] = append(a[i], i) 12 | } 13 | 14 | sort.Slice(a, func(i int, j int) bool { 15 | return a[i][0] < a[j][0] 16 | }) 17 | 18 | result := make([]int, len(a)) 19 | 20 | for i := 0; i < len(a); i++ { 21 | b := a[i + 1:] 22 | 23 | position := i + 1 + sort.Search(len(a) - i - 1, func (j int) bool { 24 | if b[j][0] < a[i][1] { 25 | return false 26 | } else { 27 | return true 28 | } 29 | }) 30 | 31 | if position >= len(a) { 32 | result[a[i][2]] = -1 33 | } else { 34 | result[a[i][2]] = a[position][2] 35 | } 36 | } 37 | 38 | return result 39 | } 40 | -------------------------------------------------------------------------------- /solutions/410.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func splitArray(nums []int, m int) int { 4 | left, right := 0, 0 5 | 6 | for _, value := range nums { 7 | right += value 8 | 9 | if value > left { 10 | left = value 11 | } 12 | } 13 | 14 | for left < right { 15 | middle := (left + right) / 2 16 | cut := getCutSize(nums, middle) 17 | 18 | if cut > m { 19 | left = middle + 1 20 | } else { 21 | right = middle 22 | } 23 | } 24 | 25 | return left 26 | } 27 | 28 | func getCutSize(nums []int, max int) int { 29 | cutSize, currentValue := 1, 0 30 | 31 | for _, value := range nums { 32 | if currentValue + value <= max { 33 | currentValue += value 34 | } else { 35 | currentValue = value 36 | cutSize++ 37 | } 38 | } 39 | 40 | return cutSize 41 | } 42 | -------------------------------------------------------------------------------- /solutions/419.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func countBattleships(board [][]byte) int { 4 | result := 0 5 | 6 | for i, array := range board { 7 | for j, value := range array { 8 | if value == 'X' { 9 | switch { 10 | case i == 0 && j == 0: 11 | result++ 12 | case i != 0 && j == 0: 13 | if board[i - 1][j] != 'X' { 14 | result++ 15 | } 16 | case i == 0 && j != 0: 17 | if board[i][j - 1] != 'X' { 18 | result++ 19 | } 20 | case i != 0 && j != 0: 21 | if board[i][j - 1] != 'X' && board[i - 1][j] != 'X' { 22 | result++ 23 | } 24 | } 25 | } 26 | } 27 | } 28 | 29 | return result 30 | } 31 | -------------------------------------------------------------------------------- /solutions/376.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func wiggleMaxLength(nums []int) int { 4 | if len(nums) <= 1 { 5 | return len(nums) 6 | } 7 | 8 | current, peak := nums[0], false 9 | 10 | for i := 1; i < len(nums); i++ { 11 | if nums[i] != current { 12 | peak = nums[i] < current 13 | 14 | break 15 | } 16 | } 17 | 18 | result := 1 19 | 20 | for i := 1; i < len(nums); i++ { 21 | if nums[i] == current { 22 | continue 23 | } 24 | 25 | if nums[i] < current { 26 | if peak { 27 | peak = !peak 28 | result++ 29 | } 30 | current = nums[i] 31 | } else { 32 | if !peak { 33 | peak = !peak 34 | result++ 35 | } 36 | 37 | current = nums[i] 38 | } 39 | } 40 | 41 | return result 42 | } 43 | -------------------------------------------------------------------------------- /solutions/388.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "strings" 5 | ) 6 | 7 | func lengthLongestPath(input string) int { 8 | paths := strings.Split(input, "\n") 9 | levelMap := map[int]int{} 10 | levelMap[0], levelMap[-1] = -1, -2 11 | current := 0 12 | 13 | for _, path := range paths { 14 | level, length := getLevel(path) 15 | levelMap[level] = levelMap[level - 1] + length + 1 16 | 17 | if isFile(path) { 18 | if levelMap[level] > current { 19 | current = levelMap[level] 20 | } 21 | } 22 | } 23 | 24 | return current 25 | } 26 | 27 | func getLevel(s string) (int, int) { 28 | for index, value := range s { 29 | if value != '\t' { 30 | return index + 1, len(s) - index 31 | } 32 | } 33 | 34 | return 0, 0 35 | } 36 | 37 | func isFile(s string) bool { 38 | return strings.Contains(s, ".") 39 | } 40 | -------------------------------------------------------------------------------- /solutions/393.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func validUtf8(data []int) bool { 4 | var startBit uint8 = 7 5 | currentBits := 0 6 | 7 | for _, value := range data { 8 | if currentBits == 0 { 9 | mask := 1< 0 { 12 | startBit-- 13 | mask = 1< 4 { 17 | return false 18 | } 19 | } 20 | 21 | if currentBits > 1 { 22 | currentBits-- 23 | } 24 | 25 | startBit = 7 26 | } else if currentBits > 0 && value & (1<<7) > 0 && value & (1<<6) == 0 { 27 | currentBits-- 28 | } else { 29 | return false 30 | } 31 | } 32 | 33 | if currentBits > 0 { 34 | return false 35 | } 36 | 37 | return true 38 | } 39 | -------------------------------------------------------------------------------- /solutions/45.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func jump(nums []int) int { 4 | maxStep := len(nums) - 1 5 | 6 | currentPosition := 0 7 | steps := 0 8 | 9 | if maxStep == 0 { 10 | return 0 11 | } 12 | 13 | for { 14 | nextMaxStep := nums[currentPosition] 15 | steps++ 16 | 17 | if nextMaxStep >= maxStep { 18 | break 19 | } 20 | 21 | max := 0 22 | nextMove := currentPosition + 1 23 | 24 | for i := currentPosition + 1; i <= currentPosition + nextMaxStep && i < maxStep; i++ { 25 | if nums[i] >= max || i+nums[i] > max+nextMove { 26 | max = nums[i] 27 | nextMove = i 28 | } 29 | } 30 | 31 | if nextMove + max >= maxStep { 32 | steps++ 33 | 34 | break 35 | } 36 | 37 | currentPosition = nextMove 38 | 39 | } 40 | 41 | return steps 42 | } 43 | -------------------------------------------------------------------------------- /solutions/131.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func partition(s string) [][]string { 4 | var result [][]string 5 | 6 | partitionPalindrome(s, []string{}, &result) 7 | 8 | return result 9 | } 10 | 11 | func partitionPalindrome(s string, partition []string, result *[][]string) { 12 | if len(s) == 0 { 13 | *result = append(*result, partition) 14 | 15 | return 16 | } 17 | 18 | for i := 0; i <= len(s); i++ { 19 | if isValidPalindrome(s[: i]) { 20 | partitionPalindrome(s[i:], append(append([]string{}, partition...), s[: i]), result) 21 | } 22 | } 23 | } 24 | 25 | func isValidPalindrome(s string) bool { 26 | if len(s) == 0 { 27 | return false 28 | } 29 | 30 | i, j := 0, len(s) - 1 31 | 32 | for i < j { 33 | if s[i] != s[j] { 34 | return false 35 | } 36 | 37 | i++ 38 | j-- 39 | } 40 | 41 | return true 42 | } 43 | -------------------------------------------------------------------------------- /solutions/147.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func insertionSortList(head *ListNode) *ListNode { 4 | if head == nil { 5 | return nil 6 | } 7 | 8 | tail := head 9 | current := head.Next 10 | head.Next = nil 11 | 12 | for current != nil { 13 | node := current 14 | current = current.Next 15 | node.Next = nil 16 | 17 | if node.Val > tail.Val { 18 | tail.Next = node 19 | tail = node 20 | 21 | continue 22 | } 23 | 24 | if node.Val < head.Val { 25 | node.Next = head 26 | head = node 27 | 28 | continue 29 | } 30 | 31 | previous := head 32 | 33 | for previous.Next != nil && previous.Next.Val < node.Val { 34 | previous = previous.Next 35 | } 36 | 37 | node.Next = previous.Next 38 | previous.Next = node 39 | } 40 | 41 | return head 42 | } 43 | -------------------------------------------------------------------------------- /solutions/29.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "math" 5 | ) 6 | 7 | func divide(dividend int, divisor int) int { 8 | if dividend == math.MinInt32 && divisor == -1 { 9 | return math.MaxInt32 10 | } 11 | 12 | sign := 1 13 | 14 | if divisor < 0 && dividend < 0 { 15 | divisor = -divisor 16 | dividend = -dividend 17 | } else if divisor < 0 { 18 | divisor = -divisor 19 | sign = -1 20 | } else if dividend < 0 { 21 | dividend = -dividend 22 | sign = -1 23 | } 24 | 25 | quotient := 0 26 | 27 | for dividend >= divisor { 28 | multiple := divisor 29 | 30 | for i := 0; dividend >= multiple; i++ { 31 | dividend -= multiple 32 | quotient += 1 << uint(i) 33 | multiple <<= 1 34 | } 35 | } 36 | 37 | if sign == -1 { 38 | return -quotient 39 | } 40 | 41 | return quotient 42 | } 43 | -------------------------------------------------------------------------------- /solutions/113.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func pathSum(root *TreeNode, sum int) [][]int { 4 | result := make([][]int, 0) 5 | current := make([]int, 0) 6 | 7 | backtrackPathSum(&result, ¤t, root, sum) 8 | 9 | return result 10 | } 11 | 12 | func backtrackPathSum(result *[][]int, current *[]int, root *TreeNode, sum int) { 13 | if root == nil { 14 | return 15 | } 16 | 17 | sum -= root.Val 18 | *current = append(*current, root.Val) 19 | 20 | if root.Left == nil && root.Right == nil && sum == 0 { 21 | temp := make([]int, len(*current)) 22 | 23 | copy(temp, *current) 24 | 25 | *result = append(*result, temp) 26 | *current = (*current)[: len(*current) - 1] 27 | 28 | return 29 | } 30 | 31 | backtrackPathSum(result, current, root.Left, sum) 32 | backtrackPathSum(result, current, root.Right, sum) 33 | 34 | *current = (*current)[: len(*current) - 1] 35 | } 36 | -------------------------------------------------------------------------------- /solutions/215.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func findKthLargest(nums []int, k int) int { 4 | base := len(nums) / 2 - 1 5 | 6 | for i := base; i >= 0; i-- { 7 | siftUp(nums, i, len(nums) - 1) 8 | } 9 | 10 | for i := len(nums) - 1; i >= 0; i-- { 11 | k-- 12 | 13 | if k == 0 { 14 | return nums[0] 15 | } 16 | 17 | nums[0], nums[i] = nums[i], nums[0] 18 | 19 | siftUp(nums, 0, i - 1) 20 | } 21 | 22 | return nums[0] 23 | } 24 | 25 | func siftUp(nums []int, i, end int) { 26 | largest, left, right := i, 2 * i + 1, 2 * i + 2 27 | 28 | if left <= end && nums[largest] < nums[left] { 29 | largest = left 30 | } 31 | 32 | if right <= end && nums[largest] < nums[right] { 33 | largest = right 34 | } 35 | 36 | if i != largest { 37 | nums[i], nums[largest] = nums[largest], nums[i] 38 | 39 | siftUp(nums, largest, end) 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /solutions/helpers.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func min(a, b int) int { 4 | if a < b { 5 | return a 6 | } 7 | 8 | return b 9 | } 10 | 11 | func max(a int, b int) int { 12 | if a > b { 13 | return a 14 | } 15 | 16 | return b 17 | } 18 | 19 | func abs(number int) int { 20 | if number < 0 { 21 | return -number 22 | } 23 | 24 | return number 25 | } 26 | 27 | func minInArray(values []int) int { 28 | min := values[0] 29 | 30 | for i := 1; i < len(values); i++ { 31 | if values[i] < min { 32 | min = values[i] 33 | } 34 | } 35 | 36 | return min 37 | } 38 | 39 | func isChar(char byte) bool { 40 | return ('0' <= char && char <= '9') || ('A' <= char && char <= 'Z') || ('a' <= char && char <= 'z') 41 | } 42 | 43 | func toLower(char byte) byte { 44 | if 'A' <= char && char <= 'Z' { 45 | return char - 'A' + 'a' 46 | } 47 | 48 | return char 49 | } -------------------------------------------------------------------------------- /solutions/102.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func levelOrder(root *TreeNode) [][]int { 4 | if root == nil { 5 | return nil 6 | } 7 | 8 | var result [][]int 9 | var roots []*TreeNode 10 | 11 | roots = append(roots, root) 12 | 13 | for len(roots) > 0 { 14 | var currentResult []int 15 | rootsCount := len(roots) 16 | 17 | for i := 0; i < rootsCount; i++ { 18 | if roots[i] != nil { 19 | if roots[i].Left != nil { 20 | roots = append(roots, roots[i].Left) 21 | } 22 | 23 | if roots[i].Right != nil { 24 | roots = append(roots, roots[i].Right) 25 | } 26 | 27 | currentResult = append(currentResult, roots[i].Val) 28 | } 29 | } 30 | 31 | roots = roots[rootsCount:] 32 | result = append(result, currentResult) 33 | } 34 | 35 | return result 36 | } 37 | -------------------------------------------------------------------------------- /solutions/322.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "math" 5 | ) 6 | 7 | func coinChange(coins []int, amount int) int { 8 | dp := make([]int, amount + 1) 9 | 10 | return coinChangeHelper(coins, amount, dp) 11 | } 12 | 13 | func coinChangeHelper(coins []int, amount int, dp []int) int { 14 | if amount == 0 || dp[amount] != 0 { 15 | return dp[amount] 16 | } 17 | 18 | result := math.MaxInt64 19 | 20 | for _, value := range coins { 21 | if value == amount { 22 | result = 1 23 | } else if value < amount { 24 | current := 1 + coinChangeHelper(coins, amount -value, dp) 25 | 26 | if current != 0 && current < result { 27 | result = current 28 | } 29 | } 30 | } 31 | 32 | if result == math.MaxInt64 { 33 | dp[amount] = -1 34 | 35 | return -1 36 | } 37 | 38 | dp[amount] = result 39 | 40 | return result 41 | } 42 | -------------------------------------------------------------------------------- /solutions/93.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "strconv" 5 | "strings" 6 | ) 7 | 8 | func restoreIpAddresses(s string) []string { 9 | result := make([]string, 0) 10 | 11 | backtrackIpAddresses(s, &result, nil) 12 | 13 | return result 14 | } 15 | 16 | func backtrackIpAddresses(s string, result *[]string, current []string) { 17 | if len(s) == 0 && len(current) == 4 { 18 | *result = append(*result, strings.Join(current, ".")) 19 | 20 | return 21 | } 22 | 23 | if len(current) > 4 { 24 | return 25 | } 26 | 27 | for i := 1; i <= 3 && i <= len(s); i++ { 28 | part := s[0: i] 29 | 30 | if strings.HasPrefix(part, "0") && len(part) != 1 { 31 | continue 32 | } 33 | 34 | if number, _ := strconv.Atoi(part); number > 255 { 35 | continue 36 | } 37 | 38 | backtrackIpAddresses(s[i:], result, append(current, part)) 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /solutions/140.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func wordBreak(s string, wordDict []string) []string { 4 | return findWordBreak(s, wordDict, map[string][]string{}) 5 | } 6 | 7 | func findWordBreak(s string, wordsList []string, cache map[string][]string) []string { 8 | if result, ok := cache[s]; ok { 9 | return result 10 | } 11 | 12 | if len(s) == 0 { 13 | return []string{""} 14 | } 15 | 16 | var result []string 17 | 18 | for _, word := range wordsList { 19 | if len(word) <= len(s) && word == s[: len(word)] { 20 | for _, current := range findWordBreak(s[len(word):], wordsList, cache) { 21 | if len(current) == 0 { 22 | result = append(result, word) 23 | } else { 24 | result = append(result, word + " " +current) 25 | } 26 | } 27 | } 28 | } 29 | 30 | cache[s] = result 31 | 32 | return result 33 | } 34 | -------------------------------------------------------------------------------- /solutions/34.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func searchRange(nums []int, target int) []int { 4 | left := -1 5 | 6 | for i, j := 0, len(nums); i < j; { 7 | middle := (i + j) / 2 8 | 9 | if nums[middle] == target { 10 | left = middle 11 | j = middle 12 | 13 | continue 14 | } 15 | 16 | if nums[middle] < target { 17 | i = middle + 1 18 | 19 | continue 20 | } 21 | 22 | j = middle 23 | } 24 | 25 | right := -1 26 | 27 | for i, j := 0, len(nums); i < j; { 28 | middle := (i + j) / 2 29 | 30 | if nums[middle] == target { 31 | right = middle 32 | i = middle + 1 33 | 34 | continue 35 | } 36 | 37 | if nums[middle] < target { 38 | i = middle + 1 39 | 40 | continue 41 | } 42 | 43 | j = middle 44 | } 45 | 46 | return []int{left, right} 47 | } 48 | -------------------------------------------------------------------------------- /solutions/23.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func mergeKLists(lists []*ListNode) *ListNode { 4 | length := len(lists) 5 | 6 | if length == 0 { 7 | return nil 8 | } 9 | 10 | if length == 1 { 11 | return lists[0] 12 | } 13 | 14 | l1 := mergeKLists(lists[:length / 2]) 15 | l2 := mergeKLists(lists[length / 2:]) 16 | 17 | return mergeLists(l1, l2) 18 | } 19 | 20 | func mergeLists(l1, l2 *ListNode) *ListNode { 21 | var dummy ListNode 22 | current := &dummy 23 | 24 | for l1 != nil && l2 != nil { 25 | if l1.Val < l2.Val { 26 | current.Next = l1 27 | 28 | l1 = l1.Next 29 | } else { 30 | current.Next = l2 31 | 32 | l2 = l2.Next 33 | } 34 | 35 | current = current.Next 36 | } 37 | 38 | if l1 != nil { 39 | current.Next = l1 40 | } else if l2 != nil { 41 | current.Next = l2 42 | } 43 | 44 | return dummy.Next 45 | } 46 | -------------------------------------------------------------------------------- /solutions/241.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "strconv" 5 | ) 6 | 7 | func diffWaysToCompute(input string) []int { 8 | var result []int 9 | 10 | for i := 0; i < len(input); i++ { 11 | if input[i] == '+' || input[i] == '-' || input[i] == '*' { 12 | for _, a := range diffWaysToCompute(input[:i]) { 13 | for _, b := range diffWaysToCompute(input[i + 1:]) { 14 | switch input[i] { 15 | case '+': 16 | result = append(result, a + b) 17 | case '-': 18 | result = append(result, a - b) 19 | case '*': 20 | result = append(result, a * b) 21 | } 22 | } 23 | } 24 | } 25 | } 26 | 27 | if value, _ := strconv.Atoi(input); len(result) == 0 { 28 | result = append(result, value) 29 | } 30 | 31 | return result 32 | } 33 | -------------------------------------------------------------------------------- /solutions/335.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func isSelfCrossing(x []int) bool { 4 | if len(x) < 4 { 5 | return false 6 | } 7 | 8 | for _, value := range x[1 :] { 9 | if value == 0 { 10 | return true 11 | } 12 | } 13 | 14 | if x[3] >= x[1] && x[2] <= x[0] { 15 | return true 16 | } 17 | 18 | spiralIn := x[3] < x[1] 19 | 20 | for i := 4; i < len(x); i ++ { 21 | if spiralIn { 22 | if x[i] >= x[i - 2] { 23 | return true 24 | } 25 | } else { 26 | if x[i] <= x[i - 2] { 27 | spiralIn = true 28 | 29 | if x[i] + x[i - 4] >= x[i - 2] { 30 | x[i - 1] = x[i - 1] - x[i - 3] 31 | } 32 | } 33 | } 34 | } 35 | 36 | for _, value := range x[1 :] { 37 | if value == 0 { 38 | return true 39 | } 40 | } 41 | 42 | return false 43 | } 44 | -------------------------------------------------------------------------------- /solutions/318.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | type MaskPair struct { 4 | mask int 5 | length int 6 | } 7 | 8 | func maxProduct(words []string) int { 9 | masks := make([]MaskPair, len(words)) 10 | 11 | for i := 0; i < len(words); i++ { 12 | masks[i] = MaskPair{mask: getBitMask(words[i]), length: len(words[i])} 13 | } 14 | 15 | result := 0 16 | 17 | for i := 0; i < len(masks); i++ { 18 | for j := i + 1; j < len(masks); j++ { 19 | if masks[i].mask & masks[j].mask != 0 { 20 | continue 21 | } 22 | 23 | current := masks[i].length * masks[j].length 24 | 25 | if current > result { 26 | result = current 27 | } 28 | } 29 | } 30 | 31 | return result 32 | } 33 | 34 | func getBitMask(s string) int { 35 | result := 0 36 | 37 | for i := 0; i < len(s); i++ { 38 | result |= 1 << int(s[i] - 'a') 39 | } 40 | 41 | return result 42 | } 43 | -------------------------------------------------------------------------------- /solutions/236.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func lowestCommonAncestor(node, p, q *TreeNode) *TreeNode { 4 | var ancestorsP []*TreeNode 5 | ancestors(node, p.Val, &ancestorsP) 6 | 7 | var ancestorsQ []*TreeNode 8 | ancestors(node, q.Val, &ancestorsQ) 9 | 10 | result := node 11 | 12 | for i := 0; i < len(ancestorsP) && i < len(ancestorsQ); i++ { 13 | if ancestorsP[len(ancestorsP) - 1 - i].Val == ancestorsQ[len(ancestorsQ)- 1 - i].Val { 14 | result = ancestorsP[len(ancestorsP) - 1 - i] 15 | } else { 16 | break 17 | } 18 | } 19 | 20 | return result 21 | } 22 | 23 | func ancestors(node *TreeNode, value int, nodes *[]*TreeNode) bool { 24 | if node == nil { 25 | return false 26 | } 27 | 28 | if node.Val == value || ancestors(node.Left, value, nodes) || ancestors(node.Right, value, nodes) { 29 | *nodes = append(*nodes, node) 30 | 31 | return true 32 | } 33 | 34 | return false 35 | } 36 | -------------------------------------------------------------------------------- /solutions/402.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func removeKdigits(num string, k int) string { 4 | count := k 5 | 6 | var stack []byte 7 | var result string 8 | 9 | for i := 0; i < len(num); i++ { 10 | for k >= 0 { 11 | if k == 0 { 12 | result = string(stack) + num[i:] 13 | } 14 | 15 | if len(stack) == 0 { 16 | stack = append(stack, num[i]) 17 | break 18 | } else if stack[len(stack) - 1] <= num[i] { 19 | stack = append(stack, num[i]) 20 | break 21 | } else { 22 | stack = stack[:len(stack) - 1] 23 | k-- 24 | } 25 | } 26 | } 27 | 28 | if k > 0 { 29 | result = string(stack)[:len(num) - count] 30 | } 31 | 32 | for i := 0; i < len(result); i++ { 33 | if result[i] != '0' { 34 | return result[i:] 35 | } 36 | } 37 | 38 | return "0" 39 | } 40 | -------------------------------------------------------------------------------- /solutions/107.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func levelOrderBottom(root *TreeNode) [][]int { 4 | if root == nil { 5 | return nil 6 | } 7 | 8 | var queue []*TreeNode 9 | var result [][]int 10 | queue = append(queue, root) 11 | 12 | for len(queue) > 0 { 13 | var level []int 14 | length := len(queue) 15 | 16 | for i := 0; i < length; i++ { 17 | node := queue[0] 18 | queue = queue[1:] 19 | level = append(level, node.Val) 20 | 21 | if node.Left != nil { 22 | queue = append(queue, node.Left) 23 | } 24 | 25 | if node.Right != nil { 26 | queue = append(queue, node.Right) 27 | } 28 | } 29 | 30 | result = append(result, level) 31 | } 32 | 33 | var reversed [][]int 34 | 35 | for i := len(result) - 1; i >= 0; i-- { 36 | reversed = append(reversed, result[i]) 37 | } 38 | 39 | return reversed 40 | } 41 | -------------------------------------------------------------------------------- /solutions/133.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func cloneGraph(node *GraphNode) *GraphNode { 4 | if node == nil { 5 | return nil 6 | } 7 | 8 | queue := []*GraphNode{node} 9 | cloned := map[int]*GraphNode{ 10 | node.Val: { 11 | Val: node.Val, 12 | Neighbors: []*GraphNode{}, 13 | }, 14 | } 15 | 16 | for len(queue) > 0 { 17 | node, queue = queue[0], queue[1:] 18 | nextNode, _ := cloned[node.Val] 19 | 20 | for _, neighbor := range node.Neighbors { 21 | if _, visited := cloned[neighbor.Val]; !visited { 22 | cloned[neighbor.Val] = &GraphNode{ 23 | Val: neighbor.Val, 24 | Neighbors: []*GraphNode{}, 25 | } 26 | 27 | queue = append(queue, neighbor) 28 | } 29 | 30 | nextNode.Neighbors = append(nextNode.Neighbors, cloned[neighbor.Val]) 31 | } 32 | } 33 | 34 | return cloned[1] 35 | } 36 | -------------------------------------------------------------------------------- /solutions/31.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func nextPermutation(nums []int) { 4 | if len(nums) == 0 { 5 | return 6 | } 7 | 8 | pivot := -1 9 | 10 | for i := len(nums) - 2; i >= 0; i-- { 11 | if nums[i] < nums[i + 1] { 12 | pivot = i 13 | 14 | break 15 | } 16 | } 17 | 18 | if pivot == -1 { 19 | reverseNumbers(&nums, 0, len(nums) - 1) 20 | 21 | return 22 | } 23 | 24 | nextPivot := 0 25 | 26 | for i := len(nums) - 1; i >= 0; i-- { 27 | if nums[i] > nums[pivot] { 28 | nextPivot = i 29 | 30 | break 31 | } 32 | } 33 | 34 | nums[pivot], nums[nextPivot] = nums[nextPivot], nums[pivot] 35 | 36 | reverseNumbers(&nums, pivot + 1, len(nums) - 1) 37 | } 38 | 39 | func reverseNumbers(nums *[]int, start, end int) { 40 | for start < end { 41 | (*nums)[start], (*nums)[end] = (*nums)[end], (*nums)[start] 42 | start++ 43 | end-- 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /solutions/174.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func calculateMinimumHP(dungeon [][]int) int { 4 | rows, columns := len(dungeon) - 1, len(dungeon[0]) - 1 5 | 6 | for row := rows; row >= 0; row-- { 7 | for column := columns; column >= 0; column-- { 8 | if row == rows && column == columns { 9 | dungeon[row][column] = max(1, 1 - dungeon[row][column]) 10 | 11 | continue 12 | } 13 | 14 | if row == rows { 15 | dungeon[row][column] = max(1, dungeon[row][column + 1] - dungeon[row][column]) 16 | 17 | continue 18 | } 19 | 20 | if column == columns { 21 | dungeon[row][column] = max(1, dungeon[row + 1][column] - dungeon[row][column]) 22 | 23 | continue 24 | } 25 | 26 | dungeon[row][column] = max(1, min(dungeon[row + 1][column], dungeon[row][column + 1]) - dungeon[row][column]) 27 | } 28 | } 29 | 30 | return dungeon[0][0] 31 | } 32 | -------------------------------------------------------------------------------- /solutions/59.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func generateMatrix(n int) [][]int { 4 | matrix := make([][]int, n) 5 | 6 | for i := 0; i < n; i++ { 7 | matrix[i] = make([]int, n) 8 | } 9 | 10 | top, right, bottom, left := 0, len(matrix[0]) - 1, len(matrix) - 1, 0 11 | 12 | for number := 1; top <= bottom && left <= right; { 13 | for i := left; i <= right; i++ { 14 | matrix[top][i] = number 15 | number++ 16 | } 17 | 18 | top++ 19 | 20 | for i := top; i <= bottom; i++ { 21 | matrix[i][right] = number 22 | number++ 23 | } 24 | 25 | right-- 26 | 27 | for i := right; i >= left; i-- { 28 | matrix[bottom][i] = number 29 | number++ 30 | } 31 | 32 | bottom-- 33 | 34 | for i := bottom; i >= top; i-- { 35 | matrix[i][left] = number 36 | number++ 37 | } 38 | 39 | left++ 40 | } 41 | 42 | return matrix 43 | } 44 | -------------------------------------------------------------------------------- /solutions/103.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func zigzagLevelOrder(root *TreeNode) [][]int { 4 | if root == nil { 5 | return nil 6 | } 7 | 8 | var result [][]int 9 | queue := []*TreeNode{root} 10 | 11 | for ascending := true; len(queue) > 0; ascending = !ascending { 12 | var level []int 13 | length := len(queue) 14 | 15 | for i := 0; i < length; i++ { 16 | current := queue[i] 17 | 18 | if ascending { 19 | level = append(level, current.Val) 20 | } else { 21 | level = append(level, queue[length - 1 - i].Val) 22 | } 23 | 24 | if current.Left != nil { 25 | queue = append(queue, current.Left) 26 | } 27 | 28 | if current.Right != nil { 29 | queue = append(queue, current.Right) 30 | } 31 | } 32 | 33 | queue = queue[length:] 34 | result = append(result, level) 35 | } 36 | 37 | return result 38 | } -------------------------------------------------------------------------------- /solutions/155.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | type MinStack struct { 4 | Stack []int 5 | Min []int 6 | } 7 | 8 | func Constructor() MinStack { 9 | return MinStack{Stack: []int{}, Min: []int{}} 10 | } 11 | 12 | func (stack *MinStack) Push(x int) { 13 | stack.Stack = append(stack.Stack, x) 14 | 15 | if len(stack.Min) == 0 { 16 | stack.Min = append(stack.Min, 0) 17 | 18 | return 19 | } 20 | 21 | if stack.Stack[stack.Min[len(stack.Min) - 1]] < x { 22 | stack.Min = append(stack.Min, stack.Min[len(stack.Min) - 1]) 23 | } else { 24 | stack.Min = append(stack.Min, len(stack.Stack) - 1) 25 | } 26 | } 27 | 28 | func (stack *MinStack) Pop() { 29 | stack.Stack = stack.Stack[: len(stack.Stack) - 1] 30 | stack.Min = stack.Min[: len(stack.Min) - 1] 31 | } 32 | 33 | func (stack *MinStack) Top() int { 34 | return stack.Stack[len(stack.Stack) - 1] 35 | } 36 | 37 | func (stack *MinStack) GetMin() int { 38 | return stack.Stack[stack.Min[len(stack.Min) - 1]] 39 | } 40 | -------------------------------------------------------------------------------- /solutions/135.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func candy(ratings []int) int { 4 | length := len(ratings) 5 | 6 | if length == 0 { 7 | return 0 8 | } 9 | 10 | totalCandies := 1 11 | leftNeighbour := 1 12 | 13 | for i := 1; i < length; { 14 | if ratings[i] > ratings[i - 1] { 15 | leftNeighbour = leftNeighbour + 1 16 | totalCandies += leftNeighbour 17 | 18 | i++ 19 | } else if ratings[i] == ratings[i-1] { 20 | leftNeighbour = 1 21 | totalCandies += 1 22 | 23 | i++ 24 | } else { 25 | count := 0 26 | 27 | for ; i < length && ratings[i - 1] > ratings[i]; i++ { 28 | count++ 29 | 30 | totalCandies += count 31 | } 32 | 33 | if count >= leftNeighbour { 34 | totalCandies += count - leftNeighbour + 1 35 | } 36 | 37 | leftNeighbour = 1 38 | } 39 | } 40 | 41 | return totalCandies 42 | } 43 | -------------------------------------------------------------------------------- /solutions/207.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func canFinish(numCourses int, prerequisites [][]int) bool { 4 | var sortedOrder, sources []int 5 | 6 | graph := make(map[int][]int, numCourses) 7 | inDegree := make([]int, numCourses) 8 | 9 | for _, pair := range prerequisites { 10 | child, parent := pair[0], pair[1] 11 | graph[parent] = append(graph[parent], child) 12 | 13 | inDegree[child]++ 14 | } 15 | 16 | for i := 0; i < numCourses; i++ { 17 | if inDegree[i] == 0 { 18 | sources = append(sources, i) 19 | } 20 | } 21 | 22 | for len(sources) > 0 { 23 | course := sources[0] 24 | sortedOrder = append(sortedOrder, course) 25 | sources = sources[1:] 26 | 27 | for _, child := range graph[course] { 28 | inDegree[child] -= 1 29 | 30 | if inDegree[child] == 0 { 31 | sources = append(sources, child) 32 | } 33 | } 34 | } 35 | 36 | return len(sortedOrder) == numCourses 37 | } 38 | -------------------------------------------------------------------------------- /solutions/341.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | type NestedIterator struct { 4 | list []*NestedInteger 5 | i int 6 | current *NestedIterator 7 | } 8 | 9 | func Constructor(nestedList []*NestedInteger) *NestedIterator { 10 | return &NestedIterator{list: nestedList} 11 | } 12 | 13 | func (this *NestedIterator) Next() int { 14 | if this.list[this.i].IsInteger() { 15 | result := this.list[this.i].GetInteger() 16 | this.i++ 17 | 18 | return result 19 | } 20 | 21 | return this.current.Next() 22 | } 23 | 24 | func (this *NestedIterator) HasNext() bool { 25 | if this.i >= len(this.list) { 26 | return false 27 | } 28 | 29 | if this.list[this.i].IsInteger() { 30 | return true 31 | } 32 | 33 | if this.current == nil { 34 | this.current = Constructor(this.list[this.i].GetList()) 35 | } 36 | 37 | if this.current.HasNext() { 38 | return true 39 | } 40 | 41 | this.i++ 42 | this.current = nil 43 | 44 | return this.HasNext() 45 | } 46 | -------------------------------------------------------------------------------- /solutions/44.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func isMatch(s string, p string) bool { 4 | stringIndex, patternIndex, stringReverseIndex := 0, 0, 0 5 | asteriskIndex := -1 6 | 7 | for stringIndex < len(s) { 8 | match := patternIndex < len(p) && (s[stringIndex] == p[patternIndex] || p[patternIndex] == '?') 9 | 10 | if match { 11 | patternIndex += 1 12 | stringIndex += 1 13 | } else if patternIndex < len(p) && p[patternIndex] == '*' { 14 | asteriskIndex = patternIndex 15 | stringReverseIndex = stringIndex 16 | 17 | patternIndex += 1 18 | } else if asteriskIndex != -1 { 19 | patternIndex = asteriskIndex + 1 20 | 21 | stringIndex = stringReverseIndex + 1 22 | stringReverseIndex = stringIndex 23 | } else { 24 | return false 25 | } 26 | } 27 | 28 | for patternIndex < len(p) && p[patternIndex] == '*' { 29 | patternIndex += 1 30 | } 31 | 32 | return patternIndex == len(p) 33 | } 34 | -------------------------------------------------------------------------------- /solutions/332.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func findItinerary(tickets [][]string) []string { 8 | result, paths := make([]string, 0), make(map[string][]string) 9 | 10 | for _, value := range tickets { 11 | paths[value[0]] = append(paths[value[0]], value[1]) 12 | } 13 | 14 | for _, value := range paths { 15 | sort.Strings(value) 16 | } 17 | 18 | findItineraryRecursive("JFK", &result, paths) 19 | 20 | return reverse(result) 21 | } 22 | 23 | func findItineraryRecursive(source string, result *[]string, paths map[string][]string) { 24 | for len(paths[source]) > 0 { 25 | current := paths[source][0] 26 | paths[source] = paths[source][1:] 27 | 28 | findItineraryRecursive(current, result, paths) 29 | } 30 | 31 | *result = append(*result, source) 32 | } 33 | 34 | func reverse(s []string) []string { 35 | for i, j := 0, len(s) - 1; i < j; { 36 | s[i], s[j] = s[j], s[i] 37 | 38 | i++ 39 | j-- 40 | } 41 | 42 | return s 43 | } 44 | -------------------------------------------------------------------------------- /solutions/394.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "strings" 5 | ) 6 | 7 | func decodeString(s string) string { 8 | stackNumber, stackString := make([]int, 0), make([]string, 0) 9 | 10 | var result string 11 | var number int 12 | 13 | for i := 0; i < len(s); i++ { 14 | switch { 15 | case s[i] >= '0' && s[i] <= '9': 16 | number = 10 * number + int(s[i]) - '0' 17 | case s[i] == '[': 18 | stackNumber = append(stackNumber, number) 19 | stackString = append(stackString, result) 20 | number = 0 21 | result = "" 22 | case s[i] == ']': 23 | temp := stackString[len(stackString) - 1] 24 | stackString = stackString[:len(stackString) - 1] 25 | count := stackNumber[len(stackNumber) -1] 26 | stackNumber = stackNumber[:len(stackNumber) - 1] 27 | result = temp + strings.Repeat(result, count) 28 | default: 29 | result += string(s[i]) 30 | } 31 | } 32 | 33 | return result 34 | } 35 | -------------------------------------------------------------------------------- /solutions/310.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func findMinHeightTrees(n int, edges [][]int) []int { 4 | graph := make([][]int, n) 5 | 6 | for _, value := range edges { 7 | graph[value[0]] = append(graph[value[0]], value[1]) 8 | graph[value[1]] = append(graph[value[1]], value[0]) 9 | } 10 | 11 | firstVisited := make([]bool, n) 12 | firstRoute := findMinHeightTree(graph, 0, firstVisited) 13 | 14 | secondVisited := make([]bool, n) 15 | secondRoute := findMinHeightTree(graph, firstRoute[0], secondVisited) 16 | 17 | return secondRoute[(len(secondRoute) - 1) / 2 : len(secondRoute) / 2 + 1] 18 | } 19 | 20 | func findMinHeightTree(graph [][]int, n int, visited []bool) []int { 21 | var result []int 22 | visited[n] = true 23 | 24 | for _, value := range graph[n] { 25 | if !visited[value] { 26 | route := findMinHeightTree(graph, value, visited) 27 | 28 | if len(route) > len(result) { 29 | result = route 30 | } 31 | } 32 | } 33 | 34 | return append(result, n) 35 | } 36 | -------------------------------------------------------------------------------- /solutions/73.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func setZeroes(matrix [][]int) { 4 | rows := len(matrix) 5 | columns := len(matrix[0]) 6 | firstColumn := false 7 | 8 | for row := 0; row < rows; row++ { 9 | if matrix[row][0] == 0 { 10 | firstColumn = true 11 | } 12 | 13 | for column := 1; column < columns; column++ { 14 | if matrix[row][column] == 0 { 15 | matrix[row][0] = 0 16 | matrix[0][column] = 0 17 | } 18 | } 19 | } 20 | 21 | for row := 1; row < rows; row++ { 22 | for column := 1; column < columns; column++ { 23 | if matrix[0][column] == 0 || matrix[row][0] == 0 { 24 | matrix[row][column] = 0 25 | } 26 | } 27 | } 28 | 29 | if matrix[0][0] == 0 { 30 | for column := 0; column < columns; column++ { 31 | matrix[0][column] = 0 32 | } 33 | } 34 | 35 | if firstColumn { 36 | for row := 0; row < rows; row++ { 37 | matrix[row][0] = 0 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /solutions/165.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "strconv" 5 | "strings" 6 | ) 7 | 8 | func compareVersion(version1 string, version2 string) int { 9 | firstVersion := strings.Split(version1, ".") 10 | secondVersion := strings.Split(version2, ".") 11 | length := len(firstVersion) 12 | 13 | if len(secondVersion) > length { 14 | length = len(secondVersion) 15 | } 16 | 17 | firstVersionParts := make([]int, length) 18 | secondVersionParts := make([]int, length) 19 | 20 | for i, rawPart := range firstVersion { 21 | part, _ := strconv.Atoi(rawPart) 22 | 23 | firstVersionParts[i] = part 24 | } 25 | 26 | for i, rawPart := range secondVersion { 27 | part, _ := strconv.Atoi(rawPart) 28 | 29 | secondVersionParts[i] = part 30 | } 31 | 32 | for i := 0; i < length; i++ { 33 | if firstVersionParts[i] < secondVersionParts[i] { 34 | return -1 35 | } else if firstVersionParts[i] > secondVersionParts[i] { 36 | return 1 37 | } 38 | } 39 | 40 | return 0 41 | } 42 | -------------------------------------------------------------------------------- /solutions/227.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func calculate(s string) int { 4 | number, sum, currentSum := 0, 0, 0 5 | lastSign := byte('+') 6 | 7 | for i := 0; i < len(s); i++ { 8 | character := s[i] 9 | 10 | if isByteDigit(character) { 11 | number = 10 * number + int(character - '0') 12 | } 13 | 14 | if i == len(s) - 1 || (!isByteDigit(character) && character != ' ') { 15 | switch lastSign { 16 | case '+': 17 | sum += currentSum 18 | currentSum = number 19 | case '-': 20 | sum += currentSum 21 | currentSum = -number 22 | case '*': 23 | currentSum *= number 24 | case '/': 25 | currentSum /= number 26 | } 27 | 28 | lastSign = character 29 | number = 0 30 | } 31 | } 32 | 33 | return sum + currentSum 34 | } 35 | 36 | func isByteDigit(b byte) bool { 37 | if b - '0' <= 9 { 38 | return true 39 | } 40 | 41 | return false 42 | } 43 | -------------------------------------------------------------------------------- /solutions/79.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func exist(board [][]byte, word string) bool { 4 | if len(word) == 0 { 5 | return false 6 | } 7 | 8 | search := []byte(word) 9 | 10 | for x := range board { 11 | for y := range board[x] { 12 | if searchWord(x, y, board, search) { 13 | return true 14 | } 15 | } 16 | } 17 | 18 | return false 19 | } 20 | 21 | func searchWord(x, y int, board [][]byte, word []byte) bool { 22 | if (x < 0 || x >= len(board)) || (y < 0 || y >= len(board[x])) { 23 | return false 24 | } 25 | 26 | if board[x][y] != word[0] { 27 | return false 28 | } 29 | 30 | if len(word) == 1 { 31 | return true 32 | } 33 | 34 | temp := board[x][y] 35 | board[x][y] = '0' 36 | 37 | found := searchWord(x + 1, y, board, word[1:]) || 38 | searchWord(x - 1, y, board, word[1:]) || 39 | searchWord(x, y + 1, board, word[1:]) || 40 | searchWord(x, y - 1, board, word[1:]) 41 | 42 | board[x][y] = temp 43 | 44 | return found 45 | } 46 | -------------------------------------------------------------------------------- /solutions/99.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func recoverTree(root *TreeNode) { 4 | var previous, first, second *TreeNode = nil, nil, nil 5 | swapped := false 6 | 7 | traverseRecoveredTree(root, &previous, &first, &second, &swapped) 8 | 9 | if !swapped { 10 | (*first).Val, (*second).Val = (*second).Val, (*first).Val 11 | } 12 | } 13 | 14 | func traverseRecoveredTree(root *TreeNode, previous, first, second **TreeNode, swapped *bool) { 15 | if root == nil { 16 | return 17 | } 18 | 19 | traverseRecoveredTree(root.Left, previous, first, second, swapped) 20 | 21 | if *swapped { 22 | return 23 | } 24 | 25 | if *previous != nil && *first == nil && (*previous).Val > root.Val { 26 | *first = *previous 27 | *second = root 28 | } else if *first != nil && (*previous).Val > root.Val { 29 | (*first).Val, root.Val = root.Val, (*first).Val 30 | 31 | *swapped = true 32 | 33 | return 34 | } 35 | 36 | *previous = root 37 | 38 | traverseRecoveredTree(root.Right, previous, first, second, swapped) 39 | } 40 | -------------------------------------------------------------------------------- /solutions/76.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func minWindow(s string, t string) string { 4 | if s == "" || t == "" { 5 | return "" 6 | } 7 | 8 | target, count := [128]int{}, [128]int{} 9 | required, current, min := 0, 0, len(s) + 1 10 | result := "" 11 | 12 | for i := range t { 13 | if target[t[i]] == 0 { 14 | required++ 15 | } 16 | 17 | target[t[i]]++ 18 | } 19 | 20 | for left, right := 0, 0; right < len(s); right++ { 21 | char := s[right] 22 | count[char]++ 23 | 24 | if count[char] == target[char] { 25 | current++ 26 | } 27 | 28 | for left <= right && current == required { 29 | char = s[left] 30 | 31 | if right - left + 1 < min { 32 | min = right - left + 1 33 | result = s[left : right+1] 34 | } 35 | 36 | count[char]-- 37 | 38 | if count[char] < target[char] { 39 | current-- 40 | } 41 | 42 | left++ 43 | } 44 | } 45 | 46 | return result 47 | } 48 | -------------------------------------------------------------------------------- /solutions/72.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func minDistance(word1 string, word2 string) int { 4 | if word1 == word2 { 5 | return 0 6 | } 7 | 8 | lengthFirst, lengthSecond := len(word1), len(word2) 9 | dp := make([][]int, 2) 10 | 11 | for i := 0; i < 2; i++ { 12 | dp[i] = make([]int, lengthSecond + 1) 13 | } 14 | 15 | for i := 0; i < lengthFirst + 1; i++ { 16 | for j := 0; j < lengthSecond + 1; j++ { 17 | if i == 0 { 18 | dp[0][j] = j 19 | } else if j == 0 { 20 | dp[1][0] = i 21 | } else if word1[i - 1] == word2[j - 1] { 22 | dp[1][j] = dp[0][j - 1] 23 | } else { 24 | dp[1][j] = min(min(dp[0][j - 1] + 1, dp[1][j - 1] + 1), dp[0][j] + 1) 25 | } 26 | } 27 | 28 | if i > 0 { 29 | for k := 0; k < lengthSecond + 1; k++ { 30 | dp[0][k] = dp[1][k] 31 | } 32 | } 33 | } 34 | 35 | if lengthFirst == 0 { 36 | return dp[0][lengthSecond] 37 | } 38 | 39 | return dp[1][lengthSecond] 40 | } 41 | -------------------------------------------------------------------------------- /solutions/368.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func largestDivisibleSubset(nums []int) []int { 8 | sort.Slice(nums, func (i int, j int) bool { 9 | return nums[i] > nums[j] 10 | }) 11 | 12 | dictionary := make(map[int][]int) 13 | 14 | return findLargestDivisibleSubset(nums, dictionary, 0) 15 | } 16 | 17 | func findLargestDivisibleSubset(nums []int, dictionary map[int][]int, current int) []int { 18 | if len(nums) == 0 { 19 | return []int{} 20 | } 21 | 22 | var result []int 23 | 24 | for i := 0; i < len(nums); i++ { 25 | var temp []int 26 | 27 | if current% nums[i] == 0 { 28 | if value, ok := dictionary[nums[i]]; ok { 29 | temp = value 30 | } else { 31 | temp = append([]int{nums[i]}, findLargestDivisibleSubset(nums[i + 1:], dictionary, nums[i])...) 32 | } 33 | 34 | dictionary[nums[i]] = temp 35 | 36 | if len(temp) > len(result) { 37 | result = temp 38 | } 39 | } 40 | } 41 | 42 | return result 43 | } 44 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Dawid Janik 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /solutions/130.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func solve(board [][]byte) { 4 | if len(board) == 0 { 5 | return 6 | } 7 | 8 | for i := 0; i < len(board); i++ { 9 | walkThroughBoard(i, 0, board) 10 | walkThroughBoard(i, len(board[0]) - 1, board) 11 | } 12 | 13 | for j := 0; j < len(board[0]); j++ { 14 | walkThroughBoard(0, j, board) 15 | walkThroughBoard(len(board) - 1, j, board) 16 | } 17 | 18 | for i := range board { 19 | for j := range board[i] { 20 | if board[i][j] == 'S' { 21 | board[i][j] = 'O' 22 | } else { 23 | board[i][j] = 'X' 24 | } 25 | } 26 | } 27 | } 28 | 29 | func walkThroughBoard(i, j int, board [][]byte) { 30 | if i < 0 || j < 0 || i >= len(board) || j >= len(board[0]) { 31 | return 32 | } 33 | 34 | if board[i][j] == 'O' { 35 | board[i][j] = 'S' 36 | 37 | walkThroughBoard(i - 1, j, board) 38 | walkThroughBoard(i + 1, j, board) 39 | walkThroughBoard(i, j - 1, board) 40 | walkThroughBoard(i, j + 1, board) 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /solutions/433.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | func minMutation(start string, end string, bank []string) int { 4 | set := make(map[string]struct{}) 5 | 6 | for _, value := range bank { 7 | set[value] = struct{}{} 8 | } 9 | 10 | queue := []string{start} 11 | i := 0 12 | 13 | for len(queue) != 0 { 14 | i++ 15 | length := len(queue) 16 | 17 | for _, firstValue := range queue { 18 | for secondValue := range set { 19 | if isNear(firstValue, secondValue) { 20 | if secondValue == end { 21 | return i 22 | } 23 | 24 | queue = append(queue, secondValue) 25 | 26 | delete(set, secondValue) 27 | } 28 | } 29 | } 30 | 31 | queue = queue[length:] 32 | } 33 | 34 | return -1 35 | } 36 | 37 | func isNear(firstString string, secondString string) bool { 38 | mutation := 0 39 | 40 | for i := 0; i < 8; i++ { 41 | if firstString[i] != secondString[i] { 42 | mutation++ 43 | } 44 | } 45 | 46 | return mutation == 1 47 | } -------------------------------------------------------------------------------- /solutions/40.go: -------------------------------------------------------------------------------- 1 | package solutions 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func combinationSum2(candidates []int, target int) [][]int { 8 | sort.Ints(candidates) 9 | 10 | line := make([]int, 0) 11 | result := make([][]int, 0) 12 | 13 | calc(0, target, candidates, line, &result) 14 | 15 | return result 16 | } 17 | 18 | func calc(index int, target int, candidates []int, line []int, result *[][]int){ 19 | for i := index; i < len(candidates); { 20 | line = append(line, candidates[i]) 21 | sum := 0 22 | 23 | for _, value := range line { 24 | sum += value 25 | } 26 | 27 | if sum == target { 28 | temp := make([]int, len(line)) 29 | copy(temp, line) 30 | *result = append(*result, temp) 31 | 32 | return 33 | } 34 | 35 | if sum > target { 36 | return 37 | } 38 | 39 | calc(i + 1, target, candidates, line, result) 40 | line = line[:len(line) - 1] 41 | i++ 42 | 43 | for ; i < len(candidates) && candidates[i] == candidates[i - 1]; { 44 | i++ 45 | } 46 | } 47 | } 48 | --------------------------------------------------------------------------------