Single Number && Single NumberII

这两个题,都是用bit operation解决的,但是第二个稍微tricky一点。

先说第一个,我们利用XOR, 如果一个数字出现两次,那么在每一位上,两两抵消。唯独那个只出现一次的,没有另一半和它抵消了。。所以就剩下了。

 1     public int singleNumber(int[] A) {
 2         if (A == null) {
 3             return 0;
 4         }
 5         int ret = 0;
 6         for (int i = 0; i < A.length; i++) {
 7             ret = ret ^ A[i];
 8         }
 9         return ret;
10     }

Single NumberII

这时再使用一的方法,就不好用了。怎么才能过滤掉出现三次的情况呢? 我们对这些数字的每一位出现的次数计数。如果出现三次,则归零。实现起来就是,该位一的个数就是:count % 3.

 1 public int singleNumber(int[] A) {
 2         if (A == null || A.length == 0) {
 3             return 0;
 4         }
 5         int res = 0;
 6         for (int i = 0; i < 32; i++) {
 7             int sum = 0;
 8             for (int j = 0; j < A.length; j++) {
 9                 if (((A[j] >>> i) & 1) == 1) {
10                     sum++;
11                     sum = sum % 3;
12                 }
13             }
14             res |= sum << i;
15         }
16         return res;
17     }
时间: 2024-08-09 14:47:51

Single Number && Single NumberII的相关文章

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 imp

[LeetCode#136, 137]Single Number, Single Number 2

The question: 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? My analysis: Thi

LeetCode 136、137:Single Number I &amp; II

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? Single Number II Given an arr

Configuring Single Number Reach on Cisco CUCM 7.1.3

Cisco CUCM allows you to configure single number reach (also known as mobility) so that a call destined for your desk phone's extension can ring on a number of other devices such as your cellphone, blackberry, home phone, etc.  This feature also allo

260. Single Number III

260. Single Number III DescriptionHintsSubmissionsDiscussSolution DiscussPick One 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 o

Single Number II

题目描写叙述 链接地址 解法 算法解释 题目描写叙述 Given 3*n + 1 numbers, every numbers occurs triple times except one, find it. Example Given [1,1,2,3,3,3,2,2,4,1] return 4 Challenge One-pass, constant extra space. 链接地址 http://www.lintcode.com/en/problem/single-number-ii/

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? 思路: 一开始自己的思路非常简单,首先设置一个status的变量,然后通过2层for循环

leetcode Single Number III

题目连接 https://leetcode.com/problems/single-number-iii/ Single Number III Description 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

LeetCode 笔记26 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? 没辙,智商碾压题.楼主没遇到之前就只会这种做法. public int si