[Swift]LeetCode179. 最大數 | Largest Number

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-pircaher-md.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given a list of non negative integers, arrange them such that they form the largest number.git

Example 1:github

Input: 
Output: "[10,2]210"

Example 2:數組

Input: 
Output: "
[3,30,34,5,9]9534330"

Note: The result may be very large, so you need to return a string instead of an integer.微信


給定一組非負整數,從新排列它們的順序使之組成一個最大的整數。app

示例 1:函數

輸入: 
輸出: [10,2]210

示例 2:spa

輸入: 
輸出: [3,30,34,5,9]9534330

說明: 輸出結果可能很是大,因此你須要返回一個字符串而不是整數。code


20msorm

 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 }
相關文章
相關標籤/搜索