Skip to content

Commit e758b5b

Browse files
Added python code for 189. Rotate Array & 137. Single Number II (#98)
* Create Rotate-List.py and update README * minor fix of the link * Added 137. Single Number II Co-authored-by: Gourav Rusiya <[email protected]>
1 parent 8a93e0c commit e758b5b

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

Python/rotate-array.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def rotate(self, nums: List[int], k: int) -> None:
3+
"""
4+
Do not return anything, modify nums in-place instead.
5+
"""
6+
lenz = len(nums)
7+
k = k % lenz
8+
if k == 0:
9+
return nums
10+
nums += nums[:-k]
11+
del nums[:lenz-k]
12+

Python/single-number-ii.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def singleNumber(self, nums: List[int]) -> int:
3+
dict1 = {} # Create hashtable
4+
# Add counts into the hashtable
5+
for x in nums:
6+
if x not in dict1:
7+
dict1[x] = 1
8+
else:
9+
dict1[x] += 1
10+
# Select the single element
11+
for ans, y in dict1.items():
12+
if y == 1:
13+
return ans
14+
15+
# Or bitwise way
16+
class Solution:
17+
def singleNumber(self, nums: List[int]) -> int:
18+
a, b = 0, 0
19+
# Just bitwise operation, (notx and a and notb) or (x and nota and b) ...
20+
for x in nums:
21+
a, b = (~x&a&~b)|(x&~a&b), ~a&(x^b)
22+
return b

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,12 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
8383
| # | Title | Solution | Time | Space | Difficulty | Tag | Tutorial |
8484
| ---- | --------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- | ------ | ------ | ---------- | --- | ---------------------------------------- |
8585
| 0136 | [Single Number](https://leetcode.com/problems/single-number/) | [Java](./Java/single-number.java) <br> [Python](./Python/single-number.py) <br> [C++](./C++/Single-Number.cpp) | _O(n)_ | _O(1)_ | Easy | | Using XOR |
86+
| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [Python](./Python/single-number-ii.py) | _O(n)_ | _O(1)_ | Medium | | |
8687
| 260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [Python](./Python/single-number-iii.py) | _O(n)_ | _O(1)_ | Medium | | |
8788
| 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [Java](./Java/number-complement.java) | _O(1)_ | _O(1)_ | Easy | | [Tutorial](https://youtu.be/6bp5V-O3zts) |
8889
| 520 | [Detect Capital Use](https://leetcode.com/problems/detect-capital/) | [Python](./Python/detect-capital.py) | _O(n)_ | _O(1)_ | Easy | | |
8990

91+
9092
<br/>
9193
<div align="right">
9294
<b><a href="#algorithms">⬆️ Back to Top</a></b>
@@ -119,6 +121,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
119121
| 152 | [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) | [Javascript](./JavaScript/152.Maximum-Product-Subarray.js) | O(n) | O(n) | Medium | Array |
120122
| 073 | [Set-Matrix-Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) | [Java](./Java/set-matrix-zeroes.java) | O(MN) | O(1) | Medium | Array |
121123
| 1288 | [Remove-Covered-Intervals](https://leetcode.com/problems/remove-covered-intervals) | [C++](./C++/Remove-Covered-Intervals.cpp) | O(N*N) | O(1) | Medium | Array |
124+
| 189 | [Rotate-Array](https://leetcode.com/problems/rotate-array/) | [Python](./Python/rotate-array.py) | O(N) | O(1) | Medium | Array
122125

123126
<br/>
124127
<div align="right">
@@ -414,6 +417,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
414417
| [Jaseem ck](https://github.com/Jaseemck) <br> <img src="https://github.com/Optider.png" width="100" height="100"> | India | Python | [Github](https://github.com/Jaseemck) |
415418
| [Ilias Khan](https://github.com/IliasKhan) <br> <img src="https://avatars3.githubusercontent.com/u/26863936?s=460&u=4501ffba5efd1a7b978416e8e434dff599c85384&v=4" width="100" height="100"> | India | C++ | [codechef](https://www.codechef.com/users/iliaskhan) <br> [Hackerrank](ckerrank.com/iliaskhan57) <br> [LeetCode](https://leetcode.com/ilias_khan/) <br> [codeforces](http://codeforces.com/profile/iliaskhan) |
416419
| [Shamoyeeta Saha](https://github.com/Shamoyeeta) <br> <img src="https://i.pinimg.com/236x/dc/ef/3a/dcef3abedf0e0761203aaeb85886a6f3--jedi-knight-open-source.jpg" width="100" height="100"> | India | C++ | [Hackerrank](https://www.hackerrank.com/sahashamoyeeta) <br> [Github](https://github.com/Shamoyeeta) |
420+
| [James Y](https://github.com/jameszu) <br> <img src="https://avatars0.githubusercontent.com/u/41566813?s=400&u=af77d15517566ea590a316030b4a6d402b0041b6&v=4" width="100" height="100"> | New Zealand | python | [Github](https://github.com/jameszu) |
417421

418422
<br/>
419423
<div align="right">

0 commit comments

Comments
 (0)