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

Commit b95f9e9

Browse files
authored
179 solved. (#68)
1 parent 0b681a0 commit b95f9e9

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ continually updating 😃.
2323
* [121. Best Time to Buy and Sell Stock](src/0121_best_time_to_buy_and_sell_stock/maxprofit.go)   *`dynamic programming;`*  *`array`*
2424
* [122. Best Time to Buy and Sell Stock II](src/0122_best_time_to_buy_and_sell_stock_2/maxprofit.go)   *`greedy;`*  *`array`*
2525
* [167. Two Sum II - Input array is sorted](./src/0167_two_sum2/two_sum2.go)   *`double index;`*  *`binary search`*
26+
* [179. Largest Number](src/0179_largest_number/ln.go)   *`sort`*
2627
* [200. Number of Islands](src/0200_number_of_island/number_of_island.go)   *`dfs;`*  *`bfs`*
2728
* [209. Minimum Size Subarray Sum](./src/0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)   *`sliding window`*
2829
* [215. Kth Largest Element in an Array](./src/0215_kth_largest_element_in_an_array/kthleiaa.go)   *`sort`*

‎src/0179_largest_number/ln.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
179. Largest Number
3+
https://leetcode.com/problems/largest-number/
4+
5+
Given a list of non negative integers, arrange them such that they form the largest number.
6+
Note: The result may be very large, so you need to return a string instead of an integer.
7+
*/
8+
// time: 2019年01月14日
9+
10+
package ln
11+
12+
import (
13+
"sort"
14+
"strconv"
15+
"strings"
16+
)
17+
18+
type sliceString []string
19+
20+
// Len is the number of elements in the collection.
21+
func (s sliceString) Len() int { return len(s) }
22+
23+
// Swap swaps the elements with indexes i and j.
24+
func (s sliceString) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
25+
26+
// Less reports whether the element with
27+
// index i should sort before the element with index j.
28+
func (s sliceString) Less(i, j int) bool { return (s[i] + s[j]) > (s[j] + s[i]) }
29+
30+
// time complexity: O(n log n), dominated by the complexity of sort.
31+
// space complexity: O(n)
32+
func largestNumber(nums []int) string {
33+
numsString := make([]string, 0)
34+
for _, num := range nums {
35+
numsString = append(numsString, strconv.Itoa(num))
36+
}
37+
38+
sort.Sort(sliceString(numsString))
39+
numStr := strings.Join(numsString, "")
40+
41+
if strings.HasPrefix(numStr, "0") {
42+
return "0"
43+
}
44+
return numStr
45+
}

‎src/0179_largest_number/ln_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package ln
2+
3+
import "testing"
4+
5+
func TestLargestNumber(t *testing.T) {
6+
testCases := [][]int{
7+
{10, 2},
8+
{3, 30, 34, 5, 9},
9+
{0},
10+
}
11+
12+
expected := []string{
13+
"210",
14+
"9534330",
15+
"0",
16+
}
17+
18+
for index, nums := range testCases {
19+
if res := largestNumber(nums); res != expected[index] {
20+
t.Errorf("expected %s, got %s", expected[index], res)
21+
}
22+
}
23+
}

‎src/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
|0155|[155. Min Stack](0155_min_stack/min_stack.go)|Easy|*`stack`*|
6464
|0165|[165. Compare Version Numbers](0165_compare_version_numbers/compare_version_numbers.go)|Medium|*`string`*|
6565
|0167|[Two Sum II - Input array is sorted](./0167_two_sum2/two_sum2.go)|Easy|*`对撞指针(双索引)`*|
66+
|0179|[179. Largest Number](0179_largest_number/ln.go)|Medium|*`sort`*|
6667
|0198|[House Robber](./0198_house_robber/house_robber.go)|Easy|*`memory search;`* *`dynamic programming`*|
6768
|0200|[200. Number of Islands](0200_number_of_island/number_of_island.go)|Medium|*`dfs;`* *`bfs`*|
6869
|0203|[203. Remove Linked List Elements](0203_remove_linked_list_elements/remove_linked_list_elements.go)|Easy|*`linked list`*|

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /