LeetCode——Single Number(136)
Given a non-empty 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?
Example 1:
Input: [2,2,1] Output: 1
Example 2:
Input: [4,1,2,1,2] Output: 4
分析:
找出陣列中只出現一次的數字,用異或2次會互相抵消的特性,當將數組裡面的所有數異或一遍後,結果只會留下那個只出現1次的。
程式碼:
class Solution {
public:
int singleNumber(vector<int>& nums) {
int n=nums[0];
for(int i=1;i<nums.size();++i)
n=n^nums[i];
return n;
}
};
相關推薦
LeetCode——Single Number(136)
Given a non-empty array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have
【python3】leetcode 136. Single Number (easy)
136. Single Number (easy) Given a non-empty array of integers, every element appears twice except for one. Find that singl
Leetcode 136 Single Number(水)
解題思路:求所有數的亦或和。class Solution { public: int singleNumber(vector<int>& nums) { sort(num
LeetCode:136, Single Number(找出單個數字)
Given a non-empty array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have
LeetCode 136 Single Number(只出現一次的數字)
翻譯 給定一個整型陣列,除了某個元素外其餘元素均出現兩次。找出這個只出現一次的元素。 備註: 你的演算法應該是一個線性時間複雜度。你可以不用額外空間來實現它嗎? 原文 Given a
【LeetCode-面試演算法經典-Java實現】【136-Single Number(只出現一次的數字)】
原題 Given an array of integers, every element appears twice except for one. Find that singl
leetcode 136 Single Number(只出現一次的數字) python3 多種思路(建立字典/排序/巧用異或)
class Solution: def singleNumber(self, nums): """ :type nums: List[int] :
leetcode【165】Single Number(關於異或)
寫在最前面:除了介紹這道題本身以外,會講一講python中的異或 leetcode【165】Single Number Your algorithm should have a linear runtime complexity. Could you implement it witho
LeetCode演算法題-Single Number(Java實現)
這是悅樂書的第175次更新,第177篇原創 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第34題(順位題號是136)。給定一個非空的整數陣列,除了一個元素外,每個元素都會出現兩次。 找到那個只出現了一次的元素。例如: 輸入:[2,2
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
LeetCode:17. Letter Combinations of a Phone Number(Medium)
class stat def es2017 不同的 進行 先進先出 ati tps 1. 原題鏈接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/ 2. 題目要
【python3】leetcode 268. Missing Number (easy)
268. Missing Number (easy) Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is
【LeetCode】754. Reach a Number(C++)
題目: You are standing at position 0 on an infinite number line. There is a goal at position target. On each move, you can either go
【LeetCode & 劍指offer刷題】特殊數題4:263 Ugly Number(系列)
【LeetCode & 劍指offer 刷題筆記】目錄(持續更新中...) 263. Ugly Number Write a program to check whether a given number is an ugly number
leetcode 202.Happy Number (python3 )
題目: 題目分析:首先,本題需要判定輸入的整數其各位數平方求和最終值是否為1 ?輸入整數位整型,返回值為布林型。 程式設計思路: 1.需要利用到迴圈實現每一次求和後判定是否為1 。 2.需要排除有可能出現死迴圈的狀況,經過幾次平方求和後,結果為某一個出現過
LeetCode | Palindrome Number(迴文數字)
Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra
LeetCode OJ: 9. Palindrome Number (C++)
題目:Determine whether an integer is a palindrome. Do this without extra space. 解法1、需要額外空間: 儲存各位數字,
[LeetCode] Single Number
could implement col algo public xtra arr com note Given an array of integers, every element appears twice except for one. Find that singl
LeetCode——Single Number
i++ entry 思路 etc 給定 size cep ret entryset Given an array of integers, every element appears twice except for one. Find that single
leetcode single-number
ole could subject ber 使用 代碼 [] ron lin 題目描述 Given an array of integers, every element appears twice except for one. Find that single