Leetcode 136 137 260 SingleNumber I II III
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:
- The order of the result is not important. So in the above example,
[5, 3]is also correct. - 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的更多相关文章
- Leetcode 137. Single Number I/II/III
Given an array of integers, every element appears twice except for one. Find that single one. 本题利用XO ...
- LeetCode(137) Single Number II
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- Leetcode SingleNumber I & II & III 136/137/260
SingleNumber I: 题目链接:https://leetcode-cn.com/problems/single-number/ 题意: 给定一个非空整数数组,除了某个元素只出现一次以外,其余 ...
- leetcode@ [136/137] Single Number & Single Number II
https://leetcode.com/problems/single-number/ Given an array of integers, every element appears twice ...
- 136.137.260. Single Number && 位运算
136. Single Number 意思就是给你一堆数,每个数都出现了两次,只有一个数只出现了一次,找出这个数 位运算(和c艹一样) &:按位与 |:按位或 ^:异或(一样为0,不一样为1) ...
- [LeetCode#136, 137]Single Number, Single Number 2
The question: Single Number Given an array of integers, every element appears twice except for one. ...
- leetcode文章137称号-Single Number II
#include<stdio.h> #include<stdlib.h> int singleNumber(int* nums, int numsSize) { int cou ...
- leetcode 136 137 Single Number
题目描述(面试常考题) 借助了异或的思想 class Solution { public: int singleNumber(vector<int>& nums) { ; ; i ...
- leetcode 136 Single Number, 260 Single Number III
leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...
随机推荐
- C#中的系统时间获取问题
C#获取当前系统时间 2010-01-02 16:24 --DateTime 数字型 System.DateTime currentTime=new System.DateTime(); 取当前年月日 ...
- CODE[VS]-最小数和最大数-整数处理-天梯青铜
题目描述 Description 输入n个数,n<=100,找到其中最小的数和最大的数 输入描述 Input Description 第一行一个整数n 接下来一行n个整数,每个整数不超过231 ...
- 【ARM】S3C6410芯片的启动流程
S3C6410芯片的启动流程 (1) 上电后首先运行iRom(BL0)内的代码,主要完成时钟和看门狗等外围器件的初始化.(2) 拷贝SD卡或者NnadFlash中的前4k(BL1)代码到片内ram(垫 ...
- Unity3D消息:消息传递函数
- CodeForces 685B Kay and Snowflake
树的重心,树形$dp$. 记录以$x$为$root$的子树的节点个数为$sz[x]$,重儿子为$son[x]$,重心为$ans[x]$. 首先要知道一个结论:以$x$为$root$的子树的重心$ans ...
- PAT乙级1004. 成绩排名 (20)
读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每个测试输入包含1个测试用例,格式为 第1行:正整数n 第2行:第1个学生的姓名 学号 成绩 第3行:第2个学生 ...
- 利用LibreOffice与ImageMagick将网页分享至微信
现在越来越多的内容分享都是在微信上进行了.然而,若想将电脑浏览器中看到的感兴趣的网页分享至微信,则只能以纯文本的方式粘贴超级链接,而不能直接拷贝图文混排的HTML.因此,我想到不妨借助LibreOff ...
- Opencv+MFC获取摄像头数据,显示在Picture控件
分为两步:OpenCV获取摄像头数据+图像在Picture上显示 第一步:OpenCV获取摄像头数据 参考:http://www.cnblogs.com/epirus/archive/2012/06/ ...
- PQ分区魔术师v9.0 中文版
软件名称: pqmagic 硬盘分区大师9.0中文绿色版 软件大小:5.80MB 软件语言:简体中文 软件类别:磁盘工具 软件授权:免费软件 更新时间:2013-10-082013-10-08 09: ...
- Python基础(六)-内置函数
map().filter().reduce() map(func,iterator) --> filter(func,iterator) --> reduce(func,iterato ...