Skip to content

179. Largest Number #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ continually updating 😃.
* [121. Best Time to Buy and Sell Stock](src/0121_best_time_to_buy_and_sell_stock/maxprofit.go)   *`dynamic programming;`*  *`array`*
* [122. Best Time to Buy and Sell Stock II](src/0122_best_time_to_buy_and_sell_stock_2/maxprofit.go)   *`greedy;`*  *`array`*
* [167. Two Sum II - Input array is sorted](./src/0167_two_sum2/two_sum2.go)   *`double index;`*  *`binary search`*
* [179. Largest Number](src/0179_largest_number/ln.go)   *`sort`*
* [200. Number of Islands](src/0200_number_of_island/number_of_island.go)   *`dfs;`*  *`bfs`*
* [209. Minimum Size Subarray Sum](./src/0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)   *`sliding window`*
* [215. Kth Largest Element in an Array](./src/0215_kth_largest_element_in_an_array/kthleiaa.go)   *`sort`*
Expand Down
45 changes: 45 additions & 0 deletions src/0179_largest_number/ln.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
179. Largest Number
https://leetcode.com/problems/largest-number/

Given a list of non negative integers, arrange them such that they form the largest number.
Note: The result may be very large, so you need to return a string instead of an integer.
*/
// time: 2019-01-14

package ln

import (
"sort"
"strconv"
"strings"
)

type sliceString []string

// Len is the number of elements in the collection.
func (s sliceString) Len() int { return len(s) }

// Swap swaps the elements with indexes i and j.
func (s sliceString) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

// Less reports whether the element with
// index i should sort before the element with index j.
func (s sliceString) Less(i, j int) bool { return (s[i] + s[j]) > (s[j] + s[i]) }

// time complexity: O(n log n), dominated by the complexity of sort.
// space complexity: O(n)
func largestNumber(nums []int) string {
numsString := make([]string, 0)
for _, num := range nums {
numsString = append(numsString, strconv.Itoa(num))
}

sort.Sort(sliceString(numsString))
numStr := strings.Join(numsString, "")

if strings.HasPrefix(numStr, "0") {
return "0"
}
return numStr
}
23 changes: 23 additions & 0 deletions src/0179_largest_number/ln_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ln

import "testing"

func TestLargestNumber(t *testing.T) {
testCases := [][]int{
{10, 2},
{3, 30, 34, 5, 9},
{0},
}

expected := []string{
"210",
"9534330",
"0",
}

for index, nums := range testCases {
if res := largestNumber(nums); res != expected[index] {
t.Errorf("expected %s, got %s", expected[index], res)
}
}
}
1 change: 1 addition & 0 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
|0155|[155. Min Stack](0155_min_stack/min_stack.go)|Easy|*`stack`*|
|0165|[165. Compare Version Numbers](0165_compare_version_numbers/compare_version_numbers.go)|Medium|*`string`*|
|0167|[Two Sum II - Input array is sorted](./0167_two_sum2/two_sum2.go)|Easy|*`对撞指针(双索引)`*|
|0179|[179. Largest Number](0179_largest_number/ln.go)|Medium|*`sort`*|
|0198|[House Robber](./0198_house_robber/house_robber.go)|Easy|*`memory search;`* *`dynamic programming`*|
|0200|[200. Number of Islands](0200_number_of_island/number_of_island.go)|Medium|*`dfs;`* *`bfs`*|
|0203|[203. Remove Linked List Elements](0203_remove_linked_list_elements/remove_linked_list_elements.go)|Easy|*`linked list`*|
Expand Down