在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given a list of non negative integers, arrange them such that they form the largest number. Example 1: Input: Example 2: Input: Note: The result may be very large, so you need to return a string instead of an integer. 给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数。 示例 1: 输入: 示例 2: 输入: 说明: 输出结果可能非常大,所以你需要返回一个字符串而不是整数。 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 }
|
请发表评论