[Swift]LeetCode179. 最大数 | Largest Number
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10151582.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given a list of non negative integers, arrange them such that they form the largest number.
Example 1:
Input:[10,2]
Output: "210"
Example 2:
Input:[3,30,34,5,9]
Output: "9534330"
Note: The result may be very large, so you need to return a string instead of an integer.
给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数。
示例 1:
输入:[10,2]
输出:210
示例 2:
输入:[3,30,34,5,9]
输出:9534330
说明: 输出结果可能非常大,所以你需要返回一个字符串而不是整数。
20ms
1 class Solution { 2 func largestNumber(_ nums: [Int]) -> String { 3 var nums = nums 4 var res:String = String() 5 nums.sort(by:{(num1:Int,num2:Int) -> Bool in 6 return String(num1) + String(num2) > String(num2) + String(num1) 7 }) 8 9 for i in 0..<nums.count 10 { 11 res += String(nums[i]) 12 } 13 return res[0] == "0" ? "0" : res 14 } 15 } 16 17 extension String { 18 //subscript函数可以检索数组中的值 19 //直接按照索引方式截取指定索引的字符 20 subscript (_ i: Int) -> Character { 21 //读取字符 22 get {return self[index(startIndex, offsetBy: i)]} 23 } 24 }
20ms
1 class Solution { 2 func largestNumber(_ nums: [Int]) -> String { 3 var arr = nums.compactMap{ return String($0) } 4 arr.sort{ return $0 + $1 > $1 + $0 } 5 while arr.count > 1 && arr[0] == "0" { 6 arr.removeFirst() 7 } 8 return arr.joined(separator: "") 9 } 10 }
24ms
1 class Solution { 2 func largestNumber(_ nums: [Int]) -> String { 3 let s = nums.map({ (i) -> String in 4 "\(i)" 5 }).sorted { (i, j) -> Bool in 6 (i + j) > (j + i) 7 } 8 if s.count == 0 || s[0] == "0" { 9 return "0" 10 } 11 return s.reduce("") { (res, t) -> String in 12 res + t 13 } 14 } 15 }
28ms
1 class Solution { 2 func largestNumber(_ nums: [Int]) -> String { 3 let strs = nums.map{"\($0)"}.sorted { (str1, str2) -> Bool in 4 return str1 + str2 > str2 + str1 5 } 6 let s = strs.joined() 7 if(s.first != "0"){ return s } 8 return "0" 9 } 10 }
28ms
1 class Solution { 2 func largestNumber(_ nums: [Int]) -> String { 3 var numsPermuation : [String] = [] 4 5 for i in 0..<nums.count { 6 numsPermuation.append(String(nums[i])) 7 } 8 9 var result = numsPermuation.map{$0}.sorted(by: {$0 + $1 > $1 + $0}).joined() 10 11 return result.first == "0" ? "0" : result 12 13 } 14 }
32ms
1 class Solution { 2 func largestNumber(_ nums: [Int]) -> String { 3 var nums = nums 4 nums.sort{ String($0) + String($1) > String($1) + String($0) } 5 var result = "" 6 var flag = false 7 for i in nums{ 8 if i != 0 { 9 flag = true 10 } 11 if flag { 12 result = result + String(i) 13 } 14 } 15 if result.count == 0 { 16 result = "0" 17 } 18 return result 19 } 20 }
32ms
1 class Solution { 2 func largestNumber(_ nums: [Int]) -> String { 3 4 var res = nums.map{String($0)}.sorted{ return $0 + $1 > $1 + $0 }.joined() 5 return res.first == "0" ? "0" : res 6 } 7 }
48ms
1 class Solution { 2 func largestNumber(_ nums: [Int]) -> String { 3 guard nums.count > 0 else { 4 return "" 5 } 6 7 var n = nums.map({ String($0) }) 8 9 n.sort { (n1, n2) -> Bool in 10 let s1 = Int(n1 + n2)! 11 let s2 = Int(n2 + n1)! 12 return s1 > s2 13 } 14 15 16 let result = n.joined() 17 18 19 return n[0] == "0" ? "0" : result 20 } 21 }
48ms
1 class Solution { 2 func largestNumber(_ nums: [Int]) -> String { 3 let array = nums.sorted { return "\($0)\($1)" > "\($1)\($0)" } 4 var result = "" 5 if let firstNum = array.first, firstNum == 0 { 6 return "0" 7 } 8 for num in array { 9 result += "\(num)" 10 } 11 return result 12 } 13 }