LeetCode:Largest Number
179. Largest Number
題目描述:
Given a list of non negative integers, arrange them such that they form the largest number.
For example, given [3, 30, 34, 5, 9]
, the largest formed number is
9534330
.
思路:
初步想法是先對陣列進行排序,使得排序後相連線的數字最大。我採用最簡單的氣泡排序,這裡的關鍵是如何比較兩個數,那個應該放在前面(即那個數大)。
比較兩數大小步驟:
1:分別計算整數m,n的前後組合的字串mn和nm。如m= 123, n= 456,則mn= 123456, nm= 456123。
2:比較組合後的mn和nm,那個數大。由於mn和nm有可能太大,超出int的範圍,故,我採用按位比較字串字元大小的方法,從前往後,依次比較把每一位大小。
連線數字成為字串:
1:兩數字轉換為字串,我使用C++中的streanstream。stringstream ss;ss << nums[i];。
2:連線字串,str += ss.str();。
附上程式碼:
class Solution {
public:
bool Comp(int m, int n)
{
stringstream ssm;
ssm << m;
stringstream ssn;
ssn << n;
string smn = ssm.str() + ssn.str();
string snm = ssn.str() + ssm.str();
stringstream ssmn(smn);
stringstream ssnm(snm);
int newmn, newnm;
ssmn >> newmn;
ssnm >> newnm;
int cnt = smn.length();
for(int i = 0; i < cnt; i ++)
{
if(smn[i] > snm[i])
return true;
else if(smn[i] < snm[i])
return false;
}
return true;
}
void BubbleSort(vector<int>& nums)
{
int n = nums.size();
for(int i = n - 1; i > 0; i --)
{
for(int j = 0; j < i; j ++)
{
if(!Comp(nums[j], nums[j + 1]))
{
int tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
}
}
}
}
string largestNumber(vector<int>& nums) {
BubbleSort(nums);
int n = nums.size();
string str = "";
for(int i = 0; i < n; i ++)
{
stringstream ss;
ss << nums[i];
if(str != "" || ss.str() != "0" || i == n - 1)
str += ss.str();
}
return str;
}
};
相關文章
- LeetCode-Largest NumberLeetCode
- LeetCode-Largest BST SubtreeLeetCode
- LeetCode Kth Largest Element in an ArrayLeetCode
- Leetcode - Largest Divisible SubsetLeetCode
- LeetCode-Largest Divisble SubsetLeetCode
- Leetcode Number of islandsLeetCode
- leetcode 368. Largest Divisible SubsetLeetCode
- [LeetCode/LintCode] Largest Palindrome ProductLeetCode
- [LeetCode] Third Maximum NumberLeetCode
- [LeetCode] Find the Duplicate NumberLeetCode
- LeetCode-Strobogrammatic NumberLeetCode
- LeetCode-Number of IslandsLeetCode
- Leetcode Valid NumberLeetCode
- leetcode Palindrome NumberLeetCode
- Leetcode Single NumberLeetCode
- Leetcode 447 Number of BoomerangsLeetCodeOOM
- Leetcode 9 Palindrome NumberLeetCode
- LeetCode-Additive NumberLeetCode
- LeetCode-Strobogrammatic Number IILeetCode
- LeetCode-Super Ugly NumberLeetCode
- LeetCode-Find the Duplicate NumberLeetCode
- LeetCode-Create Maximum NumberLeetCode
- LeetCode-Number of Islands IILeetCode
- [LeetCode] Excel Sheet Column NumberLeetCodeExcel
- Leetcode-Single NumberLeetCode
- Leetcode-Valid NumberLeetCode
- Palindrome Number leetcode javaLeetCodeJava
- Valid Number leetcode javaLeetCodeJava
- Single Number leetcode javaLeetCodeJava
- 【LEETCODE】模擬面試-215. Kth Largest EleLeetCode面試
- python leetcode 215. Kth Largest Element in an ArrayPythonLeetCode
- [leetcode] 1624. Largest Substring Between Two Equal CharactersLeetCode
- [LeetCode] 248. Strobogrammatic Number IIILeetCode
- [LeetCode] 9. Palindrome NumberLeetCode
- Leetcode 933 Number of Recent CallsLeetCode
- 【Leetcode】1395. Count Number of TeamsLeetCode
- LeetCode-Number of Digit OneLeetCodeGit
- LeetCode136:Single NumberLeetCode