From 21adb38ba1b4a3929720d5268ddfac258dfbbdd2 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 12 Apr 2021 14:23:19 +0800 Subject: [PATCH] feat: update leetcode solutions: No.0179. Largest Number --- .../0100-0199/0179.Largest Number/README.md | 24 ++++++++++++- .../0179.Largest Number/README_EN.md | 22 +++++++++++- .../0179.Largest Number/Solution.java | 34 +++++++------------ .../0100-0199/0179.Largest Number/Solution.py | 7 ++++ 4 files changed, 64 insertions(+), 23 deletions(-) create mode 100644 solution/0100-0199/0179.Largest Number/Solution.py diff --git a/solution/0100-0199/0179.Largest Number/README.md b/solution/0100-0199/0179.Largest Number/README.md index bee14c9e39020..6f95bbd494a34 100644 --- a/solution/0100-0199/0179.Largest Number/README.md +++ b/solution/0100-0199/0179.Largest Number/README.md @@ -23,6 +23,8 @@ +先转成字符串列表,再对字符串列表进行字典序降序排列。最后将列表所有字符串拼接即可。 + ### **Python3** @@ -30,7 +32,13 @@ ```python +from functools import cmp_to_key +class Solution: + def largestNumber(self, nums: List[int]) -> str: + num_list = list(map(str, nums)) + num_list.sort(key=cmp_to_key(lambda x, y: int(y + x) - int(x + y))) + return '0' if num_list[0] == '0' else ''.join(num_list) ``` ### **Java** @@ -38,7 +46,21 @@ ```java - +class Solution { + public String largestNumber(int[] nums) { + List numList = new ArrayList<>(); + for (int num : nums) { + numList.add(String.valueOf(num)); + } + numList.sort((a, b) -> (b + a).compareTo(a + b)); + if ("0".equals(numList.get(0))) return "0"; + StringBuilder sb = new StringBuilder(); + for (String s : numList) { + sb.append(s); + } + return sb.toString(); + } +} ``` ### **...** diff --git a/solution/0100-0199/0179.Largest Number/README_EN.md b/solution/0100-0199/0179.Largest Number/README_EN.md index 1c9cc7ab8a89e..af717c3c43174 100644 --- a/solution/0100-0199/0179.Largest Number/README_EN.md +++ b/solution/0100-0199/0179.Largest Number/README_EN.md @@ -33,13 +33,33 @@ ### **Python3** ```python +from functools import cmp_to_key +class Solution: + def largestNumber(self, nums: List[int]) -> str: + num_list = list(map(str, nums)) + num_list.sort(key=cmp_to_key(lambda x, y: int(y + x) - int(x + y))) + return '0' if num_list[0] == '0' else ''.join(num_list) ``` ### **Java** ```java - +class Solution { + public String largestNumber(int[] nums) { + List numList = new ArrayList<>(); + for (int num : nums) { + numList.add(String.valueOf(num)); + } + numList.sort((a, b) -> (b + a).compareTo(a + b)); + if ("0".equals(numList.get(0))) return "0"; + StringBuilder sb = new StringBuilder(); + for (String s : numList) { + sb.append(s); + } + return sb.toString(); + } +} ``` ### **...** diff --git a/solution/0100-0199/0179.Largest Number/Solution.java b/solution/0100-0199/0179.Largest Number/Solution.java index 0cace76942911..73f49dd05bc78 100644 --- a/solution/0100-0199/0179.Largest Number/Solution.java +++ b/solution/0100-0199/0179.Largest Number/Solution.java @@ -1,23 +1,15 @@ -public class Solution { +class Solution { public String largestNumber(int[] nums) { - - String[] strs = new String[nums.length]; - - for (int i = 0; i < strs.length; i++) { - strs[i] = nums[i] + ""; - } - - Arrays.sort(strs, new Comparator() { - - public int compare(String x, String y) { - return (y + x).compareTo(x + y); - } - }); - - if ("0".equals(strs[0])) { - return "0"; - } - - return String.join("", strs); - } + List numList = new ArrayList<>(); + for (int num : nums) { + numList.add(String.valueOf(num)); + } + numList.sort((a, b) -> (b + a).compareTo(a + b)); + if ("0".equals(numList.get(0))) return "0"; + StringBuilder sb = new StringBuilder(); + for (String s : numList) { + sb.append(s); + } + return sb.toString(); + } } \ No newline at end of file diff --git a/solution/0100-0199/0179.Largest Number/Solution.py b/solution/0100-0199/0179.Largest Number/Solution.py new file mode 100644 index 0000000000000..34045d666cce9 --- /dev/null +++ b/solution/0100-0199/0179.Largest Number/Solution.py @@ -0,0 +1,7 @@ +from functools import cmp_to_key + +class Solution: + def largestNumber(self, nums: List[int]) -> str: + num_list = list(map(str, nums)) + num_list.sort(key=cmp_to_key(lambda x, y: int(y + x) - int(x + y))) + return '0' if num_list[0] == '0' else ''.join(num_list)