Skip to content

Commit 719348d

Browse files
committed
commit 122/136
1 parent 077fa23 commit 719348d

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

122/122-maxProfit.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/
2+
package main
3+
4+
import "fmt"
5+
6+
func maxProfit(prices []int) int {
7+
profit := 0
8+
min, length := prices[0], len(prices)
9+
for i := 1; i < length; i++ {
10+
if prices[i] < prices[i-1] {
11+
profit += prices[i-1] - min
12+
min = prices[i]
13+
}
14+
}
15+
if min < prices[length-1] {
16+
profit += prices[length-1] - min
17+
}
18+
return profit
19+
}
20+
21+
func main() {
22+
n := [][]int{{7, 1, 5, 3, 6, 4}, {1, 2, 3, 4, 5}, {7, 6, 4, 3, 1}}
23+
for i := 0; i < len(n); i++ {
24+
fmt.Println("ret:", maxProfit(n[i]))
25+
}
26+
}

136/136-singleNumber.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// https://leetcode-cn.com/problems/single-number/
2+
package main
3+
4+
import "fmt"
5+
6+
func singleNumber(nums []int) int {
7+
ret := nums[0]
8+
for i := 1; i < len(nums); i++ {
9+
ret = ret ^ nums[i]
10+
}
11+
return ret
12+
}
13+
14+
func main() {
15+
n := [][]int{{2, 2, 1}, {4, 1, 2, 1, 2}}
16+
for i := 0; i < len(n); i++ {
17+
fmt.Println("ret:", singleNumber(n[i]))
18+
}
19+
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ LeetCode
3737
|118|[杨辉三角](https://leetcode-cn.com/problems/pascals-triangle/)|[Go](./118/118-generate.go)|简单||
3838
|119|[杨辉三角 II](https://leetcode-cn.com/problems/pascals-triangle-ii/)|[Go](./119/119-getRow.go)|简单||
3939
|121|[买卖股票的最佳时机](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/)|[Go](./121/121-maxProfit.go)|简单||
40+
|122|[买卖股票的最佳时机 II](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/)|[Go](./122/122-maxProfit.go)|简单|贪心|
41+
|136|[只出现一次的数字](https://leetcode-cn.com/problems/single-number/)|[Go](./136/136-singleNumber.go)|简单|位运算|
4042
|125|[验证回文串](https://leetcode-cn.com/problems/valid-palindrome/)|[Go](./125-isPalindrome/125-isPalindrome.go)|简单||
4143
|131|[分割回文串](https://leetcode-cn.com/problems/palindrome-partitioning/) | [Go](./131-partition/131-partition.go)|中等|递归, 动态规划|
4244
|141|[环形链表](https://leetcode-cn.com/problems/linked-list-cycle/)|[Go](./141-hasCycle/141-hasCycle.go)|简单|双指针|

0 commit comments

Comments
 (0)