Skip to content

Commit b7f1e6e

Browse files
author
Jack
committed
add some solutions
1 parent 6d6d930 commit b7f1e6e

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

Python/single-number-ii.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
#
66
# Note:
77
# Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
8-
#
8+
import collections
99

10-
class Solution:
10+
11+
class Solution(object):
1112
# @param A, a list of integer
1213
# @return an integer
1314
def singleNumber(self, A):
@@ -16,7 +17,7 @@ def singleNumber(self, A):
1617
one, two = (~x & one) | (x & ~one & ~two), (~x & two) | (x & one)
1718
return one
1819

19-
class Solution2:
20+
class Solution2(object):
2021
# @param A, a list of integer
2122
# @return an integer
2223
def singleNumber(self, A):
@@ -29,8 +30,27 @@ def singleNumber(self, A):
2930
two &= ~carry
3031
return one
3132

33+
34+
class Solution3(object):
35+
def singleNumber(self, nums):
36+
"""
37+
:type nums: List[int]
38+
:rtype: int
39+
"""
40+
return (collections.Counter(list(set(nums)) * 3) - collections.Counter(nums)).keys()[0]
41+
42+
43+
class Solution4(object):
44+
def singleNumber(self, nums):
45+
"""
46+
:type nums: List[int]
47+
:rtype: int
48+
"""
49+
return (sum(set(nums)) * 3 - sum(nums)) / 2
50+
51+
3252
# every element appears 4 times except for one with 2 times
33-
class SolutionEX:
53+
class SolutionEX(object):
3454
# @param A, a list of integer
3555
# @return an integer
3656
# [1, 1, 1, 1, 2, 2, 2, 2, 3, 3]

Python/single-number-iii.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
# above example, [5, 3] is also correct.
1515
# Your algorithm should run in linear runtime complexity.
1616
# Could you implement it using only constant space complexity?
17-
#
17+
import operator
18+
import collections
19+
1820

1921
class Solution:
2022
# @param {integer[]} nums
@@ -26,7 +28,8 @@ def singleNumber(self, nums):
2628
for i in nums:
2729
result[bool(i & bit)] ^= i
2830
return result
29-
31+
32+
3033
class Solution2:
3134
# @param {integer[]} nums
3235
# @return {integer[]}
@@ -43,3 +46,12 @@ def singleNumber(self, nums):
4346
x ^= i
4447

4548
return [x, x ^ x_xor_y]
49+
50+
51+
class Solution3(object):
52+
def singleNumber(self, nums):
53+
"""
54+
:type nums: List[int]
55+
:rtype: List[int]
56+
"""
57+
return [x[0] for x in sorted(collections.Counter(nums).items(), key=lambda i: i[1], reverse=False)[:2]]

Shell/valid-phone-numbers.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env bash
12
# Time: O(n)
23
# Space: O(1)
34
#

0 commit comments

Comments
 (0)