67. 二进制求和 Add Binary
给你两个二进制字符串,返回它们的和(用二进制表示)。
输入为 非空 字符串且只包含数字 1
和 0
。
示例 1:
输入: a = "11", b = "1"
输出: "100"
示例 2:
输入: a = "1010", b = "1011"
输出: "10101"
提示:
- 每个字符串仅由字符
'0'
或'1'
组成。 1 <= a.length, b.length <= 10^4
- 字符串如果不是
"0"
,就都不含前导零。
代码1:
package main import ( "fmt" "strings" ) func addBinary(a string, b string) string { n, m := len(a), len(b) if n < m { n, m = m, n a, b = b, a } b = strings.Repeat("0", n-m) + b res := make([]byte, n+1) carry := byte(0) for i := n - 1; i >= 0; i-- { sum := carry + a[i] - '0' + b[i] - '0' res[i+1] = sum%2 + '0' carry = sum / 2 } if carry > 0 { res[0] = '1' return string(res) } return string(res[1:]) } func main() { fmt.Println(addBinary("11", "1")) fmt.Println(addBinary("1010", "1011")) }
代码2:
package main import ( "fmt" "strconv" ) func addBinary(a string, b string) string { n, m := len(a)-1, len(b)-1 carry, res := 0, "" for n >= 0 || m >= 0 || carry > 0 { if n >= 0 { carry += int(a[n] - '0') n-- } if m >= 0 { carry += int(b[m] - '0') m-- } res = strconv.Itoa(carry%2) + res carry /= 2 } return res } func main() { fmt.Println(addBinary("11", "1")) fmt.Println(addBinary("1010", "1011")) }
输出:
100
10101
68. 文本左右对齐 Text Justification
给定一个单词数组 words
和一个长度 maxWidth
,重新排版单词,使其成为每行恰好有 maxWidth
个字符,且左右两端对齐的文本。
你应该使用 “贪心算法” 来放置给定的单词;也就是说,尽可能多地往每行中放置单词。必要时可用空格 ' '
填充,使得每行恰好有 maxWidth 个字符。
要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配,则左侧放置的空格数要多于右侧的空格数。
文本的最后一行应为左对齐,且单词之间不插入额外的空格。
注意:
- 单词是指由非空格字符组成的字符序列。
- 每个单词的长度大于 0,小于等于 maxWidth。
- 输入单词数组
words
至少包含一个单词。
示例 1:
输入: words = ["This", "is", "an", "example", "of", "text", "justification."], maxWidth = 16
输出:
[ "This is an", "example of text", "justification. " ]
示例 2:
输入:words = ["What","must","be","acknowledgment","shall","be"], maxWidth = 16
输出:
[ "What must be", "acknowledgment ", "shall be " ]
解释: 注意最后一行的格式应为 "shall be " 而不是 "shall be",
因为最后一行应为左对齐,而不是左右两端对齐。
第二行同样为左对齐,这是因为这行只包含一个单词。
示例 3:
输入:words =
["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"],maxWidth = 20
输出:
[ "Science is what we", "understand well", "enough to explain to", "a computer. Art is", "everything else we", "do " ]
提示:
1 <= words.length <= 300
1 <= words[i].length <= 20
words[i]
由小写英文字母和符号组成1 <= maxWidth <= 100
words[i].length <= maxWidth
代码1:
package main import ( "fmt" "strings" ) func fullJustify(words []string, maxWidth int) []string { ans := []string{} right, n := 0, len(words) for { left := right // 当前行的第一个单词在 words 的位置 sumLen := 0 // 统计这一行单词长度之和 // 循环确定当前行可以放多少单词,注意单词之间应至少有一个空格 for right < n && sumLen+len(words[right])+right-left <= maxWidth { sumLen += len(words[right]) right++ } // 最后一行特殊处理:单词左对齐,右侧补齐空格 if right == n { s := strings.Join(words[left:], " ") ans = append(ans, s+space(maxWidth-len(s))) return ans } // 当前行是最后一行以外的其他行 numWords := right - left numSpaces := maxWidth - sumLen // 如果只有一个单词,特殊处理:左侧补齐空格 if numWords == 1 { ans = append(ans, words[left]+space(numSpaces)) continue } // 普通情况:单词之间应均匀分配额外的空格 avgSpaces := numSpaces / (numWords - 1) extraSpaces := numSpaces % (numWords - 1) s1 := strings.Join(words[left:left+extraSpaces+1], space(avgSpaces+1)) s2 := strings.Join(words[left+extraSpaces+1:right], space(avgSpaces)) ans = append(ans, s1+space(avgSpaces)+s2) } } // 返回长度为 n 的由空格组成的字符串 func space(n int) string { return strings.Repeat(" ", n) } func main() { words := []string{"This", "is", "an", "example", "of", "text", "justification."} maxWidth := 16 for _, line := range fullJustify(words, maxWidth) { fmt.Println(line) } fmt.Println() words = []string{"What", "must", "be", "acknowledgment", "shall", "be"} for _, line := range fullJustify(words, maxWidth) { fmt.Println(line) } fmt.Println() words = []string{"Science", "is", "what", "we", "understand", "well", "enough", "to", "explain", "to", "a", "computer.", "Art", "is", "everything", "else", "we", "do"} maxWidth = 20 for _, line := range fullJustify(words, maxWidth) { fmt.Println(line) } }
代码2:
package main import ( "fmt" "strings" ) func fullJustify(words []string, maxWidth int) []string { ans := []string{} n := len(words) i := 0 for i < n { left := i // 当前行的第一个单词在 words 中的位置 sumLen := 0 // 统计这一行单词长度之和 // 循环确定当前行可以放多少单词,注意单词之间应至少有一个空格 for i < n && sumLen+len(words[i])+i-left <= maxWidth { sumLen += len(words[i]) i++ } // 当前行是最后一行的特殊处理 if i == n { s := strings.Join(words[left:], " ") ans = append(ans, s+space(maxWidth-len(s))) break } // 当前行不是最后一行的特殊处理 numWords := i - left numSpaces := maxWidth - sumLen // 如果只有一个单词,特殊处理:左侧补齐空格 if numWords == 1 { ans = append(ans, words[left]+space(numSpaces)) continue } // 普通情况:单词之间应均匀分配额外的空格 avgSpaces := numSpaces / (numWords - 1) extraSpaces := numSpaces % (numWords - 1) s1 := strings.Join(words[left:left+extraSpaces+1], space(avgSpaces+1)) s2 := strings.Join(words[left+extraSpaces+1:i], space(avgSpaces)) ans = append(ans, s1+space(avgSpaces)+s2) } return ans } // 返回长度为 n 的由空格组成的字符串 func space(n int) string { return strings.Repeat(" ", n) } func main() { words := []string{"This", "is", "an", "example", "of", "text", "justification."} maxWidth := 16 for _, line := range fullJustify(words, maxWidth) { fmt.Println(line) } fmt.Println() words = []string{"What", "must", "be", "acknowledgment", "shall", "be"} for _, line := range fullJustify(words, maxWidth) { fmt.Println(line) } fmt.Println() words = []string{"Science", "is", "what", "we", "understand", "well", "enough", "to", "explain", "to", "a", "computer.", "Art", "is", "everything", "else", "we", "do"} maxWidth = 20 for _, line := range fullJustify(words, maxWidth) { fmt.Println(line) } }
输出:
This is an example of text justification. What must be acknowledgment shall be Science is what we understand well enough to explain to a computer. Art is everything else we do
69. x 的平方根 Sqrt x
给你一个非负整数 x
,计算并返回 x
的 算术平方根 。
由于返回类型是整数,结果只保留 整数部分 ,小数部分将被 舍去 。
注意:不允许使用任何内置指数函数和算符,例如 pow(x, 0.5)
或者 x ** 0.5
。
示例 1:
输入:x = 4
输出:2
示例 2:
输入:x = 8
输出:2
解释:8 的算术平方根是 2.82842..., 由于返回类型是整数,小数部分将被舍去。
提示:
0 <= x <= 2^31 - 1
代码1:暴力枚举
package main import ( "fmt" ) func mySqrt(x int) int { i := 0 for i*i <= x { i++ } return i - 1 } func main() { fmt.Println(mySqrt(4)) fmt.Println(mySqrt(8)) fmt.Println(mySqrt(122)) }
代码2:牛顿迭代法
package main import ( "fmt" "math" ) func mySqrt(x int) int { if x == 0 { return 0 } x0 := float64(x) eps := 1e-6 for { x1 := 0.5 * (x0 + float64(x)/x0) if math.Abs(x1-x0) < eps { break } x0 = x1 } return int(x0) } func main() { fmt.Println(mySqrt(4)) fmt.Println(mySqrt(8)) fmt.Println(mySqrt(122)) }
代码3: 二分查找
package main import ( "fmt" ) func mySqrt(x int) int { left, right := 0, x res := -1 for left <= right { mid := left + (right-left)/2 guess := mid * mid if guess <= x { res = mid left = mid + 1 } else { right = mid - 1 } } return res } func main() { fmt.Println(mySqrt(4)) fmt.Println(mySqrt(8)) fmt.Println(mySqrt(122)) }
输出:
2
2
11
🌟 每日一练刷题专栏 🌟
✨持续,努力奋斗做强刷题搬运工!
👍 点赞,你的认可是我坚持的动力!
🌟 收藏,你的青睐是我努力的方向!
✎ 评论,你的意见是我进步的财富!
☸ 主页:https://hannyang.blog.csdn.net/