Movatterモバイル変換


[0]ホーム

URL:


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

Commit5051980

Browse files
Added python solutions and corrected 128 to medium (haoel#263)
1 parentd2ce5c6 commit5051980

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

‎README.md

Lines changed: 2 additions & 2 deletions
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|
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
deflongestConsecutive(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+
fornuminhm:
9+
if (num-1)notinhm:
10+
current=num+1
11+
cons=1
12+
whilecurrentinhm:
13+
current+=1
14+
cons+=1
15+
ifcons>longest:
16+
longest=cons
17+
18+
returnlongest
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
defsingleNumber(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+
fornuminnums:
10+
hashmap[num]+=1
11+
ifhashmap[num]==3:
12+
delhashmap[num]
13+
14+
singleNum=hashmap.keys()[0]
15+
returnsingleNum

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp