Skip to content

Commit 5051980

Browse files
Added python solutions and corrected 128 to medium (#263)
1 parent d2ce5c6 commit 5051980

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ LeetCode
425425
|140|[Word Break II](https://leetcode.com/problems/word-break-ii/)| [C++](./algorithms/cpp/wordBreak/wordBreak.II.cpp)|Hard|
426426
|139|[Word Break](https://leetcode.com/problems/word-break/)| [C++](./algorithms/cpp/wordBreak/wordBreak.cpp)|Medium|
427427
|138|[Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/)| [C++](./algorithms/cpp/copyListWithRandomPointer/copyListWithRandomPointer.cpp), [Python](./algorithms/python/CopyListWithRandomPointer/copyRandomList.py)|Hard|
428-
|137|[Single Number II](https://leetcode.com/problems/single-number-ii/)| [C++](./algorithms/cpp/singleNumber/singleNumber.II.cpp)|Medium|
428+
|137|[Single Number II](https://leetcode.com/problems/single-number-ii/)| [C++](./algorithms/cpp/singleNumber/singleNumber.II.cpp), [Python](./algorithms/python/SingleNumberII/SingleNumberII.py)|Medium|
429429
|136|[Single Number](https://leetcode.com/problems/single-number/)| [C++](./algorithms/cpp/singleNumber/singleNumber.cpp)|Medium|
430430
|135|[Candy](https://leetcode.com/problems/candy/)| [C++](./algorithms/cpp/candy/candy.cpp)|Hard|
431431
|134|[Gas Station](https://leetcode.com/problems/gas-station/)| [C++](./algorithms/cpp/gasStation/gasStation.cpp)|Medium|
@@ -434,7 +434,7 @@ LeetCode
434434
|131|[Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning/)| [C++](./algorithms/cpp/palindromePartitioning/palindromePartitioning.cpp)|Medium|
435435
|130|[Surrounded Regions](https://leetcode.com/problems/surrounded-regions/)| [C++](./algorithms/cpp/surroundedRegions/surroundedRegions.cpp)|Medium|
436436
|129|[Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers/)| [C++](./algorithms/cpp/sumRootToLeafNumber/sumRootToLeafNumber.cpp), [Python](./algorithms/python/SumRootToLeafNumbers/sumNumbers.py)|Medium|
437-
|128|[Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/)| [C++](./algorithms/cpp/longestConsecutiveSequence/longestConsecutiveSequence.cpp)|Hard|
437+
|128|[Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/)| [C++](./algorithms/cpp/longestConsecutiveSequence/longestConsecutiveSequence.cpp), [Python](./algorithms/python/LongestConsecutiveSequence/LongestConsecutive.py)|Medium|
438438
|127|[Word Ladder](https://leetcode.com/problems/word-ladder/)| [C++](./algorithms/cpp/wordLadder/wordLadder.cpp)|Medium|
439439
|126|[Word Ladder II](https://leetcode.com/problems/word-ladder-ii/)| [C++](./algorithms/cpp/wordLadder/wordLadder.II.cpp)|Hard|
440440
|125|[Valid Palindrome](https://leetcode.com/problems/valid-palindrome/)| [C++](./algorithms/cpp/validPalindrome/validPalindrome.cpp), [Java](./algorithms/java/src/validPalindrome/ValidPalindrome.java)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def longestConsecutive(self, nums):
2+
3+
#first, create a hashmap
4+
hm = set(nums)
5+
longest = 0
6+
#find the longest consecutive sequence from each number in the new
7+
#hashmap (set)
8+
for num in hm:
9+
if (num - 1) not in hm:
10+
current = num + 1
11+
cons = 1
12+
while current in hm:
13+
current += 1
14+
cons += 1
15+
if cons > longest:
16+
longest = cons
17+
18+
return longest

Diff for: algorithms/python/SingleNumberII/SingleNumberII.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def singleNumber(self, nums):
2+
3+
#1. make hashmap
4+
#2. while traversing list, add each element
5+
#3. remove it once it has been seen 3 times
6+
#4. finally, the only value left is the one we're looking for
7+
8+
hashmap = defaultdict(int)
9+
for num in nums:
10+
hashmap[num] += 1
11+
if hashmap[num] == 3:
12+
del hashmap[num]
13+
14+
singleNum = hashmap.keys()[0]
15+
return singleNum

0 commit comments

Comments
 (0)