zoukankan      html  css  js  c++  java
  • Single Number,Single Number II

    Single Number

    Total Accepted: 103745 Total Submissions: 218647 Difficulty: Medium

    Given an array of integers, every element appears twice except for one. Find that single one.

    Note:
    Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

     
    class Solution {
    public:
        int singleNumber(vector<int>& nums) {
            int res = 0;
            int nums_size = nums.size();
            for(int i=0;i<nums_size;i++){
                res ^= nums[i];
            }
            return res;
        }
    };
     

    Single Number II

    Total Accepted: 69333 Total Submissions: 191725 Difficulty: Medium

    Given an array of integers, every element appears three times except for one. Find that single one.

    Note:
    Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

    统计各个二进制位中1的个数,该方法适用于这种类型的题目:数组中的所有数都出现了K次,只有一个数只出现了一次

    const int BITS = sizeof(int) * 8;
    
    class Solution {
    public:
        int singleNumber(vector<int>& nums) {
            int times[BITS]={0};
            cout<<BITS<<endl;
            int nums_size = nums.size();
            for(int i=0;i<nums_size;i++){
                int x = nums[i];
                for(int j=0;j<BITS;j++){
                    if((x>>j) & 1){
                        times[j]++;
                    }
                }
            }
            int res = 0;
            for(int i=0;i<BITS;i++){
                if(times[i]%3){
                    res += 1<<i;
                }
            }
            return res;
        }
    };

    Single Number III

    Total Accepted: 18186 Total Submissions: 44516 Difficulty: Medium

    Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.

    For example:

    Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].

    Note:

    1. The order of the result is not important. So in the above example, [5, 3] is also correct.
    2. Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity

    所有的结果异或之后剩下一个非零值,这个值就是只出现一次的两个数的异或结果,找到这个数中第一个bit=1的数位,用1左移这么多个数位做为数组的分割器。

    const int BITS = sizeof(int)*8;
    class Solution {
    public:
        vector<int> singleNumber(vector<int>& nums) {
            int nums_size = nums.size();
            int n = 0;
            for(int i=0;i<nums_size;i++){
                n ^= nums[i];
            }
            int seprator = 0;
            for(int i=0;i<BITS;i++){
                if((n>>i) & 1){
                    seprator = 1<<i;
                    break;
                }
            }
            vector<int> res;
            int res1=0,res2=0;
            for(int i=0;i<nums_size;i++){
                if( (seprator & nums[i])){
                    res1 ^= nums[i];
                }else{
                    res2 ^= nums[i];
                }
            }
            res.push_back(res1);
            res.push_back(res2);
            return res;
        }
    };
  • 相关阅读:
    Git 版本更新(Windows下)
    Qt Quick 构建 Android app
    Android Studio更新到3.6.0 Gradle报错No cached version of com.android.tools.build:aapt2:3.2.0-alpha16-4748712 available for of...
    在自定义类中使用setMouseCallBack
    关于Qt 状态机
    C++ 新特性 移动构造函数和移动赋值
    关于Qt MetaObject connectSlotsByName
    Prototype 设计模式在框架中的应用
    关于设计模式
    如何提高Qt工程的编译速度
  • 原文地址:https://www.cnblogs.com/zengzy/p/5052161.html
Copyright © 2011-2022 走看看