关闭

leetcode136

标签: leetcode位运算
152人阅读 评论(0) 收藏 举报
分类:

1、Single Number
Given an array of integers, every element appears twice except for one. Find that single one.
由于题中所有非singlenumber都是出现两次,可以用位运算,将两个相同的数字直接异或后清零,剩下的就是single number啦~

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        for(int i=1;i<nums.size();i++)
            nums[0]=nums[0]^nums[i];
        return nums[0];
    }
};

对于数字,位运算运算速度快~对于字母,ASCII码运算速度快~
a^b按位异或:相同为0,不同为1;近似为无进位的相加
a&b:都为1得1,否则为0;
a>>1:右移一位,相当于实现对除数为2的整除运算;
a<<1:左移一位,相当于乘2;
应用:
清零:a&0
删除两个相同数字:a^a
数值交换:
a=a^b;
b=b^a;
a=a^b

0
0
查看评论

LeetCode(136)Single Number

LeetCode刷题刷到现在,经常感到挫败。因为有些题目一想就是一整天,提交上去一看还超时了。太伤心了。现在采取更加科学合理的方式,按照AC率降序来刷题。不再采取以前的按照日期来刷的方式了。 题目如下: Given an array of integers, every element appear...
  • feliciafay
  • feliciafay
  • 2014-01-04 13:09
  • 1101

Leetcode_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 run...
  • pistolove
  • pistolove
  • 2015-01-14 18:04
  • 1293

LeetCode 136. Single Number 题解(C++)

Given an array of integers, every element appears twice except for one. Find that single one.
  • prayyojay
  • prayyojay
  • 2016-09-29 12:07
  • 346

【LeetCode-136】Single Number(C++)

题目:给出一个整形数组,数组中的数据只有一个数是单个的,其他的数都有两个,返回这个单个的数。 解决方法:1. 先将数组排序,如果这个数的左边和右边均与这个数不相等,那么这个数就是要返回的数。这种实现方法的时间复杂度是O(n),其中n为数组中的元素个数,并且没有浪费多余的空间。 class Sol...
  • liujiayu1015
  • liujiayu1015
  • 2016-08-30 22:09
  • 137

leetcode136题解

leetcode 136.Single Number题目Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm shoul...
  • qq_26431347
  • qq_26431347
  • 2017-06-02 20:25
  • 174

LeetCode136 Single Number

详细见:leetcode.com/problems/single-number Java Solution: github package leetcode; /* * Given an array of integers, every element appears twic...
  • zxwtry
  • zxwtry
  • 2017-05-18 22:23
  • 180

leetcode136: Single Number

要求:给出一个数组,数组中的每个元素出现两次,有一个元素只出现一次,寻找这个只出现一次使得数。另外,要求时间复杂度是线性的(这是关键) 如果对于时间复杂度没有要求,遍历数组并将每个元素两两比较即可得出答案,此时的时间复杂度应当是O(n^2) 由于题目要求时间复杂度是线性,所以不能在for中嵌套f...
  • sinat_35425429
  • sinat_35425429
  • 2016-10-23 10:20
  • 90

[LeetCode136]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 ...
  • xiuwunianhua
  • xiuwunianhua
  • 2016-09-06 07:21
  • 68

Leetcode136: Word Search

Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where &qu...
  • u013089961
  • u013089961
  • 2015-11-03 10:12
  • 120

LeetCode136——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 ...
  • booirror
  • booirror
  • 2015-02-27 10:40
  • 1154
    个人资料
    • 访问:9417次
    • 积分:665
    • 等级:
    • 排名:千里之外
    • 原创:59篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条