Leecode 0461.hamming-distance
461. 汉明距离两个整数之间的 汉明距离 指的是这两个数字对应二进制位不同的位置的数目。 给你两个整数 x 和 y,计算并返回它们之间的汉明距离。 示例 1: 1234567输入:x = 1, y = 4输出:2解释:1 (0 0 0 1)4 (0 1 0 0) ↑ ↑上面的箭头指出了对应二进制位不同的位置。 示例 2: 12输入:x = 3, y = 1输出:1 题目大意 汉明距离是指两个整数的二进制表示中,对应位置二进制位不同的数目。给定两个整数 x 和 y,计算并返回它们之间的汉明距离。 例如: 输入 x=1, y=4,二进制分别为 0001 和 0100,对应位不同的位置有 2 处,输出 2; 输入 x=3, y=1,二进制分别为 0011 和 0001,对应位不同的位置有 1 处,输出 1。 解题思路核心思路是利用异或运算 + 统计 1 的个数,步骤如下: 异或运算(XOR):异或的特性是 “相同为 0,不同为 1”。对 x 和 y 做异或操作,得到的结果 xor_result 中,每一位的 1 都对应 x 和 y...
Leecode 0003. Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating Characters题目Given a string, find the length of the longest substring without repeating characters. Example 1: 123Input: "abcabcbb"Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: 123Input: "bbbbb"Output: 1Explanation: The answer is "b", with the length of 1. Example 3: 1234Input: "pwwkew"Output: 3Explanation: The answer is "wke", with the length of 3. ...
Leecode 0001.two sum
1. Two Sum题目Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: 1234Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,return [0, 1]. 题目大意在数组中找到两个数,它们的和等于给定的目标值 target,并返回这两个数在数组中的下标。要求每个元素只能使用一次,且保证有且仅有一个解。 解题思路方法一:双层循环(暴力枚举)思路 通过双重循环遍历数组中的每一对元素,检查它们的和是否等于目标值 target。外层循环控制第一个元素的索引...