Add solution 1009/1010

This commit is contained in:
halfrost
2022-01-27 22:22:22 +08:00
parent f41d1df6d7
commit 65553627c8
4 changed files with 146 additions and 2 deletions

View File

@ -0,0 +1,74 @@
# [1009. Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer/)
## 题目
The **complement** of an integer is the integer you get when you flip all the `0`'s to `1`'s and all the `1`'s to `0`'s in its binary representation.
- For example, The integer `5` is `"101"` in binary and its **complement** is `"010"` which is the integer `2`.
Given an integer `n`, return *its complement*.
**Example 1:**
```
Input: n = 5
Output: 2
Explanation: 5 is "101" in binary, with complement "010" in binary, which is 2 in base-10.
```
**Example 2:**
```
Input: n = 7
Output: 0
Explanation: 7 is "111" in binary, with complement "000" in binary, which is 0 in base-10.
```
**Example 3:**
```
Input: n = 10
Output: 5
Explanation: 10 is "1010" in binary, with complement "0101" in binary, which is 5 in base-10.
```
**Constraints:**
- `0 <= n < 109`
## 题目大意
每个非负整数 N 都有其二进制表示。例如 5 可以被表示为二进制 "101"11 可以用二进制 "1011" 表示依此类推。注意 N = 0 任何二进制表示中都不含前导零。
二进制的反码表示是将每个 1 改为 0 且每个 0 变为 1。例如二进制数 "101" 的二进制反码为 "010"。
给你一个十进制数 N请你返回其二进制表示的反码所对应的十进制整数。
## 解题思路
- 简单题。求一个十进制数的反码,只需要让该数和全 1 的数进行异或计算即可。所以本题重点在如何构造 mask 上。
## 代码
```go
package leetcode
func bitwiseComplement(n int) int {
mask := 1
for mask < n {
mask = (mask << 1) + 1
}
return mask ^ n
}
```
----------------------------------------------
<div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1006.Clumsy-Factorial/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1010.Pairs-of-Songs-With-Total-Durations-Divisible-by-60/">下一页➡️</a></p>
</div>

View File

@ -0,0 +1,70 @@
# [1010. Pairs of Songs With Total Durations Divisible by 60](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/)
## 题目
You are given a list of songs where the ith song has a duration of `time[i]` seconds.
Return *the number of pairs of songs for which their total duration in seconds is divisible by* `60`. Formally, we want the number of indices `i`, `j` such that `i < j` with `(time[i] + time[j]) % 60 == 0`.
**Example 1:**
```
Input: time = [30,20,150,100,40]
Output: 3
Explanation: Three pairs have a total duration divisible by 60:
(time[0] = 30, time[2] = 150): total duration 180
(time[1] = 20, time[3] = 100): total duration 120
(time[1] = 20, time[4] = 40): total duration 60
```
**Example 2:**
```
Input: time = [60,60,60]
Output: 3
Explanation: All three pairs have a total duration of 120, which is divisible by 60.
```
**Constraints:**
- `1 <= time.length <= 6 * 104`
- `1 <= time[i] <= 500`
## 题目大意
在歌曲列表中,第 i 首歌曲的持续时间为 time[i] 秒。
返回其总持续时间(以秒为单位)可被 60 整除的歌曲对的数量。形式上,我们希望下标数字 i 和 j 满足  i < j 且有 (time[i] + time[j]) % 60 == 0
## 解题思路
- 简单题先将数组每个元素对 60 取余将它们都转换到 [0,59] 之间然后在数组中找两两元素之和等于 60 的数对可以在 0-30 之内对半查找符合条件的数对 0 30 单独计算因为多个 0 相加余数还为 0 2 30 相加之和为 60
## 代码
```go
func numPairsDivisibleBy60(time []int) int {
counts := make([]int, 60)
for _, v := range time {
v %= 60
counts[v]++
}
res := 0
for i := 1; i < len(counts)/2; i++ {
res += counts[i] * counts[60-i]
}
res += (counts[0] * (counts[0] - 1)) / 2
res += (counts[30] * (counts[30] - 1)) / 2
return res
}
```
----------------------------------------------
<div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1009.Complement-of-Base-10-Integer/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1011.Capacity-To-Ship-Packages-Within-D-Days/">下一页➡️</a></p>
</div>

View File

@ -117,6 +117,6 @@ func calSum(mid, m int, nums []int) bool {
----------------------------------------------
<div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1006.Clumsy-Factorial/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1010.Pairs-of-Songs-With-Total-Durations-Divisible-by-60/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1017.Convert-to-Base-2/">下一页➡️</a></p>
</div>

View File

@ -66,5 +66,5 @@ func findNumbers(nums []int) int {
----------------------------------------------
<div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1200~1299/1290.Convert-Binary-Number-in-a-Linked-List-to-Integer/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1200~1299/1299.Replace-Elements-with-Greatest-Element-on-Right-Side/">下一页➡️</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1200~1299/1296.Divide-Array-in-Sets-of-K-Consecutive-Numbers/">下一页➡️</a></p>
</div>