Skip to content

Commit adc3a5b

Browse files
committed
docs: update readme
0 parents  commit adc3a5b

File tree

333 files changed

+17300
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

333 files changed

+17300
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bin

Makefile

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Targets not related to individual files
2+
.PHONY: all test cover_func cover_html out clean vet loc fmt test_v bench
3+
4+
BUILD_OUT_DIR = bin
5+
TEST_COVERAGE_PROFILE = coverage.out
6+
7+
all: out fmt vet test_v loc solved
8+
9+
out:
10+
mkdir -p $(BUILD_OUT_DIR)
11+
12+
fmt:
13+
go fmt ./...
14+
15+
vet:
16+
go vet ./...
17+
18+
test: out
19+
go test -race ./... -coverprofile=$(BUILD_OUT_DIR)/$(TEST_COVERAGE_PROFILE)
20+
21+
test_v: out
22+
go test -race -v ./... -coverprofile=$(BUILD_OUT_DIR)/$(TEST_COVERAGE_PROFILE)
23+
24+
bench:
25+
go test -bench=.
26+
27+
cover_func: test
28+
go tool cover -func=$(BUILD_OUT_DIR)/$(TEST_COVERAGE_PROFILE)
29+
30+
cover_html: test
31+
go tool cover -html=$(BUILD_OUT_DIR)/$(TEST_COVERAGE_PROFILE)
32+
33+
clean:
34+
rm -rf $(BUILD_OUT_DIR)
35+
36+
solved:
37+
ls -ld -- */ | wc -l
38+
39+
loc:
40+
find . -type f -not -path "./vendor/*" -name "*.go" | xargs wc -l

README.md

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# go-leetcode
2+
3+
A collection of 100+ popular [LeetCode](https://leetcode.com/) problems solved in
4+
[Go](https://golang.org/).
5+
6+
Each directory includes a:
7+
- Description with link to LeetCode problem
8+
- Solution to the problem
9+
- A unit test
10+
11+
Note that each of these problems **have passed** their respective test cases on LeetCode. The unit
12+
tests included with each solution in this repo are not comprehensive. They serve as a quick way to
13+
drop in a test case, hook up the debugger, and step through the algorithms to build understanding.
14+
15+
## Problems
16+
17+
There are a variety of popular LeetCode problems solved in this repository. However, most of the problems
18+
solved are from a highly recommended, curated list of problems called
19+
[Top 75 LeetCode Problems](https://www.teamblind.com/post/New-Year-Gift---Curated-List-of-Top-100-LeetCode-Questions-to-Save-Your-Time-OaM1orEU).
20+
21+
The [Top 75 LeetCode Problems](https://www.teamblind.com/post/New-Year-Gift---Curated-List-of-Top-100-LeetCode-Questions-to-Save-Your-Time-OaM1orEU)
22+
list is comprehensive, covering a decent breadth and depth for each category. Working through that list will certainly
23+
help build the core concepts, techniques, and intuition needed to solve these types of problems.
24+
25+
I've included a checklist that you can use to work through the list below:
26+
27+
#### Array
28+
29+
- [X] Two Sum - [https://leetcode.com/problems/two-sum/](https://leetcode.com/problems/two-sum/)
30+
- [X] Best Time to Buy and Sell Stock - [https://leetcode.com/problems/best-time-to-buy-and-sell-stock/](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)
31+
- [X] Contains Duplicate - [https://leetcode.com/problems/contains-duplicate/](https://leetcode.com/problems/contains-duplicate/)
32+
- [X] Product of Array Except Self - [https://leetcode.com/problems/product-of-array-except-self/](https://leetcode.com/problems/product-of-array-except-self/)
33+
- [X] Maximum Subarray - [https://leetcode.com/problems/maximum-subarray/](https://leetcode.com/problems/maximum-subarray/)
34+
- [X] Maximum Product Subarray - [https://leetcode.com/problems/maximum-product-subarray/](https://leetcode.com/problems/maximum-product-subarray/)
35+
- [X] Find Minimum in Rotated Sorted Array - [https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)
36+
- [X] Search in Rotated Sorted Array - [https://leetcode.com/problems/search-in-rotated-sorted-array/](https://leetcode.com/problems/search-in-rotated-sorted-array/)
37+
- [X] 3Sum - [https://leetcode.com/problems/3sum/](https://leetcode.com/problems/3sum/)
38+
- [X] Container With Most Water - [https://leetcode.com/problems/container-with-most-water/](https://leetcode.com/problems/container-with-most-water/)
39+
40+
---
41+
42+
#### Binary
43+
44+
- [X] Sum of Two Integers - [https://leetcode.com/problems/sum-of-two-integers/](https://leetcode.com/problems/sum-of-two-integers/)
45+
- [X] Number of 1 Bits - [https://leetcode.com/problems/number-of-1-bits/](https://leetcode.com/problems/number-of-1-bits/)
46+
- [X] Counting Bits - [https://leetcode.com/problems/counting-bits/](https://leetcode.com/problems/counting-bits/)
47+
- [X] Missing Number - [https://leetcode.com/problems/missing-number/](https://leetcode.com/problems/missing-number/)
48+
- [X] Reverse Bits - [https://leetcode.com/problems/reverse-bits/](https://leetcode.com/problems/reverse-bits/)
49+
50+
---
51+
52+
#### Dynamic Programming
53+
54+
- [X] Climbing Stairs - [https://leetcode.com/problems/climbing-stairs/](https://leetcode.com/problems/climbing-stairs/)
55+
- [X] Coin Change - [https://leetcode.com/problems/coin-change/](https://leetcode.com/problems/coin-change/)
56+
- [X] Longest Increasing Subsequence - [https://leetcode.com/problems/longest-increasing-subsequence/](https://leetcode.com/problems/longest-increasing-subsequence/)
57+
- [X] Longest Common Subsequence - [https://leetcode.com/problems/longest-common-subsequence/](https://leetcode.com/problems/longest-common-subsequence/)
58+
- [X] Word Break Problem - [https://leetcode.com/problems/word-break/](https://leetcode.com/problems/word-break/)
59+
- [X] Combination Sum - [https://leetcode.com/problems/combination-sum-iv/](https://leetcode.com/problems/combination-sum-iv/)
60+
- [X] House Robber - [https://leetcode.com/problems/house-robber/](https://leetcode.com/problems/house-robber/)
61+
- [X] House Robber II - [https://leetcode.com/problems/house-robber-ii/](https://leetcode.com/problems/house-robber-ii/)
62+
- [X] Decode Ways - [https://leetcode.com/problems/decode-ways/](https://leetcode.com/problems/decode-ways/)
63+
- [X] Unique Paths - [https://leetcode.com/problems/unique-paths/](https://leetcode.com/problems/unique-paths/)
64+
- [X] Jump Game - [https://leetcode.com/problems/jump-game/](https://leetcode.com/problems/jump-game/)
65+
66+
---
67+
68+
#### Graph
69+
70+
- [X] Clone Graph - [https://leetcode.com/problems/clone-graph/](https://leetcode.com/problems/clone-graph/)
71+
- [X] Course Schedule - [https://leetcode.com/problems/course-schedule/](https://leetcode.com/problems/course-schedule/)
72+
- [X] Pacific Atlantic Water Flow - [https://leetcode.com/problems/pacific-atlantic-water-flow/](https://leetcode.com/problems/pacific-atlantic-water-flow/)
73+
- [X] Number of Islands - [https://leetcode.com/problems/number-of-islands/](https://leetcode.com/problems/number-of-islands/)
74+
- [X] Longest Consecutive Sequence - [https://leetcode.com/problems/longest-consecutive-sequence/](https://leetcode.com/problems/longest-consecutive-sequence/)
75+
- [X] Alien Dictionary (Leetcode Premium) - [https://leetcode.com/problems/alien-dictionary/](https://leetcode.com/problems/alien-dictionary/)
76+
- [X] Graph Valid Tree (Leetcode Premium) - [https://leetcode.com/problems/graph-valid-tree/](https://leetcode.com/problems/graph-valid-tree/)
77+
- [X] Number of Connected Components in an Undirected Graph (Leetcode Premium) - [https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/)
78+
79+
---
80+
81+
#### Interval
82+
83+
- [X] Insert Interval - [https://leetcode.com/problems/insert-interval/](https://leetcode.com/problems/insert-interval/)
84+
- [X] Merge Intervals - [https://leetcode.com/problems/merge-intervals/](https://leetcode.com/problems/merge-intervals/)
85+
- [X] Non-overlapping Intervals - [https://leetcode.com/problems/non-overlapping-intervals/](https://leetcode.com/problems/non-overlapping-intervals/)
86+
- [X] Meeting Rooms (Leetcode Premium) - [https://leetcode.com/problems/meeting-rooms/](https://leetcode.com/problems/meeting-rooms/)
87+
- [X] Meeting Rooms II (Leetcode Premium) - [https://leetcode.com/problems/meeting-rooms-ii/](https://leetcode.com/problems/meeting-rooms-ii/)
88+
89+
---
90+
91+
#### Linked List
92+
93+
- [X] Reverse a Linked List - [https://leetcode.com/problems/reverse-linked-list/](https://leetcode.com/problems/reverse-linked-list/)
94+
- [X] Detect Cycle in a Linked List - [https://leetcode.com/problems/linked-list-cycle/](https://leetcode.com/problems/linked-list-cycle/)
95+
- [X] Merge Two Sorted Lists - [https://leetcode.com/problems/merge-two-sorted-lists/](https://leetcode.com/problems/merge-two-sorted-lists/)
96+
- [X] Merge K Sorted Lists - [https://leetcode.com/problems/merge-k-sorted-lists/](https://leetcode.com/problems/merge-k-sorted-lists/)
97+
- [X] Remove Nth Node From End Of List - [https://leetcode.com/problems/remove-nth-node-from-end-of-list/](https://leetcode.com/problems/remove-nth-node-from-end-of-list/)
98+
- [X] Reorder List - [https://leetcode.com/problems/reorder-list/](https://leetcode.com/problems/reorder-list/)
99+
100+
---
101+
102+
#### Matrix
103+
104+
- [X] Set Matrix Zeroes - [https://leetcode.com/problems/set-matrix-zeroes/](https://leetcode.com/problems/set-matrix-zeroes/)
105+
- [X] Spiral Matrix - [https://leetcode.com/problems/spiral-matrix/](https://leetcode.com/problems/spiral-matrix/)
106+
- [X] Rotate Image - [https://leetcode.com/problems/rotate-image/](https://leetcode.com/problems/rotate-image/)
107+
- [X] Word Search - [https://leetcode.com/problems/word-search/](https://leetcode.com/problems/word-search/)
108+
109+
---
110+
111+
#### String
112+
113+
- [X] Longest Substring Without Repeating Characters - [https://leetcode.com/problems/longest-substring-without-repeating-characters/](https://leetcode.com/problems/longest-substring-without-repeating-characters/)
114+
- [X] Longest Repeating Character Replacement - [https://leetcode.com/problems/longest-repeating-character-replacement/](https://leetcode.com/problems/longest-repeating-character-replacement/)
115+
- [X] Minimum Window Substring - [https://leetcode.com/problems/minimum-window-substring/](https://leetcode.com/problems/minimum-window-substring/)
116+
- [X] Valid Anagram - [https://leetcode.com/problems/valid-anagram/](https://leetcode.com/problems/valid-anagram/)
117+
- [X] Group Anagrams - [https://leetcode.com/problems/group-anagrams/](https://leetcode.com/problems/group-anagrams/)
118+
- [X] Valid Parentheses - [https://leetcode.com/problems/valid-parentheses/](https://leetcode.com/problems/valid-parentheses/)
119+
- [X] Valid Palindrome - [https://leetcode.com/problems/valid-palindrome/](https://leetcode.com/problems/valid-palindrome/)
120+
- [X] Longest Palindromic Substring - [https://leetcode.com/problems/longest-palindromic-substring/](https://leetcode.com/problems/longest-palindromic-substring/)
121+
- [X] Palindromic Substrings - [https://leetcode.com/problems/palindromic-substrings/](https://leetcode.com/problems/palindromic-substrings/)
122+
- [X] Encode and Decode Strings (Leetcode Premium) - [https://leetcode.com/problems/encode-and-decode-strings/](https://leetcode.com/problems/encode-and-decode-strings/)
123+
124+
---
125+
126+
#### Tree
127+
128+
- [X] Maximum Depth of Binary Tree - [https://leetcode.com/problems/maximum-depth-of-binary-tree/](https://leetcode.com/problems/maximum-depth-of-binary-tree/)
129+
- [X] Same Tree - [https://leetcode.com/problems/same-tree/](https://leetcode.com/problems/same-tree/)
130+
- [X] Invert/Flip Binary Tree - [https://leetcode.com/problems/invert-binary-tree/](https://leetcode.com/problems/invert-binary-tree/)
131+
- [X] Binary Tree Maximum Path Sum - [https://leetcode.com/problems/binary-tree-maximum-path-sum/](https://leetcode.com/problems/binary-tree-maximum-path-sum/)
132+
- [X] Binary Tree Level Order Traversal - [https://leetcode.com/problems/binary-tree-level-order-traversal/](https://leetcode.com/problems/binary-tree-level-order-traversal/)
133+
- [X] Serialize and Deserialize Binary Tree - [https://leetcode.com/problems/serialize-and-deserialize-binary-tree/](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/)
134+
- [X] Subtree of Another Tree - [https://leetcode.com/problems/subtree-of-another-tree/](https://leetcode.com/problems/subtree-of-another-tree/)
135+
- [X] Construct Binary Tree from Preorder and Inorder Traversal - [https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)
136+
- [X] Validate Binary Search Tree - [https://leetcode.com/problems/validate-binary-search-tree/](https://leetcode.com/problems/validate-binary-search-tree/)
137+
- [X] Kth Smallest Element in a BST - [https://leetcode.com/problems/kth-smallest-element-in-a-bst/](https://leetcode.com/problems/kth-smallest-element-in-a-bst/)
138+
- [X] Lowest Common Ancestor of BST - [https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)
139+
- [X] Implement Trie (Prefix Tree) - [https://leetcode.com/problems/implement-trie-prefix-tree/](https://leetcode.com/problems/implement-trie-prefix-tree/)
140+
- [X] Add and Search Word - [https://leetcode.com/problems/add-and-search-word-data-structure-design/](https://leetcode.com/problems/add-and-search-word-data-structure-design/)
141+
- [X] Word Search II - [https://leetcode.com/problems/word-search-ii/](https://leetcode.com/problems/word-search-ii/)
142+
143+
---
144+
145+
#### Heap
146+
147+
- [X] Merge K Sorted Lists - [https://leetcode.com/problems/merge-k-sorted-lists/](https://leetcode.com/problems/merge-k-sorted-lists/)
148+
- [X] Top K Frequent Elements - [https://leetcode.com/problems/top-k-frequent-elements/](https://leetcode.com/problems/top-k-frequent-elements/)
149+
- [X] Find Median from Data Stream - [https://leetcode.com/problems/find-median-from-data-stream/](https://leetcode.com/problems/find-median-from-data-stream/)

add_two_numbers_2/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 2. Add Two Numbers
2+
3+
https://leetcode.com/problems/add-two-numbers/
4+
5+
# Description
6+
7+
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
8+
9+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
10+
11+
Example:
12+
```
13+
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
14+
Output: 7 -> 0 -> 8
15+
Explanation: 342 + 465 = 807.
16+
```

add_two_numbers_2/solution.go

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package add_two_numbers_2
2+
3+
import (
4+
"github.com/austingebauer/leetcode/structures"
5+
"math/big"
6+
)
7+
8+
func addTwoNumbers(l1 *structures.ListNode, l2 *structures.ListNode) *structures.ListNode {
9+
num1 := depthFirstNum(l1)
10+
num2 := depthFirstNum(l2)
11+
12+
var sum big.Int
13+
sum.Add(num1, num2)
14+
15+
// single digit sum
16+
if len(sum.String()) == 1 {
17+
return &structures.ListNode{
18+
Val: int(sum.Int64()),
19+
Next: nil,
20+
}
21+
}
22+
23+
var sumList *structures.ListNode
24+
var current *structures.ListNode
25+
26+
for sum.String() != "0" {
27+
var lastDigit big.Int
28+
lastDigit.Mod(&sum, big.NewInt(10))
29+
30+
node := &structures.ListNode{
31+
Val: int(lastDigit.Int64()),
32+
Next: nil,
33+
}
34+
if sumList == nil {
35+
sumList = node
36+
}
37+
if current == nil {
38+
current = node
39+
} else {
40+
current.Next = node
41+
current = current.Next
42+
}
43+
44+
sum.Div(&sum, big.NewInt(10))
45+
}
46+
47+
return sumList
48+
}
49+
50+
func depthFirstNum(l *structures.ListNode) *big.Int {
51+
if l == nil {
52+
return big.NewInt(0)
53+
}
54+
55+
v := depthFirstNum(l.Next)
56+
return v.Mul(v, big.NewInt(10)).Add(v, big.NewInt(int64(l.Val)))
57+
}
58+
59+
func addTwoNumbers2(l1 *structures.ListNode, l2 *structures.ListNode) *structures.ListNode {
60+
carry := 0
61+
sumList := &structures.ListNode{
62+
Val: 0,
63+
Next: nil,
64+
}
65+
frontSumList := sumList
66+
67+
for l1 != nil || l2 != nil {
68+
// calculate sum and carry values
69+
l1Val := 0
70+
l2Val := 0
71+
if l1 != nil {
72+
l1Val = l1.Val
73+
l1 = l1.Next
74+
}
75+
if l2 != nil {
76+
l2Val = l2.Val
77+
l2 = l2.Next
78+
}
79+
80+
sum := l1Val + l2Val + carry
81+
carry = sum / 10
82+
sumList.Val = sum % 10
83+
84+
// no more list to process, but carry is not 0
85+
if l1 == nil && l2 == nil && carry > 0 {
86+
sumList.Next = &structures.ListNode{
87+
Val: carry,
88+
Next: nil,
89+
}
90+
}
91+
92+
// one or both lists still can be processed
93+
if l1 != nil || l2 != nil {
94+
sumList.Next = &structures.ListNode{
95+
Val: 0,
96+
Next: nil,
97+
}
98+
sumList = sumList.Next
99+
}
100+
}
101+
102+
return frontSumList
103+
}

0 commit comments

Comments
 (0)