Skip to content

Commit ee4a0b8

Browse files
committed
added Single Number 2 (medium)
1 parent 75746b1 commit ee4a0b8

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

Hard/EscapeLargeMaze/solution.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ def isEscapePossible(self, blocked: List[List[int]], source: List[int], target:
99
visited = set()
1010
queue.append(source)
1111
while queue:
12-
print("queue:", queue)
13-
print("visited:", visited)
12+
# print("queue:", queue)
13+
# print("visited:", visited)
1414
currentCoord = queue.pop(0)
1515
if currentCoord == target:
1616
return True
@@ -30,7 +30,7 @@ def isEscapePossible(self, blocked: List[List[int]], source: List[int], target:
3030

3131
# test driver
3232
sol = Solution()
33-
blocked = [[1,2], [0,2], [2,2]]
33+
blocked = []
3434
source = [0,0]
35-
target = [3,3]
35+
target = [9, 9]
3636
print("Output:", sol.isEscapePossible(blocked, source, target))

Medium/SingleNumber2/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
# Single Number II
3+
[Leetcode Link](https://leetcode.com/problems/single-number-ii/)
4+
5+
## Problem:
6+
7+
Given a **non-empty** array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
8+
9+
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
10+
11+
## Example:
12+
13+
```
14+
Input: [2,2,3,2]
15+
Output: 3
16+
```
17+
```
18+
Input: [0,1,0,1,0,1,99]
19+
Output: 99
20+
```

Medium/SingleNumber2/solution.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from typing import List
2+
3+
class Solution:
4+
def singleNumber(self, nums: List[int]) -> int:
5+
binSum = 0
6+
mask = bin((1<<32) -1)
7+
uniqueBin = ""
8+
for num in nums:
9+
binary = int(bin(num & int(mask,2))[2:])
10+
print(num, "=", binary)
11+
binSum += binary
12+
print("sum =", binSum)
13+
for bit in str(binSum):
14+
if int(bit) % 3 != 0:
15+
uniqueBin += "1"
16+
else:
17+
uniqueBin += "0"
18+
print("unique number in binary:", uniqueBin)
19+
if int(uniqueBin, 2) > 2**31:
20+
uniqueBin = str(bin(int(uniqueBin,2)-1))
21+
print(uniqueBin)
22+
print(bin((~int(uniqueBin, 2) & int(mask,2))))
23+
return -(~int(uniqueBin, 2) & int(mask,2))
24+
return int(uniqueBin, 2)
25+
26+
27+
# test driver
28+
sol = Solution()
29+
input = [-19,-46,-19,-46,-9,-9,-19,17,17,17,-13,-13,-9,-13,-46,-28]
30+
print("Input:", input)
31+
print("Output:", sol.singleNumber(input))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Languages used: Java and Python
7878
- [Number of Operations to Make Network Connected](Medium/NumToMakeNetworkConnected)
7979
- [Sort Colors](Medium/SortColors)
8080
- [Design Linked List](Medium/DesignLinkedList)
81+
- [Single Number II](Medium/SingleNumber2)
8182
- Hard
8283
- [Maximum Score Words Formed by Letters](Hard/MaximumScoreWords)
8384
- [Reducing Dishes](Hard/ReducingDishes)

0 commit comments

Comments
 (0)