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. Duilib使用wke显示echarts

    不得不说wke是个简洁好用的浏览器内核.网上很多大神已经把wke嵌入到duilib中了,先感谢他们辛勤的工作.这里通过wke吧C++的数据在ECharts上美观的显示出来.借鉴前人,将ECharts进 ...

  2. 【XML】document.createEvent的使用方法

    <aclass="comment-mod"onclick="alert('ss')"href="#">评论</a> ...

  3. 关于scanf的返回值

    今天写一个小程序时误用while(scanf("%d",&b)!=a),以为scanf的返回值就是输入的值. 其实真相是: scanf返回接收到的变量值的个数. int a ...

  4. drupal7 sql接口笔记

    1.查询: execute() ->fetch():从结果集中取出一行作为一个对象 execute() ->fetchField():获取单个值 execute() ->fetchA ...

  5. Spring之ContextLoaderListener的作用

    Spring org.springframework.web.context.ContextLoaderListener public class ContextLoaderListener exte ...

  6. python2到python3的转换以及f.write在python3 中的用法

    .利用Python内置(Python脚本)工具,帮你自动转换 Python 2.x版本,比如我安装的Python 2.7.2,其在windows下载安装好之后,就自带了相关的一些有用的工具. 其中一个 ...

  7. TCP和UDP报文分片的区别

    搞了三年网络,今天才知道这个细节,汗,总结下: MTU大家都知道,是链路层中的网络对数据帧的一个限制,依然以以太网为例,MTU为1500个字节.一个IP数据报在以太网中 传输,如果它的长度大于该MTU ...

  8. Python基础篇-day7

    本节目录-面向对象1 类介绍1.1 面向对象oo特征1.2 类的特性1.3 创建与调用 1.3.1 基本结构 1.3.2 结构说明 1.3.3 对外部提供只读访问接口 1.3.4 析构方法2 继承2. ...

  9. mysql for windows zip版安装

    1.将mysql_5.6.24_winx64.zip 解压到文件夹 2.增加环境变量 3.修改mysql配置文件 将mysql根目录下的my-default.ini 复制一份更名为 my.ini.修改 ...

  10. CustomSummaryCalculate 用法

    private void gridView1_CustomSummaryCalculate(object sender, DevExpress.Data.CustomSummaryEventArgs ...