136 Single Number(找唯一数Medium)

题目意思:一个int数组,有一个数只出现一次,其他数均出现两次,找到这个唯一数

知识普及:~:非运算,单目运算符1为0,0为1;

       &:与运算,都为1则为1,否则为0

     |:或运算,全为0则为0,否则为1

          ^:异或运算,相同为0,不同为1

思路:将数组中元素进行异或运算,则只剩下0和唯一数,异或得到的是唯一数

 1 class Solution {
 2 public:
 3     int singleNumber(vector<int>& nums) {
 4         int res=0;
 5         for(int i=0;i<nums.size();++i){
 6             res^=nums[i];
 7         }
 8         return res;
 9     }
10 };

时间复杂度:O(n)

时间: 2024-10-25 11:26:21

136 Single Number(找唯一数Medium)的相关文章

[LeetCode] 260. Single Number III 单独数 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. Example: Input: [1,2,1,3,2,5] Output: [3,5] Note: The order of the result is

LeetCode 136. Single Number &amp; 268. Missing Number

136. Single Number 考察的是异或运算.相同的数异或结果为0,一个数与0异或还是原来的数,以及异或符合交换律.因此,把所有的数都异或起来,结果就是落单的那个数. class Solution { public: int singleNumber(vector<int>& nums) { int res=0; for (int num:nums){ res ^= num; } return res; } }; 268. Missing Number 可以用数学方法直接做,

136. Single Number C++ 答案

136. Single Number -- Easy 解答 相同的数,XOR 等于 0,所以,将所有的数字 XOR 就可以得到只出现一次的数 class Solution { public: int singleNumber(vector<int>& nums) { int s = 0; for(int i = 0; i < nums.size(); i++) { s = s ^ nums[i]; } return s; } }; 参考 LeetCode Problems' So

【59】136. Single Number

136. Single Number Description Submission Solutions Add to List Total Accepted: 191020 Total Submissions: 360448 Difficulty: Easy Contributors: Admin Given an array of integers, every element appears twice except for one. Find that single one. Note:Y

136. Single Number &amp;&amp; 137. Single Number II &amp;&amp; 260. Single Number III

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? Subscribe to see which co

136. Single Number - LeetCode

Question 136.?Single Number Solution 思路:构造一个map,遍历数组记录每个数出现的次数,再遍历map,取出出现次数为1的num public int singleNumber(int[] nums) { Map<Integer, Integer> countMap = new HashMap<>(); for (int i=0; i<nums.length; i++) { Integer count = countMap.get(nums

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循环

136. Single Number leetcode做题报告

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? class Solution { public: int singleNumber(vec

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 runtime complexity. Could you i