File tree Expand file tree Collapse file tree 3 files changed +48
-1
lines changed
src/com/andavid/leetcode/_136 Expand file tree Collapse file tree 3 files changed +48
-1
lines changed Original file line number Diff line number Diff line change 2020| 122 | [ Best Time to Buy and Sell Stock II] [ 122 ] |
2121| 189 | [ Rotate Array] [ 189 ] |
2222| 217 | [ Contains Duplicate] [ 217 ] |
23-
23+ | 136 | [ Single Number ] [ 136 ] |
2424
2525[ leetcode ] : https://leetcode.com/problemset/all/
2626[ blankj ] : https://github.com/Blankj/awesome-java-leetcode
3131[ 122 ] : https://github.com/andavid/leetcode-java/blob/master/note/122/README.md
3232[ 189 ] : https://github.com/andavid/leetcode-java/blob/master/note/189/README.md
3333[ 217 ] : https://github.com/andavid/leetcode-java/blob/master/note/217/README.md
34+ [ 136 ] : https://github.com/andavid/leetcode-java/blob/master/note/136/README.md
Original file line number Diff line number Diff line change 1+ # [ Single Number] [ title ]
2+
3+ ## Description
4+
5+ Given an array of integers, every element appears twice except for one. Find that single one.
6+
7+ Note:
8+ Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
9+
10+ ## 思路
11+
12+ 给定一个数组,数组中除了一个数只出现了一次,其他数都出现了两次,找出那个数。
13+
14+ 把所有的数字进行异或运算即可。
15+
16+
17+ ## [ 完整代码] [ src ]
18+
19+ ``` java
20+ class Solution {
21+ public int singleNumber (int [] nums ) {
22+ int n = 0 ;
23+ for (int num : nums) {
24+ n ^ = num;
25+ }
26+ return n;
27+ }
28+ }
29+ ```
30+
31+ [ title ] : https://leetcode.com/problems/single-number
32+ [ src ] : https://github.com/andavid/leetcode-java/blob/master/src/com/andavid/leetcode/_136/Solution.java
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int singleNumber (int [] nums ) {
3+ int n = 0 ;
4+ for (int num : nums ) {
5+ n ^= num ;
6+ }
7+ return n ;
8+ }
9+
10+ public static void main (String [] args ) {
11+ Solution solution = new Solution ();
12+ System .out .println (solution .singleNumber (new int []{1 , 2 , 1 }));
13+ }
14+ }
You can’t perform that action at this time.
0 commit comments