diff --git a/website/content/ChapterFour/1000~1099/1009.Complement-of-Base-10-Integer.md b/website/content/ChapterFour/1000~1099/1009.Complement-of-Base-10-Integer.md new file mode 100644 index 00000000..b448d6c2 --- /dev/null +++ b/website/content/ChapterFour/1000~1099/1009.Complement-of-Base-10-Integer.md @@ -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 +} +``` + + +---------------------------------------------- +
diff --git a/website/content/ChapterFour/1000~1099/1010.Pairs-of-Songs-With-Total-Durations-Divisible-by-60.md b/website/content/ChapterFour/1000~1099/1010.Pairs-of-Songs-With-Total-Durations-Divisible-by-60.md new file mode 100644 index 00000000..b8edb0eb --- /dev/null +++ b/website/content/ChapterFour/1000~1099/1010.Pairs-of-Songs-With-Total-Durations-Divisible-by-60.md @@ -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 +} +``` + + +---------------------------------------------- + diff --git a/website/content/ChapterFour/1000~1099/1011.Capacity-To-Ship-Packages-Within-D-Days.md b/website/content/ChapterFour/1000~1099/1011.Capacity-To-Ship-Packages-Within-D-Days.md index 8cd28a6c..c812630a 100755 --- a/website/content/ChapterFour/1000~1099/1011.Capacity-To-Ship-Packages-Within-D-Days.md +++ b/website/content/ChapterFour/1000~1099/1011.Capacity-To-Ship-Packages-Within-D-Days.md @@ -117,6 +117,6 @@ func calSum(mid, m int, nums []int) bool { ---------------------------------------------- diff --git a/website/content/ChapterFour/1200~1299/1295.Find-Numbers-with-Even-Number-of-Digits.md b/website/content/ChapterFour/1200~1299/1295.Find-Numbers-with-Even-Number-of-Digits.md index 17d3b6ba..f9e0d8f3 100644 --- a/website/content/ChapterFour/1200~1299/1295.Find-Numbers-with-Even-Number-of-Digits.md +++ b/website/content/ChapterFour/1200~1299/1295.Find-Numbers-with-Even-Number-of-Digits.md @@ -66,5 +66,5 @@ func findNumbers(nums []int) int { ----------------------------------------------