Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
lazywhite committed Jun 23, 2022
1 parent 20e370d commit ec33de1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 16 deletions.
43 changes: 43 additions & 0 deletions 005.买汽水.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
每瓶汽水单价2元
规则
4个瓶盖可以换1瓶汽水
2个空瓶可以换1瓶汽水
30元可以喝到多少瓶汽水?
*/

package main

import "fmt"

func main() {
fmt.Println(countBottle(30))
}

func countBottle(n int) int {
unitPrice := 2

lid := n / unitPrice
empty := n / unitPrice
total := n / unitPrice

newBottle := 0 // 兑换到的新瓶子

// 注意准入条件
for lid+newBottle >= 4 || empty+newBottle >= 2 {
// 1. drink
lid += newBottle
empty += newBottle

// 2. 兑换
newBottle = lid/4 + empty/2
total += newBottle

lid %= 4
empty %= 2
}

return total
}
41 changes: 25 additions & 16 deletions 151.颠倒字符串中的单词.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,33 +66,42 @@
*/
package main

import (
"strings"
"fmt"
)

func main() {
s := "the sky is blue"
reverseWords(s)
fmt.Println(reverseWords(s))
}

// @lc code=start
func reverseWords(s string) string {

result := []byte{}
result := new(strings.Builder)

for i := len(s) - 1; i >= 0; i-- {
ans := []byte{}
hasWord := false
for i >= 0 && s[i] == byte(' ') {
i--
}
for i >= 0 && s[i] != byte(' ') {
hasWord = true
ans = append([]byte{s[i]}, ans...)
i--
i := len(s) - 1

for i >= 0{
j := i
for j >= 0 && s[j] == byte(' ') {
j--
}
if hasWord {
ans = append(ans, byte(' '))
result = append(result, ans...)
end := j
for j >= 0 && s[j] != byte(' ') {
j--
}
start := j

result.Write([]byte(s[start+1:end+1]))
result.WriteByte(byte(' '))


i = j
}
return string(result[:len(result)-1])
rt := result.String()
return string(rt[:len(rt)-1])
}

// @lc code=end

0 comments on commit ec33de1

Please sign in to comment.