An open API service indexing awesome lists of open source software.

https://github.com/botengyao/leetcode_go

Leetcode by Go
https://github.com/botengyao/leetcode_go

go leetcode-golang leetcode-solutions

Last synced: 7 months ago
JSON representation

Leetcode by Go

Awesome Lists containing this project

README

          

# Leetcode_go_python
Go and Python solutions for Leetcode. (😁Updating)

## Array

| Title | Solution | Difficulty | Time | Space | Like | Mark |
| ----- | :--------: | :----------: | :----: | :-----: | :---: | :----------: |
|[1. Two Sum](https://leetcode.com/problems/two-sum/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/array/1.Twosum.go)| Easy | O(N)| O(N)|||
|[11. Contaniner with most water](https://leetcode.com/problems/container-with-most-water/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/array/11.Container_With_Most_Water.go)| Medium| O(N)| O(1)||Two pointers (move small)|
|[15. 3 Sum](https://leetcode.com/problems/3sum)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/array/15.3sum.go)| Medium | O(N^2)| O(N)|||
|[16. 3 Sum Closest](https://leetcode.com/problems/3sum-closest)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/array/16.3Sum_Closest.go)| Medium | O(N^2)| O(1)|||
|[33. Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/array/33.Search_in_Rotated_Sorted_Array.go)| Medium | O(lg(N))| O(1)||Binary compare lo|
|[39. Combination Sum](https://leetcode.com/problems/combination-sum)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/array/39.Combination_sum.go)| Medium | O(2^N)| O(N)|||
|[40. Combination Sum II](https://leetcode.com/problems/combination-sum-ii)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/array/40.Combination_sum2.go)| Medium | O(2^N)| O(N)|||
|[53. Maximum Subarray](https://leetcode.com/problems/maximum-subarray)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/array/53.Maximum_Subarray.go)| Medium | O(N)| O(1)||DP or Divide|
|[84. Largest Rectangle in Histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/array/84.Largest_Rectangle_in_Histogram.go)| Hard | O(N)| O(N)|🌹|Monotonous stack|
|[122. Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/array/122.Best_Time_to_Buy_and_Sell_II.go)| Easy | O(N)| O(1)|||
|[136. Single number](https://leetcode.com/problems/single-number/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/array/136.Single_number.go)| Easy | O(N)| O(1)|🌹|Bit manipulation|
|[283. Move zeros](https://leetcode.com/problems/move-zeros/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/array/283.Move_Zeroes.go)| Easy | O(N)| O(1)|||
|[311. Sparse Matrix Multiplication](https://leetcode.com/problems/sparse-matrix-multiplication/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/array/311.Sparse_Matrix_Multiplication.go)| Medium | O(N*M)| O(N*M)|||
|[525. Contiguous Array](https://leetcode.com/problems/contiguous-array/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/array/525.Contiguous_Array.go)| Medium | O(N)| O(N)||diff = 0|

## String
| Title | Solution | Difficulty | Time | Space | Like | Mark |
| ----- | :--------: | :----------: | :----: | :-----: | :-----: | :----------: |
|[49. Group Anagrams](https://leetcode.com/problems/group-anagrams/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/string/49._Group_Anagrams.go)| Medium | O(Nk) | O(Nk)|| sort/count|
|[67. Add Binary](https://leetcode.com/problems/add-binary/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/string/67.Add_Binary.go)| Easy | O(Max(N) | O(Max(N))|||
|[273. Integer to English Words](https://leetcode.com/problems/integer-to-english-words/)| [Python](https://github.com/calmbryan/Leetcode_Go/blob/master/string/273.Integer_to_English_Words.py)| Hard | O(len(digits)) | O(1)|| Devide and Conquer|
|[438. Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/string/438.Find_All_Anagrams_in_a_String.go)| Medium | O(N) 26 | O(M)|||
|[844. Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/string/844.Backspace_String_Compare.go)| Medium | O(N) | O(1)|| pointer(end -> start) |
|[953. Verifying an Alien Dictionary](https://leetcode.com/problems/verifying-an-alien-dictionary/)| [Python](https://github.com/calmbryan/Leetcode_Go/blob/master/string/953.Verifying_an_Alien_Dictionary.py)| Easy | O(K*N) | O(1)|||

## Permutation

| Title | Solution | Difficulty | Time | Space | Like | Mark |
| ----- | :--------: | :----------: | :----: | :-----: | :-----: | :----------: |
|[60. Permutation Sequence](https://leetcode.com/problems/permutation-sequence)| [Python](https://github.com/calmbryan/Leetcode_Go/blob/master/permutation/60permutation_sequence.py)| Medium | O(N^2) | O(N)||right to left, (n-1)!|
|[31. Next Permutation](https://leetcode.com/problems/next-permutation/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/permutation/31.Next_permutation.go)| Medium | O(N) | O(1)|||

## Parentheses
| Title | Solution | Difficulty | Time | Space | Like | Mark |
| ----- | :--------: | :----------: | :----: | :-----: | :-----: | :----------: |
|[301. Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/string/301.Remove_Invalid_Parentheses.go)| Hard | O(N * 2^N) | O(2^N)||bt, memo, pruning|
|[678. Valid Parenthesis String](https://leetcode.com/problems/valid-parenthesis-string/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/string/678.Valid_Parenthesis_String.go)| Medium | O(N) | O(1)|🌹||
|[1249. Minimum Remove to Make Valid Parentheses](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/string/1249.Minimum_Remove_to_Make_Valid_Parentheses.go)| Medium | O(N) | O(N)||check open and close|

## Graph
| Title | Solution | Difficulty | Time | Space | Like | Mark |
| ----- | :--------: | :----------: | :----: | :-----: | :-----: | :----------: |

## Tree
| Title | Solution | Difficulty | Time | Space | Like | Mark |
| ----- | :--------: | :----------: | :----: | :-----: | :-----: | :----------: |
|[95. Unique Binary Search Trees II](https://leetcode.com/problems/unique-binary-search-trees-ii/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/tree/95.Unique_Binary_Search_TreesII.go)| Medium | O(N * Gn) | O(N*Gn)||(lo, hi), DFS, Memo|
|[96. Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/tree/96.Unique_Binary_Search_Trees.go)| Medium | O(N^2) | O(N)||Catalan number|
|[124. Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/tree/124.Binary_Tree_Maximum_Path_Sum.go)| Hard | O(N)) | O(h)|||
|[199. Binary Tree Right Side View](https://leetcode.com/problems/binary-tree-right-side-view/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/tree/199.Binary_Tree_Right_Side_View.go) [Python](https://github.com/calmbryan/Leetcode_Go/blob/master/tree/199.Binary_Tree_Right_Side_DFS.py)| Medium | O(N)) | O(h)||BFS/DFS|
|[222. Count Complete Tree Nodes](https://leetcode.com/problems/count-complete-tree-nodes/)| [Python](https://github.com/calmbryan/Leetcode_Go/blob/master/tree/222.Count_Complete_Tree_Nodes.py)| Medium | O(d^2)) | O(1)|🌹|bs + bs|
|[543. Diameter of Binary Tree](https://leetcode.com/problems/backspace-string-compare/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/tree/543.Diameter_of_Binary_Tree.go)| Easy | O(N) | O(N)|||
|[1008. Construct Binary Search Tree from Preorder Traversal](https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/tree/1008.Construct_Binary_Search_Tree_from_Preorde.go)| Medium | O(N) | O(N)|||

## Dynamic Programing

| Title | Solution | Difficulty | Time | Space | Like | Mark |
| ----- | :--------: | :----------: | :----: | :-----: | :-----: | :----------: |
|[64. Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/dp/64.Minimum_Path_Sum.go)| Medium | O(MN)| O(1)|| see 174 |
|[174. Dungeon Game](https://leetcode.com/problems/dungeon-game/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/dp/174.Dungeon_Game.go)| Hard | O(MN)| O(1)|🌹| bottom-right to top-left|
|[887. Super Egg Drop](https://leetcode.com/problems/super-egg-drop/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/dp/887.Super_Egg_Drop.go)| Hard | O(N^1/2)| O(KM)|🌹||

## DFS/Memo
| Title | Solution | Difficulty | Time | Space | Like | Mark |
| ----- | :--------: | :----------: | :----: | :-----: | :-----: | :----------: |

## Linked List

| Title | Solution | Difficulty | Time | Space | Like | Mark |
| ----- | :--------: | :----------: | :----: | :-----: | :-----: | :----------: |
|[202. Happy Number](https://leetcode.com/problems/happy-number/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/array/202.Happy_Number.go)| Easy | O(log(N)) | O(1)|🌹|Hare and tortoise|
|[206. Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)| [Go](https://github.com/calmbryan/Leetcode_Go/blob/master/linkedlist/206.Reverse_Linked_List.go)| Easy | O(N) | O(1)|||
|[30-day First Unique Number](https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/531/week-4/3313/)| [Go solution](https://github.com/calmbryan/Leetcode_Go/blob/master/linkedlist/First_Unique_Number.go)| Easy | O(1) | O(1)||Double-Linked-List|

## Fundamental

### 1. Bit manipulation
##### (1). MIN and MAX
```go
//Int32Max Max int 0111 1111 1111 11..
const Int32Max int = int(^uint(0) >> 1)

//Int32Min Min int 1000 0000 0000 00..
const Int32Min int = ^Int32Max
```

##### (2). Add Binary
```go
//a = 1111
//b = 0010
//Sum without carry: a ^ b = 1101
//Carry a & b = 0010 --> (a & b) << 1 = 0100
//Loop until carry become 0
//cannot handle the situation that the number of bits is over 64...
func addBinary2(a string, b string) string {
binA, _ := strconv.ParseInt(a, 2, 64)
binB, _ := strconv.ParseInt(b, 2, 64)
var answer int64
var carry int64
for binB != 0 {
answer = binA ^ binB //The sum results without carray
carry = (binA & binB) << 1 //The carry
binA, binB = answer, carry
}
return strconv.FormatInt(binA, 2)

}
```
##### (3). Single Number
```go
//Only one is single and others appear twice.
func SingleNumber(nums []int) int {
// Bit Mapulation
// a ^ 0 = a
// a ^ a = 0
// a ^ b ^ b = a ^ 0 = a
var res int
for _, num := range nums {
res ^= num
}
return res

```
### 2. Go Fundamental
#### Slice
```go
var s1 []int // == nil
s2 := *new([]int) // == nil

var s3 = []int{} // empty slice != nil
var s4 = make([]int, 0) // empty slice != nil
//All can use s1 = append(s1, a)
```

```go
res = append(res, s)
*res = append(*res, s)
```
`*res = append(*res, s)` means that a pointer points to this slice, and the slice is a struct which has three fileds: cap, len, array. When append function is used, the base array address may be changed because a new array with big capacity is allocated. Therefore, when we pass a slice into a function as a result set, try to pass the pointer of the slice instead of the slice struct itself.

#### Map
This variable m is a map of string keys to int values:
```go
var m map[string]int
```
Map types are reference types, like pointers or slices, and so the value of m above is nil; it doesn't point to an initialized map. A nil map behaves like an empty map when reading, but attempts to write to a nil map will cause a runtime panic; don't do that.
To initialize a map, use the built in make function:

```go
m = make(map[string]int)
```
The same syntax may be used to initialize an empty map, which is functionally identical to using the make function:
```go
m = map[string]int{}
```

No set in Go, so a map can be used. `map[T]struct{}` is better than `map[T]bool` because `struct{}` doesn't occupy memory:
```go
set := make(map[int]struct{})
set[v] = struct{}{}s
```

How to compare two maps?
```go
if reflect.DeepEqual(slide, memo) {
res = append(res, i-len(p)+1)
}
```

#### String
```go
//https://gobyexample.com/string-functions
package main

import (
"fmt"
"strings"
)

var p = fmt.Println

func main() {

p("Contains: ", strings.Contains("test", "es")) //true
p("Count: ", strings.Count("test", "t")) //2
p("HasPrefix: ", strings.HasPrefix("test", "te")) //true
p("HasSuffix: ", strings.HasSuffix("test", "st")) //true
p("Index: ", strings.Index("test", "e")) //1
p("Join: ", strings.Join([]string{"a", "b"}, "-")) //a-b
p("Repeat: ", strings.Repeat("a", 5)) //aaaaa
p("Replace: ", strings.Replace("foooo", "o", "0", -1)) //f0000
p("Replace: ", strings.Replace("foooo", "o", "0", 1)) //f0ooo
p("Split: ", strings.Split("a-b-c-d", "-")) //["a","b","c","d"]
p("ToLower: ", strings.ToLower("TEST"))
p("ToUpper: ", strings.ToUpper("test"))
p()

p("Len: ", len("hello")) //5
p("Char:", "hello"[1]) //e

str := []rune("abcd") //[a, b, c, d]
str2 := []rune("你好") //[你,好]
p(len("你好")) //6
p(len(str2)) //2
p(str)
}

```