Single Number和Single Number II

1 Single Number

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?

解析:

a ^ a = 0,a ^ 0 = a

所以对所有的数取异或,其最终结果即为所求。

1     public int singleNumber(int[] A) {
2         int single = 0;
3         for (int i : A) {
4             single ^= i;
5         }
6         return single;
7     }

2 Single Number II

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) a @ a @ a = 0,a @ 0 = a

只要能找到满足上述等式的操作函数@,即可解决。

(2) a -> one,a @ a -> two,a @ a @ a -> one @ two = 0

即操作一次a,结果记录到one;操作两次,结果记录到two;操作三次时,结果记录到了one和two,则将该结果置为0.

 1     public int singleNumber_3(int[] A) {
 2         int one = 0;
 3         int two = 0;
 4         for (int carry : A) {
 5             while (carry != 0) {
 6                 two ^= one & carry;
 7                 one ^= carry;
 8                 carry = one & two;
 9             }
10         }
11         return one;
12     }

时间: 2024-10-07 16:40:01

Single Number和Single Number II的相关文章

136. Single Number && 137. Single Number II && 260. Single Number III

136. Single Number 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? Subscribe to see which co

leetcode 375. Guess Number Higher or Lower II

传送门 375. Guess Number Higher or Lower II QuestionEditorial Solution My Submissions Total Accepted: 1546 Total Submissions: 5408 Difficulty: Medium We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess w

375. Guess Number Higher or Lower II (Python)

375. Guess Number Higher or Lower II Description We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I'll tell you whether the number I picked is hig

LC 375. Guess Number Higher or Lower II

We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I'll tell you whether the number I picked is higher or lower. However, when you guess a particula

Find n’th number in a number system with only 3 and 4

这是在看geeksforgeeks时看到的一道题,挺不错的,题目是 Given a number system with only 3 and 4. Find the nth number in the number system. First few numbers in the number system are: 3, 4, 33, 34, 43, 44, 333, 334, 343, 344, 433, 434, 443, 444, 3333, 3334, 3343, 3344, 343

LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III

136. Single Number Given an array of integers, every element appears twice except for one. Find that single one. (Easy) Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 分析: 第一问属于技巧题,做过就会,

leetcode 136. Single Number && 137. Single Number II

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? 异或运算应用: 异或运算满足交换律和结合律, 通过交换元素(如插入排序等)可以使得相同元

LeetCode 136. Single Number & 268. Missing Number

136. Single Number 考察的是异或运算.相同的数异或结果为0,一个数与0异或还是原来的数,以及异或符合交换律.因此,把所有的数都异或起来,结果就是落单的那个数. class Solution { public: int singleNumber(vector<int>& nums) { int res=0; for (int num:nums){ res ^= num; } return res; } }; 268. Missing Number 可以用数学方法直接做,

263. Ugly Number &amp;&amp; 264. Ugly Number II &amp;&amp; 313. Super Ugly Number

263. Ugly Number Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.