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 dade468

Browse files
go: 121,136,141
1 parent bc2aabb commit dade468

File tree

5 files changed

+112
-4
lines changed

5 files changed

+112
-4
lines changed

‎README.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -901,11 +901,11 @@ You can also ask for problem solving ideas and discuss in GitHub issues directly
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
903903
|118|[Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_118.java) & [Go](../master/codes/go/leetcodes/interview/easy_collection/7.go)| Easy| Others
904-
|121|[Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/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/_121.java) & Python |Easy| Dynamic Programming
905-
|122|[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/interview/top_interview_questions/easy_collection/array/_122.java) & Python|Easy| Array
904+
|121|[Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/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/_121.java) & [Go](../master/codes/go/leetcodes/interview/easy_collection/121.go) |Easy| Dynamic Programming
905+
|122|[Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/array/_122.java) & Python|Easy| Array
906906
|125|[Valid Palindrome](https://leetcode.com/problems/valid-palindrome/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/_125.java) & Python |Easy| Strings
907-
|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) & Python |Easy| Array
908-
|141|[Linked List Cycle](https://leetcode.com/problems/number-of-islands/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/linked_list/_141.java) & Python|Easy| Linked List
907+
|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
908+
|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
910910
|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
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
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package easy_collection
2+
3+
import "math"
4+
5+
/**
6+
* 121. Best Time to Buy and Sell Stock
7+
* <p>
8+
* Say you have an array for which the ith element is the price of a given stock on day i.
9+
* <p>
10+
* If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
11+
* <p>
12+
* Note that you cannot sell a stock before you buy one.
13+
* <p>
14+
* Example 1:
15+
* <p>
16+
* Input: [7,1,5,3,6,4]
17+
* Output: 5
18+
* <p>
19+
* Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
20+
* Not 7-1 = 6, as selling price needs to be larger than buying price.
21+
* <p>
22+
* Example 2:
23+
* <p>
24+
* Input: [7,6,4,3,1]
25+
* Output: 0
26+
* <p>
27+
* Explanation: In this case, no transaction is done, i.e. max profit = 0.
28+
*/
29+
30+
func maxProfit(prices []int) int {
31+
minPrice := math.MaxInt32
32+
target := 0
33+
for _, p := range prices {
34+
if p < minPrice {
35+
minPrice = p
36+
} else if p-minPrice > target {
37+
target = p - minPrice
38+
}
39+
}
40+
return target
41+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package easy_collection
2+
3+
/**
4+
* 136. Single Number
5+
* <p>
6+
* Given a non-empty array of integers, every element appears twice except for one. Find that single one.
7+
* <p>
8+
* Note:
9+
* <p>
10+
* Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
11+
* <p>
12+
* Example 1:
13+
* <p>
14+
* Input: [2,2,1]
15+
* Output: 1
16+
* <p>
17+
* Example 2:
18+
* <p>
19+
* Input: [4,1,2,1,2]
20+
* Output: 4
21+
*/
22+
23+
func singleNumber(nums []int) int {
24+
if len(nums) == 0 {
25+
return 0
26+
}
27+
ans := nums[0]
28+
for i, n := range nums {
29+
if i != 0 {
30+
ans ^= n
31+
}
32+
}
33+
return ans
34+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package easy_collection
2+
3+
/**
4+
* 141. Linked List Cycle
5+
* <p>
6+
* Given a linked list, determine if it has a cycle in it.
7+
* <p>
8+
* Follow up:
9+
* <p>
10+
* Can you solve it without using extra space?
11+
*/
12+
13+
/**
14+
* Definition for singly-linked list.
15+
* type ListNode struct {
16+
* Val int
17+
* Next *ListNode
18+
* }
19+
*/
20+
21+
func hasCycle(head *ListNode) bool {
22+
fast := head
23+
slow := head
24+
for fast != nil && fast.Next != nil {
25+
fast = fast.Next.Next
26+
slow = slow.Next
27+
if fast == slow {
28+
return true
29+
}
30+
}
31+
return false
32+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package easy_collection
1010
* Input: 1->2->4, 1->3->4
1111
* Output: 1->1->2->3->4->4
1212
*/
13+
1314
type ListNode struct {
1415
Val int
1516
Next *ListNode

0 commit comments

Comments
(0)

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