1. 程式人生 > >LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III

LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III

136. Single Number

Given an array of integers, every element appears twice except for one. Find that single one. (Easy)

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

分析:

第一問屬於技巧題,做過就會,沒做過很難想。考慮異或操作,相同數異或之後為0, 0與一個數異或還是這個數本身,且異或操作滿足交換律和結合律。

所以這個題將所有的數異或起來,就能得到那個落單的數。

程式碼:

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

137. Single Number II

Given an array of integers, every element appears three times except for one. Find that single one. (Medium)

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

分析:

除了“單身狗”以外其他元素均出現了三次,考慮上一題中的異或的思想(不進位加法,兩個1之後就變成0),推廣到三個數,就是有一位1如果出現三遍,就應該抵消為0;

所以可以考慮建立一個32個元素陣列表示int的各個位置,然後把每個數的每一位對應加進去,mod 3後的結果恢復成一個數即為結果。

程式碼:

 1 class Solution {
 2 public:
 3     int singleNumber(vector<int>& nums) {
 4         int bitArray[32];
 5         memset(bitArray, 0, sizeof(bitArray));
 6         int result = 0;
 7         for (int i = 0; i < 32; ++i) {
 8             for (int j = 0; j < nums.size(); ++j) {
 9                 bitArray[i] += (nums[j] >> i & 1);
10             }
11             bitArray[i] %= 3;
12             result |= (bitArray[i] << i);
13         }
14         return result;
15     }
16 };

260. Single Number 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. (Medium)

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?

分析:

有兩個單身狗,其他都是出現兩次。考慮怎麼把這個問題轉化為single number1的問題。

利用1中的思路,先把所有的數異或,最後可以得到一個數。可以知道這個數(xorResult)是那兩個單身狗異或的結果,但是我們無法從這個數恢復兩個數本身。

但是我們可以xorResult中第一個1出現的位置,這個位置是1說明之前兩個數在這一位不同(一個0,一個1);

於是我們可以把原陣列中的元素這一位是0還是1分為兩個陣列,這兩個陣列便都是single number1的問題,最後把兩個結果新增到vector返回。

程式碼:

 1 class Solution {
 2 public:
 3     vector<int> singleNumber(vector<int>& nums) {
 4         int xorResult = 0;
 5         for (int i = 0; i < nums.size(); ++i) {
 6             xorResult ^= nums[i];
 7         }
 8         int lastBitofOne = xorResult - (xorResult & (xorResult - 1) );
 9         int result1 = 0, result2 = 0;
10         for (int i = 0; i < nums.size(); ++i) {
11             if ( (nums[i] & lastBitofOne) == 0 ) {
12                 result1 ^= nums[i];
13             }
14             else {
15                 result2 ^= nums[i];
16             }
17         }
18         vector<int> result{result1, result2};
19         return result;
20     }
21 };

相關推薦

LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III

136. Single Number Given an array of integers, every element appears twice except for one. Find that single one. (Easy) Note:Your algorithm should have a l

LeetCode260 Single Number 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 eleme

hdu1212 Big Number &第六屆山東省賽Single Round Math (同餘定理,大數取模)

題目大意:每次輸入兩個數,第一個是高精度,第二個數小於100000;求 a mod b 根據同餘定理: (a+b)% c = (a%c+ b%c)%c (a*b)%c =  ( a%c* b%c)%

375. Guess Number Higher or Lower II

ive end guess whether num true possibly find hat We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. Yo

leetcode375 Guess Number Higher or Lower II

span cnblogs ron guess problem higher pan tro size 思路: dp。 https://leetcode.com/problems/guess-number-higher-or-lower-ii/discuss/ 實現: 1

[LeetCode] Number of Distinct Islands II 不同島嶼的個數之二

leetcode and arr sid connected emp after etc sum Given a non-empty 2D array grid of 0‘s and 1‘s, an island is a group of 1‘s (represen

375. Guess Number Higher or Lower II (Python)

下一步 每次 ever 到你 寫代碼 however 思路 時序 簡單 375. Guess Number Higher or Lower II Description We are playing the Guess Game. The game is as follo

LeerCode 375.猜數字大小 II Guess Number Higher or Lower II

題目連結 在1到n之間猜數字,猜錯了,會告訴你大了還是小了。 並且,如果猜的是數字X,錯了,就要支付金額為X的現金;猜對了,就贏了。 那麼至少需要擁有多少現金才能確保你能贏得這個遊戲。 解法可以看其他博主寫的文章 文章連結 基於一些規律,加上題目給出的測試用例比較少,可以嘗試走捷徑。

LC 711. Number of Distinct Islands II

Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-directionally (horizon

[LeetCode] Guess Number Higher or Lower II 猜數字大小之二

We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wro

LintCode 804: Number of Distinct Islands II (BFS/DFS 難題)

Number of Distinct Islands II Given a non-empty 2D array grid of 0’s and 1’s, an island is a group of 1’s (representing land) connected 4-di

LeetCode169 Majority Element, LintCode47 Majority Number II, LeetCode229 Majority Element II, LintCode48 Majority Number III

LeetCode169. Majority Element Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times

LoadRunner壓力測試之Unique Number參數類型、Random Number參數類型淺析

tin borde cin iter dom 固定 date runner 模式 前幾天工作需要用LoadRunner進行壓力測試,期間對手機號進行參數化設置。 當時選用了<Value>137{Random_quhao}{Unique_weiyi}</

ios 解決bug(2)---invalid number of rows in section 0. The number of rows contained。。。

在做UItableview的刪除時,報錯原因如下 invalid number of rows in section 0.  The number of rows contained in an ex

Invalid update: invalid number of rows in section 0. The number of rows contained in an ....

報錯提示: Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Invalid update: invalid number of row

理解TCP序列號(Sequence Number)和確認號(Acknowledgment Number

一、概念及作用 TCP會話的每一端都包含一個32位(bit)的序列號,該序列號被用來跟蹤該端傳送的資料量。每一個包中都包含序列號,在接收端則通過確認號用來通知傳送端資料成功接收。 二、TCP三次握手 TCP標誌位 TCP在其協議頭中使用大量的標誌位或者說1位(bi

[LeetCode] 729. My Calendar I 731. My Calendar II 732. My Calendar III 題解

tps 二叉 tco 指定 start class 個數 否則 .com 題目描述 MyCalendar主要實現一個功能就是插入指定起始結束時間的事件,對於重合的次數有要求。 MyCalendar I要求任意兩個事件不能有重疊的部分,如果插入這個事件會導致重合,則插入失敗

從μC/OS-II到μC/OS-III的各種改進

編者按:    μC/OSII對我國嵌入式實時作業系統的普及與推廣起到了十分積極的作用,在嵌入式系統教學、研究以及開發應用等方面頗有影響。2011年8月,μC/OSIII的原始碼在Micrium網站上公開,《μC/OSIII The Real Time Kernel》一書的修訂本也陸續釋出到網上,書中涉及

[Swift]LeetCode137. 只出現一次的數字 II | Single Number II

Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one

Leetcode137 Single Number II

136題的加強版,有三個相同數字出現了。 class Solution { public int singleNumber(int[] nums) { if(nums.length==1) return nums[0]; int ones=0,twos=0;