為了練習尋找實習時會碰到的問題,我用別人推薦的 LeetCode online judge。
希望能直接寫程式,不用事先編譯就能直接AC。
下面是我寫的 Single Number 的解法。
這是改良後的版本,這邊是舊版本。
執行時間為 O(n), 並且沒用到額外記憶體!
To practice future interview question, I use LeetCode online judge.
I am trying to type code directly on website without compile in other tool.
The following program is my Single Number solution.
It's a improved version. See the older version here.
Time complexity is O(n), ans I implement it without using extra memory.
嘗試次數 Try number: 1
是否事先在其他工具編譯 if compile first in other tool: No
使用的程式語言 using programming language: C++
以前是否看過 seen this problem before: No
點這裡看題目 Click here to see this Problem!
Please feel free to use it after adding this blog as an reference. (http://autekroy.blogspot.tw) If there is any mistake or comment, please let me know. :D
歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
希望能直接寫程式,不用事先編譯就能直接AC。
下面是我寫的 Single Number 的解法。
這是改良後的版本,這邊是舊版本。
執行時間為 O(n), 並且沒用到額外記憶體!
To practice future interview question, I use LeetCode online judge.
I am trying to type code directly on website without compile in other tool.
The following program is my Single Number solution.
It's a improved version. See the older version here.
Time complexity is O(n), ans I implement it without using extra memory.
嘗試次數 Try number: 1
是否事先在其他工具編譯 if compile first in other tool: No
使用的程式語言 using programming language: C++
以前是否看過 seen this problem before: No
點這裡看題目 Click here to see this Problem!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//see the discussion, then I improve the method | |
class Solution { | |
public: | |
int singleNumber(int A[], int n) { | |
/* | |
if we XOR same number, the result will be 0. Ex: 3 ^ 3 = 0 | |
if we XOR any number with zero, the result will be that number. Ex: 3 ^ 0 = 3 | |
so we can XOR all element, the element which appears twice will counterbalance to 0. | |
Then left the element appears only once. | |
*/ | |
for(int i = 1; i < n; i++) | |
A[0] ^= A[i]; | |
return A[0]; | |
} | |
}; |
Please feel free to use it after adding this blog as an reference. (http://autekroy.blogspot.tw) If there is any mistake or comment, please let me know. :D
歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
en → zh-TW
LeetCode
沒有留言:
張貼留言
請留下您的任何想法或建議!
Please leave any thought or comment!