摘要: 338. 比特位计数 - 力扣(LeetCode) (leetcode-cn.com) 思路 1 逃课法: 1. 使用语言的内置方法 bits.OnesCount func countBits(n int) (result []int) { for i := 0; i <= n; i++ { res 阅读全文
posted @ 2022-04-26 17:59 SoutherLea 阅读(14) 评论(0) 推荐(0) 编辑
摘要: 448. 找到所有数组中消失的数字 - 力扣(LeetCode) (leetcode-cn.com) 思路: 1. 由题意可知数组长度n,元素取值范围1~n. 2. 我们可以对数组进行遍历,根据数组元素的值,将它和index=值-1的位置的元素交换数值。如果该位置的元素和当前元素值相等, 则不进行交 阅读全文
posted @ 2022-04-26 17:37 SoutherLea 阅读(15) 评论(0) 推荐(0) 编辑
摘要: 136. 只出现一次的数字 - 力扣(LeetCode) (leetcode-cn.com) 思路 使用位运算^: 异或运算性质如下 1^1=0 0^0=0 1^0=1 0^1=1 所以我们只需要把数组中的元素遍历异或一遍,剩下的就是那个单独的数字 func singleNumber(nums [] 阅读全文
posted @ 2022-04-26 10:27 SoutherLea 阅读(15) 评论(0) 推荐(0) 编辑
摘要: 169. 多数元素 - 力扣(LeetCode) (leetcode-cn.com) 思路 1: 1. 先对所有元素进行排序 2. 返回位于数组最中间的元素 func majorityElement(nums []int) int { lenth:=len(nums) sort.Ints(nums) 阅读全文
posted @ 2022-04-26 02:34 SoutherLea 阅读(12) 评论(0) 推荐(0) 编辑
摘要: 461. 汉明距离 - 力扣(LeetCode) (leetcode-cn.com) 思路 1 使用内置方法: func hammingDistance(x int, y int) int { return bits.OnesCount(uint(x^y)) } 思路 2 自己实现功能 1. x^y 阅读全文
posted @ 2022-04-26 02:24 SoutherLea 阅读(11) 评论(0) 推荐(0) 编辑