leetcode-27-exercise_bit maniputation

461. Hamming Distance

解题思路:

把两个数的每一位和1比较,如果结果不同说明这两位不同。要比较32次。

int hammingDistance(int x, int y) {
       int result = 0;
       for (int i = 0; i < 32; i++) {
           if (((x>>i)&0x1) != ((y>>i)&0x1)) {
               result ++;
           }
       }
       return result;
    }

  



477. Total Hamming Distance

解题思路:

因为数据是从0到10^9的,所以可以转化为31位二进制数(10^9 = (10^3)^3 ~ (2^10)^3 = 2^30)。对于所有数的每一位,

计算该位置上1的个数和0的个数,那么,这一位的总差异数应该是二者之积。取每一位的话,可以用右移来取。

int totalHammingDistance(vector<int>& nums) {
        int result = 0;
        int ones = 0;
        for (int i = 0; i < 31; i++) {
            for (int j = 0; j < nums.size(); j++) {
                if ((nums[j] >> i) & 0x1)
                    ones ++;
            }
            result += ones * (nums.size() - ones);
            ones = 0;
        }
        return result;
    }

  



78. Subsets

Given a set of distinct integers, nums, return all possible subsets.

Note: The solution set must not contain duplicate subsets.

解题思路:

首先,子集的数量应该是2^n。注意创建这种结构的写法。

对于result来说,nums的每一个元素都可能存在也可能不存在。如果把j写成二进制,如果第i位为1,就可以把nums[i]

放入result[j]。

vector<vector<int>> subsets(vector<int>& nums) {
        int size = pow(2, nums.size());
        vector<vector<int> > result(size, vector<int>{});
        for (int i = 0; i < nums.size(); i++) {
            for (int j = 0; j < size; j++) {
                if (j >> i & 0x1)
                    result[j].push_back(nums[i]);
            }
        }
        return result;
    }


201. Bitwise AND of Numbers Range

解题思路:

要想确定整个范围内的数,转换为二进制时各个位置是否全为1,全部写出来是没有必要的。注意,此处只需要找出起始数共同的前缀就好了,

因为两个数可以修改后面的部分,必然会存在有0的位置,所以通过右移找出共同前缀,记录右移次数,再左移回来就好。

int rangeBitwiseAnd(int m, int n) {
        int i = 0;
        while (m != n) {
            m = m >> 1;
            n = n >> 1;
            i ++;
        }
        return (m << i);
    }

  



187. Repeated DNA Sequences

解题思路:

使用unordered_map。其中size_t是一个与机器相关的unsigned类型,其大小足以保证存储内存中对象的大小。

用hash存子串,节省查找时间。如果子串重复次数等于2,就保留。如果这个子串重复次数多于2了,那肯定保存过了,就不用管了。

vector<string> findRepeatedDnaSequences(string s) {
        vector<string> result;
        if (s.length() <= 10)
            return result;
        hash<string> h;
        unordered_map<size_t, int> m;
        for (int i = 0; i <= s.length() - 10; i++) {
            string sub = s.substr(i, 10);
            m[h(sub)] ++;
            if (m[h(sub)] == 2) {
                result.push_back(sub);
                continue;
            }
        }
        return result;
    }

  



leetcode-27-exercise_bit maniputation

时间: 2024-10-28 23:08:39

leetcode-27-exercise_bit maniputation的相关文章

图解双指针 | LeetCode 27. 移除元素

题目描述 原题链接:LeetCode 27. 移除元素 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 元素的顺序可以改变.你不需要考虑数组中超出新长度后面的元素. 示例 1: 给定 nums = [3,2,2,3], val = 3, 函数应该返回新的长度 2, 并且 nums 的前两个元素均为 2. 你不需要考虑数组中超出新长度后面的元素

LeetCode(27)题解:Remove Element

https://leetcode.com/problems/remove-element/ Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. 思路: 因为可以改变元

LeetCode 27.Remove Element 数组元素删除

27. Remove Element Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be changed.

leetcode 27

27. Remove Element Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be changed.

leetcode 27 Remove Element

Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. 题解:水题.遇到val就和最后一个不是val的数交换位置就好了. class Solution { public:

leetCode 27.Remove Element (删除元素) 解题思路和方法

Remove Element Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. 思路:此题和26题一脉相承,算法上不难,具体如代码所示: public class

Leetcode 27 Remove Element STL

和remove zero类似的方法完成该题 1 class Solution { 2 public: 3 int removeElement(vector<int>& nums, int val) { 4 vector<int>::size_type j = 0; 5 for(vector<int>::size_type i = 0; i < nums.size(); ++i){ 6 if(nums[i] != val) nums[j++] = nums[

LeetCode 27 Remove Element(移除元素)

翻译 给定一个数组和一个值,删除该值的所有实例,并返回新的长度. 元素的顺序可以被改变,也不关心最终的数组长度. 原文 Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new lengt

[LeetCode] 27. Remove Element ☆

Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be changed. It doesn't matter

LeetCode 27 Remove Element (C,C++,Java,Python)

Problem: Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. Solution: 和26题一样,就是判断条件不一样而已. 题目大意: 给一个数组,要求返回删除