Leetccode 136 SingleNumber I

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?

1.自己想到了先排序,再遍历数组找,复杂度较高

2.参考其他想法,最好的是利用异或,详见代码

import java.util.Arrays;

public class S136 {

    public int singleNumber(int[] nums) {
//AC but not good
/* Arrays.sort(nums);
int i = 0;
for(;i<nums.length-1;i+=2){
if(nums[i]!=nums[i+1]){
return nums[i];
}
}
return nums[i];*/
//best one 异或运算的神奇之处 1.a^b == b^a 2.0^a == a
if(nums.length<1)
return 0;
int ret = nums[0];
for(int i = 0;i<nums.length;i++){
ret = ret^nums[i];
}
return ret;
}
}

Leetccode 137 SingleNumber 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.利用排序和遍历同样可以AC

2.利用异或,详见代码

public class S137 {
public int singleNumber(int[] nums) {
//AC but not good
/* Arrays.sort(nums);
int i = 0;
for(;i<nums.length-1;i+=3){
if(nums[i]!=nums[i+1]){
return nums[i];
}
}
return nums[i];*/
//a general algorithm
int a[] = new int[32];
int ret = 0;
for(int i = 0;i<32;i++){
for(int j = 0;j<nums.length;j++){
a[i] += (nums[j]>>i)&1; }
ret |= (a[i]%3)<<i;
}
return ret;
}
}

Leetccode 260 SingleNumber 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?

思路:先将所有元素异或得到的结果ret肯定不为零,再移位寻找第一个不为零的二进制位,记录位置pos。再遍历数组,将所有pos位置为零的数异或得到num1,所有pos位置为一的数异或得到num2,num1和num2即answer。因为ret中不为零的二进制位所对应位肯定是num1和num2对应位异或,必定是num1和num2此位不同,将数组分为两组分别异或其实就是第一种情况的解法了。详见代码

public class S260 {
public int[] singleNumber(int[] nums) {
int num1= 0,num2 = 0;
int ret = 0;
for(int i = 0;i<nums.length;i++){
ret ^= nums[i];
}
int pos = 0;
for(;pos<32;pos++){
if((ret>>pos&1) == 1){
break;
}
}
for(int i = 0;i<nums.length;i++){
if((nums[i]>>pos&1)==1){
num1 ^= nums[i];
}else{
num2 ^= nums[i];
}
}
int rets[] = {num1,num2};
return rets;
}
}

Leetcode 136 137 260 SingleNumber I II III的更多相关文章

  1. Leetcode 137. Single Number I/II/III

    Given an array of integers, every element appears twice except for one. Find that single one. 本题利用XO ...

  2. LeetCode(137) Single Number II

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

  3. Leetcode SingleNumber I & II & III 136/137/260

    SingleNumber I: 题目链接:https://leetcode-cn.com/problems/single-number/ 题意: 给定一个非空整数数组,除了某个元素只出现一次以外,其余 ...

  4. leetcode@ [136/137] Single Number & Single Number II

    https://leetcode.com/problems/single-number/ Given an array of integers, every element appears twice ...

  5. 136.137.260. Single Number && 位运算

    136. Single Number 意思就是给你一堆数,每个数都出现了两次,只有一个数只出现了一次,找出这个数 位运算(和c艹一样) &:按位与 |:按位或 ^:异或(一样为0,不一样为1) ...

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

    The question: Single Number Given an array of integers, every element appears twice except for one. ...

  7. leetcode文章137称号-Single Number II

    #include<stdio.h> #include<stdlib.h> int singleNumber(int* nums, int numsSize) { int cou ...

  8. leetcode 136 137 Single Number

    题目描述(面试常考题) 借助了异或的思想 class Solution { public: int singleNumber(vector<int>& nums) { ; ; i ...

  9. leetcode 136 Single Number, 260 Single Number III

    leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...

随机推荐

  1. NOIP2016游记(非题解)

    去年的比赛现在来发是不是晚了. -------------------------------- Day1-白天 出发啦, 动车购票处一群丧病的又在玩售票机 动车上看到胡神打苍蝇 苍蝇打苍蝇 在车上颓 ...

  2. libvirt里的面向对象的C语言

    C语言:类的声明和定义 // 通用父类的定义 struct _virClass { virClassPtr parent; unsigned int magic; char *name; size_t ...

  3. glibc漏洞监测并修复

    [CVE 2015-0235: GNU glibc gethostbyname 缓冲区溢出漏洞 ]全面爆发,该漏洞的产生是Qualys公司在进行内部代码审核时,发现了一个在GNU C库(glibc)中 ...

  4. POJ 2484 A Funny Game

    博弈. $n>=3$,后手赢,否则先手赢. #pragma comment(linker, "/STACK:1024000000,1024000000") #include& ...

  5. CodeForces 711C Coloring Trees

    简单$dp$. $dp[i][j][k]$表示:前$i$个位置染完色,第$i$个位置染的是$j$这种颜色,前$i$个位置分成了$k$组的最小花费.总复杂度$O({n^4})$. #pragma com ...

  6. hdu1033

    #include<stdio.h> #include<string.h> const int MAXN=200; char str[MAXN]; int main() { in ...

  7. GL应用方面

    1.图和表 2.计算机辅助设计CAD 3.虚拟现实环境 4.数据可视化 5.教学与培训(基于VR) 6.计算机艺术 7.娱乐 8.图像处理 9.用户界面

  8. 专注VR/AR广告 ,内容感知广告公司Uru获80万美元投资

    随着AR/VR技术不断地跃进,越来越多的公司开始运用这项技术为消费者提供广告和营销信息.Uru是一家打造计算机视觉驱动内容广告的公司,专注于数字视频和VR/AR类似的沉浸式媒介,就在刚刚这家公司宣布完 ...

  9. windows 开机自动启动方案

    方案1: 把要启动的软件的快捷方式放到启动菜单对应的目录里,就像下面这个路径: C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Sta ...

  10. [kuangbin带你飞]专题六 最小生成树 POJ 1287 Networking

    最小生成树模板题 跑一次kruskal就可以了 /* *********************************************** Author :Sun Yuefeng Creat ...