LeetCode OJ Single Number II
2015-03-23 00:10
260 查看
Given an array of integers, every element appears three times except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
To solve this problem, I used quick_sort to sort the array, and then find...
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
To solve this problem, I used quick_sort to sort the array, and then find...
class Solution { public: int interchange(int A[], int low, int high) { int key = A[low]; while (low < high) { while (low < high && A[high] >= key) high--; if (low < high) A[low++] = A[high]; while (low < high && A[low] <= key) low++; if (low < high) A[high--] = A[low]; } A[low] = key; return low; } void quicksort(int A[], int low, int high) { int new_mid; if (low < high) { new_mid = interchange(A, low, high); quicksort(A, low, new_mid - 1); quicksort(A, new_mid + 1, high); } } int singleNumber(int A[], int n) { if (n < 3) return A[0]; quicksort(A, 0, n - 1); int counter = 1; for (int i = 1; i < n; i++) { if (A[i] == A[i - 1]) { counter++; } else { if (counter != 3) { return A[i - 1]; } else { counter = 1; } } } return A[n - 1]; } };
相关文章推荐
- LeetCode: Single Number II
- [leetcode] Single Number II
- leetcode Single Number II
- Single Number II - LeetCode
- Single Number II--LeetCode
- [LeetCode] 137--Single Number II --Medium--
- [LeetCode]题解(python):137-Single Number II
- leetcode 之 Single Number II
- leetcode-Single Number II
- [LeetCode] 103: Single Number II
- LeetCode Single Number II
- 【Leetcode】Single Number II
- leetcode Single Number II
- Leetcode - Single Number II
- Leetcode_137_Single Number II
- leetcode 日经贴,Cpp code -Single Number II
- [leetcode-137]Single Number II (c)
- LeetCode——Single Number II(找出数组中只出现一次的数2)
- 【LeetCode-面试算法经典-Java实现】【137-Single Number II(只字出一次的数字II)】
- Leetcode算法学习日志-137 Single Number II