Skip to content

Commit 3cb8ba4

Browse files
committed
update
1 parent 14056ee commit 3cb8ba4

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

Python/excel-sheet-column-title.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Time: O(logn)
2+
# Space: O(1)
3+
#
4+
# Given a positive integer, return its corresponding column title as appear in an Excel sheet.
5+
#
6+
# For example:
7+
#
8+
# 1 -> A
9+
# 2 -> B
10+
# 3 -> C
11+
# ...
12+
# 26 -> Z
13+
# 27 -> AA
14+
# 28 -> AB
15+
#
16+
17+
class Solution:
18+
# @return a string
19+
def convertToTitle(self, num):
20+
result, dvd = "", num
21+
22+
while dvd:
23+
result += chr((dvd - 1) % 26 + ord('A'))
24+
dvd = (dvd - 1) / 26
25+
26+
return result[::-1]
27+
28+
if __name__ == "__main__":
29+
for i in xrange(1, 29):
30+
print Solution().convertToTitle(i)

Python/majority-element.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
#
4+
# Given an array of size n, find the majority element.
5+
# The majority element is the element that appears more than [n/2] times.
6+
#
7+
# You may assume that the array is non-empty and the majority element always exist in the array.
8+
#
9+
10+
class Solution:
11+
# @param num, a list of integers
12+
# @return an integer
13+
def majorityElement(self, num):
14+
idx, cnt = 0, 1
15+
16+
for i in xrange(1, len(num)):
17+
if num[idx] == num[i]:
18+
cnt += 1
19+
else:
20+
cnt -= 1
21+
if cnt == 0:
22+
idx = i
23+
cnt = 1
24+
25+
return num[idx]
26+
27+
if __name__ == "__main__":
28+
print Solution().majorityElement([1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 6])

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
LeetCode
22
========
33

4-
Up to date (2014-12-18), there are total `167` problems on [LeetCode Online Judge](https://oj.leetcode.com/).
4+
Up to date (2014-12-21), there are total `169` problems on [LeetCode Online Judge](https://oj.leetcode.com/).
55
The number of problems is increasing recently.
6-
Here is the classification of all `167` problems.
6+
Here is the classification of all `169` problems.
77
I'll keep updating for full summary and better solutions. Stay tuned for updates.
88

99
---
@@ -53,6 +53,7 @@ Problem | Solution | Time | Space | Difficul
5353
[Best Time to Buy and Sell Stock]| [best-time-to-buy-and-sell-stock.py] | _O(n)_ | _O(1)_ | Medium |
5454
[First Missing Positive]| [first-missing-positive.py] | _O(n)_ | _O(1)_ | Hard | Tricky
5555
[Longest Consecutive Sequence]| [longest-consecutive-sequence.py] | _O(n)_ | _O(n)_ | Hard | Tricky
56+
[Majority Element] | [majority-element.py] | _O(n)_ | _O(1)_ | Easy |
5657
[Missing Ranges]| [missing-ranges.py] | _O(n)_ | _O(1)_ | Medium |
5758
[Next Permutation]| [next-permutation.py] | _O(n)_ | _O(1)_ | Medium | Tricky
5859
[Pascal's Triangle]| [pascals-triangle.py] | _O(n^2)_ | _O(n)_ | Easy |
@@ -79,6 +80,8 @@ Problem | Solution | Time | Space | Difficul
7980
[first-missing-positive.py]:https://github.com/kamyu104/LeetCode/blob/master/Python/first-missing-positive.py
8081
[Longest Consecutive Sequence]:https://oj.leetcode.com/problems/longest-consecutive-sequence/
8182
[longest-consecutive-sequence.py]:https://github.com/kamyu104/LeetCode/blob/master/Python/longest-consecutive-sequence.py
83+
[Majority Element]: https://oj.leetcode.com/problems/majority-element/
84+
[majority-element.py]:https://github.com/kamyu104/LeetCode/blob/master/Python/majority-element.py
8285
[Missing Ranges]:https://oj.leetcode.com/problems/missing-ranges/
8386
[missing-ranges.py]:https://github.com/kamyu104/LeetCode/blob/master/Python/missing-ranges.py
8487
[Next Permutation]:https://oj.leetcode.com/problems/next-permutation/
@@ -298,6 +301,7 @@ Problem | Solution | Time | Space | Difficul
298301
Problem | Solution | Time | Space | Difficulty | Notes
299302
--------------- | --------------- | --------------- | --------------- | -------------- | -----
300303
[Divide Two Integers] | [divide-two-integers.py] | _O(logn)_ | _O(1)_ | Medium |
304+
[Excel Sheet Column Title] | [excel-sheet-column-title.py] | _O(logn)_ | _O(1)_ | Easy |
301305
[Fraction to Recurring Decimal] | [fraction-to-recurring-decimal.py] | _O(logn)_ | _O(1)_ | Medium |
302306
[Gray Code] | [gray-code.py] | _O(2^n)_ | _O(1)_ | Medium |
303307
[Integer to Roman] | [integer-to-roman.py] | _O(n)_ | _O(1)_ | Medium |
@@ -309,6 +313,8 @@ Problem | Solution | Time | Space | Difficul
309313

310314
[Divide Two Integers]:https://oj.leetcode.com/problems/divide-two-integers/
311315
[divide-two-integers.py]:https://github.com/kamyu104/LeetCode/blob/master/Python/divide-two-integers.py
316+
[Excel Sheet Column Title]:https://oj.leetcode.com/problems/excel-sheet-column-title/
317+
[excel-sheet-column-title.py]:https://github.com/kamyu104/LeetCode/blob/master/Python/excel-sheet-column-title.py
312318
[Fraction to Recurring Decimal]:https://oj.leetcode.com/problems/fraction-to-recurring-decimal/
313319
[fraction-to-recurring-decimal.py]:https://github.com/kamyu104/LeetCode/blob/master/Python/fraction-to-recurring-decimal.py
314320
[Gray Code]:https://oj.leetcode.com/problems/gray-code/

0 commit comments

Comments
 (0)