Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ce9ebd6

Browse files
go: 19,98,101,189,204,206,326
1 parent db47535 commit ce9ebd6

File tree

10 files changed

+323
-9
lines changed

10 files changed

+323
-9
lines changed

‎README.md‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -879,11 +879,11 @@ You can also ask for problem solving ideas and discuss in GitHub issues directly
879879
| # | Title | Solutions | Difficulty | Tag
880880
|-----|----------------|:---------------:|:--------:|:-------------:
881881
|1|[Two Sum](https://leetcode.com/problems/two-sum/description/)|[Java](../master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/hash_table/_1.java) & [Go](../master/codes/go/leetcodes/interview/easy_collection/1.go) |Easy| Array
882-
|7|[Reverse Integer](https://leetcode.com/problems/reverse-integer/description/)|[Java](../master/codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/strings/_7.java) & & [Go](../master/codes/go/leetcodes/interview/easy_collection/7.go) |Easy| Strings
882+
|7|[Reverse Integer](https://leetcode.com/problems/reverse-integer/description/)|[Java](../master/codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/strings/_7.java) & [Go](../master/codes/go/leetcodes/interview/easy_collection/7.go) |Easy| Strings
883883
|8|[String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/strings/_8.java) & Python |Medium| Strings
884884
|13|[Roman to Integer](https://leetcode.com/problems/roman-to-integer/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/math/_13.java) & Python |Easy| Math
885885
|14|[Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_14.java) & Python|Easy| Strings
886-
|19|[Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/linked_list/_19.java) & Python|Medium| Linked List
886+
|19|[Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/linked_list/_19.java) & [Go](../master/codes/go/leetcodes/interview/easy_collection/19.go)|Medium| Linked List
887887
|20|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/queue_stack/_20.java) & Python|Easy| Others
888888
|21|[Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/linked_list/_21.java) & [Go](../master/codes/go/leetcodes/interview/easy_collection/21.go)|Easy| Linked List
889889
|26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_26.java) & [Go](../master/codes/go/leetcodes/interview/easy_collection/26.go)|Easy| Array
@@ -895,8 +895,8 @@ You can also ask for problem solving ideas and discuss in GitHub issues directly
895895
|66|[Plus One](https://leetcode.com/problems/plus-one/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_66.java) & [Go](../master/codes/go/leetcodes/interview/easy_collection/66.go)|Easy| Array
896896
|70|[Climbing Stairs](https://leetcode.com/problems/climbing-stairs/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_70.java) & Python| Easy| Dynamic Programming
897897
|88|[Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/sorting_and_searching/_88.java) & Python |Easy| Sorting and Searching
898-
|98|[Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/binary_search_tree/_98.java) & Python |Medium| Trees
899-
|101|[Symmetric Tree](https://leetcode.com/problems/symmetric-tree/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/binary_tree/_101.java) & Python |Easy| Trees
898+
|98|[Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/binary_search_tree/_98.java) & [Go](../master/codes/go/leetcodes/interview/easy_collection/98.go) |Medium| Trees
899+
|101|[Symmetric Tree](https://leetcode.com/problems/symmetric-tree/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/binary_tree/_101.java) & [Go](../master/codes/go/leetcodes/interview/easy_collection/101.go) |Easy| Trees
900900
|102|[Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/binary_tree/_102.java) & Python |Medium| Trees
901901
|104|[Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/binary_tree/_104.java) & Python |Easy| Trees
902902
|108|[Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/binary_search_tree/_108.java) & Python |Easy| Trees
@@ -907,20 +907,20 @@ You can also ask for problem solving ideas and discuss in GitHub issues directly
907907
|136|[Single Number](https://leetcode.com/problems/single-number/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/hash_table/_136.java) & [Go](../master/codes/go/leetcodes/interview/easy_collection/136.go) |Easy| Array
908908
|141|[Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/linked_list/_141.java) & [Go](../master/codes/go/leetcodes/interview/easy_collection/141.go)|Easy| Linked List
909909
|155|[Min Stack](https://leetcode.com/problems/min-stack/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/queue_stack/_155.java) & Python|Easy| Design
910-
|189|[Rotate Array](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_189.java) & Python| Easy| Array
910+
|189|[Rotate Array](https://leetcode.com/problems/rotate-array/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_189.java) & [Go](../master/codes/go/leetcodes/interview/easy_collection/189.go)| Easy| Array
911911
|190|[Reverse Bits](https://leetcode.com/problems/reverse-bits/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/others/_190.java) & Python |Easy| Others
912912
|191|[Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/description/)|[Java](../master/codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/others/_191.java) & [Go](../master/codes/go/leetcodes/interview/easy_collection/191.go) |Easy| Others
913913
|198|[House Robber](https://leetcode.com/problems/house-robber/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/dynamic_programming/_198.java) & Python |Easy| Dynamic Programming
914-
|204|[Count Primes](https://leetcode.com/problems/count-primes/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/math/_204.java) & Python |Easy| Math
915-
|206|[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/linked_list/_206.java) & Python|Easy| Linked List
914+
|204|[Count Primes](https://leetcode.com/problems/count-primes/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/math/_204.java) & [Go](../master/codes/go/leetcodes/interview/easy_collection/204.go) |Easy| Math
915+
|206|[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/linked_list/_206.java) & [Go](../master/codes/go/leetcodes/interview/easy_collection/206.go)|Easy| Linked List
916916
|217|[Contains Duplicate](https://leetcode.com/problems/contains-duplicate/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/hash_table/_217.java) & [Go](../master/codes/go/leetcodes/interview/easy_collection/217.go) |Easy| Array
917917
|234|[Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/linked_list/_234.java) & [Go](../master/codes/go/leetcodes/interview/easy_collection/234.go)|Easy| Linked List
918918
|237|[Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/linked_list/_237.java) & [Go](../master/codes/go/leetcodes/interview/easy_collection/237.go) |Easy| Linked List
919919
|242|[Valid Anagram](https://leetcode.com/problems/valid-anagram/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/strings/_242.java) & Python |Easy| Strings
920920
|268|[Missing Number](https://leetcode.com/problems/missing-number/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/others/_268.java) & [Go](../master/codes/go/leetcodes/interview/easy_collection/268.go) |Easy| Others
921921
|278|[First Bad Version](https://leetcode.com/problems/first-bad-version/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/binary_search/_278.java) & [Python](https://github.com/guobinhit/myleetcode/blob/master/codes/python/leetcodes/src/main/python/com/hit/basmath/learn/binary_search/_278.py)|Easy| Sorting and Searching
922922
|283|[Move Zeroes](https://leetcode.com/problems/move-zeroes/)|[Java](../master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_283.java) & [Go](../master/codes/go/leetcodes/interview/easy_collection/283.go)| Easy| Array
923-
|326|[Power of Three](https://leetcode.com/problems/power-of-three/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/math/_326.java) & Python |Easy| Math
923+
|326|[Power of Three](https://leetcode.com/problems/power-of-three/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/math/_326.java) & [Go](../master/codes/go/leetcodes/interview/easy_collection/326.go) |Easy| Math
924924
|344|[Reverse String](https://leetcode.com/problems/reverse-string/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_344.java) & [Go](../master/codes/go/leetcodes/interview/easy_collection/344.go) | Easy| Strings
925925
|350|[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/binary_search/_350.java) & Python |Easy| Array
926926
|384|[Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/design/_384.java) & Python |Medium| Design
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package easy_collection
2+
3+
/**
4+
* 101. Symmetric Tree
5+
* <p>
6+
* Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
7+
* <p>
8+
* For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
9+
* <p>
10+
* 1
11+
* / \
12+
* 2 2
13+
* / \ / \
14+
* 3 4 4 3
15+
* <p>
16+
* But the following [1,2,2,null,3,null,3] is not:
17+
* <p>
18+
* 1
19+
* / \
20+
* 2 2
21+
* \ \
22+
* 3 3
23+
* <p>
24+
* Note:
25+
* <p>
26+
* Bonus points if you could solve it both recursively and iteratively.
27+
*/
28+
29+
// TreeNode Definition for a binary tree node.
30+
type TreeNode struct {
31+
Val int
32+
Left *TreeNode
33+
Right *TreeNode
34+
}
35+
36+
func isSymmetric(root *TreeNode) bool {
37+
return root == nil || helper101(root.Left, root.Right)
38+
}
39+
40+
func helper101(left, right *TreeNode) bool {
41+
if left == nil || right == nil {
42+
return left == right
43+
}
44+
if left.Val != right.Val {
45+
return false
46+
}
47+
return helper101(left.Left, right.Right) && helper101(left.Right, right.Left)
48+
}

‎codes/go/leetcodes/interview/easy_collection/122.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ package easy_collection
3434
* Explanation: In this case, no transaction is done, i.e. max profit = 0.
3535
*/
3636

37-
func maxProfit2(prices []int) int {
37+
func maxProfit122(prices []int) int {
3838
target := 0
3939
for i, p := range prices {
4040
if i > 0 {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package easy_collection
2+
3+
/**
4+
* 189. Rotate Array
5+
* <p>
6+
* Given an array, rotate the array to the right by k steps, where k is non-negative.
7+
* <p>
8+
* Example 1:
9+
* <p>
10+
* Input: [1,2,3,4,5,6,7] and k = 3
11+
* <p>
12+
* Output: [5,6,7,1,2,3,4]
13+
* <p>
14+
* Explanation:
15+
* <p>
16+
* rotate 1 steps to the right: [7,1,2,3,4,5,6]
17+
* rotate 2 steps to the right: [6,7,1,2,3,4,5]
18+
* rotate 3 steps to the right: [5,6,7,1,2,3,4]
19+
* <p>
20+
* Example 2:
21+
* <p>
22+
* Input: [-1,-100,3,99] and k = 2
23+
* <p>
24+
* Output: [3,99,-1,-100]
25+
* <p>
26+
* Explanation:
27+
* <p>
28+
* rotate 1 steps to the right: [99,-1,-100,3]
29+
* rotate 2 steps to the right: [3,99,-1,-100]
30+
* <p>
31+
* Note:
32+
* <p>
33+
* Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
34+
* <p>
35+
* Could you do it in-place with O(1) extra space?
36+
*/
37+
38+
func rotate189(nums []int, k int) {
39+
numsLength := len(nums)
40+
k %= numsLength
41+
if k > 0 {
42+
reverse189(nums, 0, numsLength-1)
43+
reverse189(nums, 0, k-1)
44+
reverse189(nums, k, numsLength-1)
45+
}
46+
}
47+
48+
func reverse189(nums []int, left, right int) {
49+
for left < right {
50+
temp := nums[left]
51+
nums[left] = nums[right]
52+
nums[right] = temp
53+
left++
54+
right--
55+
}
56+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package easy_collection
2+
3+
/**
4+
* 19. Remove Nth Node From End of List
5+
* <p>
6+
* Given a linked list, remove the n-th node from the end of list and return its head.
7+
* <p>
8+
* Example:
9+
* <p>
10+
* Given linked list: 1->2->3->4->5, and n = 2.
11+
* <p>
12+
* After removing the second node from the end, the linked list becomes 1->2->3->5.
13+
* <p>
14+
* Note:
15+
* <p>
16+
* Given n will always be valid.
17+
* <p>
18+
* Follow up:
19+
* <p>
20+
* Could you do this in one pass?
21+
*/
22+
23+
/**
24+
* Definition for singly-linked list.
25+
* type ListNode struct {
26+
* Val int
27+
* Next *ListNode
28+
* }
29+
*/
30+
31+
func removeNthFromEnd(head *ListNode, n int) *ListNode {
32+
start := &ListNode{Val: 0}
33+
slow := start
34+
fast := start
35+
slow.Next = head
36+
for i := 0; i <= n; i++ {
37+
fast = fast.Next
38+
}
39+
for fast != nil {
40+
slow = slow.Next
41+
fast = fast.Next
42+
}
43+
slow.Next = slow.Next.Next
44+
return start.Next
45+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package easy_collection
2+
3+
/**
4+
* 204. Count Primes
5+
* <p>
6+
* Count the number of prime numbers less than a non-negative number, n.
7+
* <p>
8+
* Example:
9+
* <p>
10+
* Input: 10
11+
* Output: 4
12+
* Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
13+
*/
14+
15+
func countPrimes(n int) int {
16+
count := 0
17+
notPrime := make([]bool, n)
18+
for i := 2; i < n; i++ {
19+
if !notPrime[i] {
20+
count++
21+
for j := 2; j*i < n; j++ {
22+
notPrime[j*i] = true
23+
}
24+
}
25+
26+
}
27+
return count
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package easy_collection
2+
3+
/**
4+
* 206. Reverse Linked List
5+
* <p>
6+
* Reverse a singly linked list.
7+
* <p>
8+
* Example:
9+
* <p>
10+
* Input: 1->2->3->4->5->NULL
11+
* <p>
12+
* Output: 5->4->3->2->1->NULL
13+
* <p>
14+
* Follow up:
15+
* <p>
16+
* A linked list can be reversed either iteratively or recursively. Could you implement both?
17+
*/
18+
19+
/**
20+
* Definition for singly-linked list.
21+
* type ListNode struct {
22+
* Val int
23+
* Next *ListNode
24+
* }
25+
*/
26+
27+
func reverseList(head *ListNode) *ListNode {
28+
var newHead *ListNode
29+
for head != nil {
30+
nextNode := head.Next
31+
head.Next = newHead
32+
newHead = head
33+
head = nextNode
34+
}
35+
return newHead
36+
}

‎codes/go/leetcodes/interview/easy_collection/21.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ package easy_collection
1111
* Output: 1->1->2->3->4->4
1212
*/
1313

14+
// ListNode Definition for singly-linked list.
1415
type ListNode struct {
1516
Val int
1617
Next *ListNode
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package easy_collection
2+
3+
/**
4+
* 326. Power of Three
5+
* <p>
6+
* Given an integer, write a function to determine if it is a power of three.
7+
* <p>
8+
* Example 1:
9+
* <p>
10+
* Input: 27
11+
* Output: true
12+
* <p>
13+
* Example 2:
14+
* <p>
15+
* Input: 0
16+
* Output: false
17+
* <p>
18+
* Example 3:
19+
* <p>
20+
* Input: 9
21+
* Output: true
22+
* <p>
23+
* Example 4:
24+
* <p>
25+
* Input: 45
26+
* Output: false
27+
* <p>
28+
* Follow up:
29+
* <p>
30+
* Could you do it without using any loop / recursion?
31+
*/
32+
33+
func isPowerOfThree(n int) bool {
34+
if n < 1 {
35+
return false
36+
}
37+
for n%3 == 0 {
38+
n /= 3
39+
}
40+
return n == 1
41+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /