Leetcode 137. Single Number II
方法1: 136的升級版。依舊是bit manipulation。我感覺這題目老難了。我現在看了都沒咋看懂,更別說面試時候自己寫出來了。時間複雜n,空間複雜1.
class Solution {
public int singleNumber(int[] nums) {
int seenOnce = 0, seenTwice = 0;
for (int num : nums) {
// first appearence:
// add num to seen_once
// don't add to seen_twice because of presence in seen_once
// second appearance:
// remove num from seen_once
// add num to seen_twice
// third appearance:
// don't add to seen_once because of presence in seen_twice
// remove num from seen_twice
System.out.println(~seenTwice) ;
seenOnce = ~seenTwice & (seenOnce ^ num);
System.out.println(~seenOnce) ;
seenTwice = ~seenOnce & (seenTwice ^ num);
}
return seenOnce;
}
}
總結:
- 無
相關文章
- 137-Single Number II
- [LeetCode] 305. Number of Islands IILeetCode
- Leetcode 之 PHP 解析 (260. Single Number III)LeetCodePHP
- 136. Single Number
- 260-Single Number III
- Leetcode Number of islandsLeetCode
- [LeetCode] Third Maximum NumberLeetCode
- [LeetCode] Find the Duplicate NumberLeetCode
- Leetcode 9 Palindrome NumberLeetCode
- Leetcode 447 Number of BoomerangsLeetCodeOOM
- Leetcode 933 Number of Recent CallsLeetCode
- [LeetCode] 9. Palindrome NumberLeetCode
- LeetCode之Fibonacci Number(Kotlin)LeetCodeKotlin
- leetcode-90. Subsets IILeetCode
- Leetcode 40 Combination Sum IILeetCode
- Leetcode 213 House Robber IILeetCode
- LeetCode 1103[分糖果II]LeetCode
- Leetcode 611 javascript Valid Triangle NumberLeetCodeJavaScript
- LeetCode之Number of Recent Calls(Kotlin)LeetCodeKotlin
- [LeetCode] 248. Strobogrammatic Number IIILeetCode
- [LeetCode] 191. Number of 1 BitsLeetCode
- Leetcode 17 Letter Combinations of a Phone NumberLeetCode
- 【Leetcode】1395. Count Number of TeamsLeetCode
- [LeetCode] 210. Course Schedule IILeetCode
- [LeetCode] 212. Word Search IILeetCode
- [Leetcode]253. Meeting Rooms IILeetCodeOOM
- [LeetCode] 2105. Watering Plants IILeetCode
- LeetCode 52. N皇后 IILeetCode
- [leetcode]linked-list-cycle-iiLeetCode
- leetcode 219. Contains Duplicate IILeetCodeAI
- [LeetCode] 910. Smallest Range IILeetCode
- [LeetCode] 45. Jump Game IILeetCodeGAM
- LeetCode-047-全排列 IILeetCode
- [LeetCode] 3152. Special Array IILeetCode
- LeetCode Palindrome Number(009)解法總結LeetCode
- Leetcode – 017. Letter Combinations of a Phone NumberLeetCode
- [LeetCode] 3238. Find the Number of Winning PlayersLeetCode
- [LeetCode] 2684. Maximum Number of Moves in a GridLeetCode