260. Single Number III

问题:

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?
分析:问题中只有两个数是出现一次的。

1. 第一个pass: 把数组里面所有的数都xor起来,会得到单独的两个数的xor结果,相同的元素xor结果是0,和0 xor的结果是这个数本身,因为单独的两个数彼此之间是不相同的,所以一定有一些数位上的数是不同的,xor的结果这位上就是1.

    xor = ~(xor - 1) 能够找到两个数里面最右的一个不同的数位

2. 第二个pass:对于数组里面的所有的数,和那个最右不同的数位xor,不同的那两个单独的数一定会被分到不同的组,然后对两个组内部再xor一次,就能得到结果:如果结果是0时,也就是说这一位上是设置了的那个,和res[0] xor,否则, 和res[1] xor。


代码:

class Solution {
public:
    vector<int> singleNumber(vector<int>& nums) {
        int r = 0, n = nums.size(), i = 0, last = 0;
        vector<int> ret(2, 0);
        
        for (i = 0; i < n; ++i) 
            r ^= nums[i];
        
        last = r & (~(r - 1));
        for (i = 0; i < n; ++i)
        {
            if ((last & nums[i]) != 0)
                ret[0] ^= nums[i];
            else
                ret[1] ^= nums[i];
        }
        
        return ret;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, here are some common OOP features and principles: Features: 1. Encapsulation: Encapsulation is the process of hiding the implementation details of an object from the outside world, and restricting access to the object's internal data and methods. 2. Abstraction: Abstraction is the process of creating a simplified version of something complex, in order to make it more manageable and easier to understand. 3. Inheritance: Inheritance is the mechanism by which one class inherits properties and methods from another class. 4. Polymorphism: Polymorphism is the ability of an object to take on many different forms, depending on the context in which it is used. Principles: 1. Single Responsibility Principle: The Single Responsibility Principle (SRP) states that a class should have only one reason to change. This means that a class should only be responsible for one thing, and should not be responsible for multiple unrelated tasks. 2. Open/Closed Principle: The Open/Closed Principle (OCP) states that a class should be open for extension but closed for modification. This means that you should be able to add new functionality to a class without modifying its existing code. 3. Liskov Substitution Principle: The Liskov Substitution Principle (LSP) states that a subclass should be able to be substituted for its parent class without affecting the correctness of the program. This means that a subclass should be able to use all the methods and properties of its parent class without any issues. 4. Interface Segregation Principle: The Interface Segregation Principle (ISP) states that a class should not be forced to implement interfaces it does not use. This means that you should only include the methods and properties that are necessary for a class to perform its specific tasks in its interface. 5. Dependency Inversion Principle: The Dependency Inversion Principle (DIP) states that high-level modules should not depend on low-level modules, but both should depend on abstractions. This means that you should use interfaces or abstract classes to decouple the high-level and low-level classes, making your code more flexible and easy to maintain.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值