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/LintCode] Largest Palindrome ProductLeetCode
- LeetCode Kth Largest Element in an ArrayLeetCode
- leetcode 368. Largest Divisible SubsetLeetCode
- Leetcode Number of islandsLeetCode
- python leetcode 215. Kth Largest Element in an ArrayPythonLeetCode
- [LeetCode] Third Maximum NumberLeetCode
- [LeetCode] Find the Duplicate NumberLeetCode
- Leetcode 9 Palindrome NumberLeetCode
- Leetcode 447 Number of BoomerangsLeetCodeOOM
- [leetcode] 1624. Largest Substring Between Two Equal CharactersLeetCode
- [LeetCode] 2275. Largest Combination With Bitwise AND Greater Than ZeroLeetCode
- Leetcode 933 Number of Recent CallsLeetCode
- [LeetCode] 9. Palindrome NumberLeetCode
- LeetCode之Fibonacci Number(Kotlin)LeetCodeKotlin
- 【LEETCODE】模擬面試-215. Kth Largest EleLeetCode面試
- 976. Largest Perimeter Triangle(Leetcode每日一題-2020.11.29)LeetCode每日一題
- Leetcode 611 javascript Valid Triangle NumberLeetCodeJavaScript
- LeetCode之Number of Recent Calls(Kotlin)LeetCodeKotlin
- [LeetCode] 248. Strobogrammatic Number IIILeetCode
- [LeetCode] 191. Number of 1 BitsLeetCode
- [LeetCode] 305. Number of Islands IILeetCode
- Leetcode 17 Letter Combinations of a Phone NumberLeetCode
- Leetcode 137. Single Number IILeetCode
- 【Leetcode】1395. Count Number of TeamsLeetCode
- LeetCode Palindrome Number(009)解法總結LeetCode
- Leetcode – 017. Letter Combinations of a Phone NumberLeetCode
- [LeetCode] 3238. Find the Number of Winning PlayersLeetCode
- [LeetCode] 2684. Maximum Number of Moves in a GridLeetCode
- LeetCode C++ 703. Kth Largest Element in a Stream【Heap/Design】簡單LeetCodeC++
- Leetcode 202 Happy Number Javascript 解決方案LeetCodeAPPJavaScript
- [LeetCode] 2406. Divide Intervals Into Minimum Number of GroupsLeetCodeIDE
- LeetCode - 解題筆記 - 8 - Palindrome NumberLeetCode筆記
- LeetCode Letter Combinations of a Phone Number(017)解法總結LeetCode
- Leetcode 之 PHP 解析 (260. Single Number III)LeetCodePHP
- leetcode學習筆記09 palindrome-numberLeetCode筆記
- LeetCode演算法題-Number of Boomerangs(Java實現)LeetCode演算法OOMJava
- LeetCode65. Valid Number — 判斷合法數字LeetCode
- [LeetCode] 1953. Maximum Number of Weeks for Which You Can WorkLeetCode
- [LeetCode] 3226. Number of Bit Changes to Make Two Integers EqualLeetCode