260. Single Number III

解决单例数问题的算法:给定一个整数数组,找出仅出现一次的两个数,要求时间复杂度为O(n),空间复杂度为O(1)。核心思路是异或操作找到唯一值,再按位分组。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

260. Single Number III

Medium

3945186Add to ListShare

Given an integer array 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. You can return the answer in any order.

You must write an algorithm that runs in linear runtime complexity and uses only constant extra space.

Example 1:

Input: nums = [1,2,1,3,2,5]
Output: [3,5]
Explanation:  [5, 3] is also a valid answer.

Example 2:

Input: nums = [-1,0]
Output: [-1,0]

Example 3:

Input: nums = [0,1]
Output: [1,0]

Constraints:

  • 2 <= nums.length <= 3 * 104
  • -231 <= nums[i] <= 231 - 1
  • Each integer in nums will appear twice, only two integers will appear once.

class Solution:
    def singleNumber(self, nums: List[int]) -> List[int]:
        """
        sorted(Solution().singleNumber([1, 2, 1, 3, 2, 5])) == [3, 5]
        
        参考别人的解题思路:假设a,b为单独的2个数,对nums所有数进行异或,可以得到 d=a^b 的值

        方法1:找到d中第一个二进制位为1的位数k,再次对nums数组的数在位数k为1或者为0进行分组,a,b肯定落在单独的一边
        int k = 0;
        while(!(sum >> k & 1)) k++;
        if(x >> k & 1)

        采用方法2:d &(-d), 可以找到最低位为1的数,接着同方法1分组
        时间复杂度:O(n) 空间复杂度:O(1)

        如 a=3,b=5 d=a^b=6  d &(-d)= 2 可以找到最低位为1的数
        正数转负数 总结过程:          6 = 0 110 (原码)
        a.最高位改成1                1 110 负数二进制表示
        b.除了最高位,其他位取反       1 001
        c.结果+1                   1 010
        d.得到的结果就是对应的负值     -6 = 1 010  负数在机器中以补码形式保存
        负数转正数反之
        """

        d = 0
        for i in nums:
            d ^= i
        # 找到最低位为1的数
        d &= -d
        result = [0, 0]
        for i in nums:
            if i & d == 0:
                result[0] ^= i
            else:
                result[1] ^= i
        return result

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、付费专栏及课程。

余额充值