Skip to content

Commit 88c2bc0

Browse files
committed
update
1 parent 0c82e3e commit 88c2bc0

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

Python/largest-number.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Time: O(n^2)
2+
# Space: O(n)
3+
#
4+
# Given a list of non negative integers, arrange them such that they form the largest number.
5+
#
6+
# For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.
7+
#
8+
# Note: The result may be very large, so you need to return a string instead of an integer.
9+
#
10+
11+
class Solution:
12+
# @param num, a list of integers
13+
# @return a string
14+
def largestNumber(self, num):
15+
largest = ""
16+
none_zero = False
17+
for n in sorted(num, cmp=self.cmp_items):
18+
if n != 0:
19+
none_zero = True
20+
if none_zero:
21+
largest += "{}".format(n)
22+
23+
if largest == "":
24+
largest = "0"
25+
26+
return largest
27+
28+
def cmp_items(self, a, b):
29+
if a == b:
30+
return 0
31+
elif "{}{}".format(a, b) > "{}{}".format(b, a):
32+
return -1
33+
else:
34+
return 1
35+
36+
if __name__ == "__main__":
37+
num = [3, 30, 34, 5, 9]
38+
print Solution().largestNumber(num)

README.md

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

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

99
---
@@ -360,6 +360,7 @@ Problem | Solution | Time | Space | Difficul
360360
--------------- | --------------- | --------------- | --------------- | -------------- | -----
361361
[Insert Interval]| [insert-interval.py] | _O(n)_ | _O(1)_ | Hard |
362362
[Insertion Sort List]|[insertion-sort-list.py] | _O(n^2)_ | _O(1)_ | Medium |
363+
[Largest Number] | [largest-number.py] | _O(n^2)_ | _O(n)_ | Medium |
363364
[Maximum Gap] | [maximum-gap.py]| _O(n)_ | _O(n)_ | Hard | Tricky
364365
[Merge Intervals]| [merge-intervals.py] | _O(n^2)_ | _O(1)_ | Hard |
365366
[Merge Sorted Array]| [merge-sorted-array.py] | _O(n)_ | _O(1)_ | Easy |
@@ -371,6 +372,8 @@ Problem | Solution | Time | Space | Difficul
371372
[insert-interval.py]:https://github.com/kamyu104/LeetCode/blob/master/Python/insert-interval.py
372373
[Insertion Sort List]:https://oj.leetcode.com/problems/insertion-sort-list/
373374
[insertion-sort-list.py]:https://github.com/kamyu104/LeetCode/blob/master/Python/insertion-sort-list.py
375+
[Largest Number]:https://oj.leetcode.com/problems/largest-number/
376+
[largest-number.py]:https://github.com/kamyu104/LeetCode/blob/master/Python/largest-number.py
374377
[Maximum Gap]:https://oj.leetcode.com/problems/maximum-gap/
375378
[maximum-gap.py]:https://github.com/kamyu104/LeetCode/blob/master/Python/maximum-gap.py
376379
[Merge Intervals]:https://oj.leetcode.com/problems/merge-intervals/

0 commit comments

Comments
 (0)