Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 815ac00

Browse files
D. J.:
- Added the leetcode problem and solution for 260
1 parent 9cd69c8 commit 815ac00

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@
213213
- [236 Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/)
214214
- [238 Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/description/)
215215
- [242 Valid Anagram](https://leetcode.com/problems/valid-anagram/description/)
216+
- [260 Single Number III](https://leetcode.com/problems/single-number-iii/description/)
216217
- [274 H-Index](https://leetcode.com/problems/h-index/description/)
217218
- [279 Perfect Squares](https://leetcode.com/problems/perfect-squares/description/)
218219
- [289 Game of Life](https://leetcode.com/problems/game-of-life/description/)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def singleNumber(self, nums: List[int]) -> List[int]:
8+
"""
9+
Given an integer array nums, in which exactly two elements appear only once and
10+
all the other elements appear exactly twice. Find the two elements that appear
11+
only once. You can return the answer in any order.
12+
13+
You must write an algorithm that runs in linear runtime complexity and uses
14+
only constant extra space.
15+
"""
16+
# Get the XOR product of the two distinct values
17+
result = 0
18+
for n in nums:
19+
result ^= n
20+
21+
# Get the position of the first 1-bit
22+
k = 0
23+
while result & 1 == 0:
24+
result >>= 1
25+
k += 1
26+
27+
# Group the values according to the first 1-bit
28+
num1, num2 = 0, 0
29+
for n in nums:
30+
if (n & (1 << k)) >> k == 1:
31+
num1 ^= n
32+
else:
33+
num2 ^= n
34+
35+
return [num1, num2]

‎tests/test_260_single_number_III.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._260_single_number_III import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["nums", "expected"],
10+
argvalues=[
11+
([1, 2, 1, 3, 2, 5], [3, 5]),
12+
([-1, 0], [-1, 0]),
13+
([0, 1], [1, 0]),
14+
],
15+
)
16+
def test_func(nums: List[int], expected: List[int]):
17+
"""Tests the solution of a LeetCode problem."""
18+
single_numbers = Solution().singleNumber(nums)
19+
assert single_numbers == expected

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /