【LeetCode】179. Largest Number

Description:

  For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.

Analysis:

  The problem can be solved by sorting. It‘s reallllllllllllllllllllllllllllllllllllllllllly a tallent and brilliant idea to give a compare function like "return s1 + s2 > s2 + s1". Which converts complication to easiness. OMg...

Code:

bool cmp(const string &a, const string &b){
    return a + b > b + a;
}
class Solution {
public:
    string int2String(int n){
        string ret = "";
        if(n == 0) return "0";
        while(n){
            ret += ‘0‘ + (n % 10);
            n /= 10;
        }
        for(int i = 0; i < ret.length() / 2; i ++)
            swap(ret[i], ret[ret.length() - i - 1]);
        return ret;
    }

    string largestNumber(vector<int>& nums) {
        vector<string>rec;
        for(int i = 0; i < nums.size(); i ++){
            string str = int2String(nums[i]);
            rec.push_back(str);
        }
        sort(rec.begin(), rec.end(), cmp);
        string ans = "";
        for(int i = 0; i < rec.size(); i ++){
            ans += rec[i];
        }
        while(ans[0] == ‘0‘ && ans.length() > 1){
            return "0";
        }
        return ans;
    }
};

Using some new features in c++11:

1. Anonymous function: lambda used in cmp(compare function)

2. to_string(int) : returns a string.

3. auto

Learn more about those new features about c++11.

    string largestNumber(vector<int>& nums) {
        vector<string>rec;
        for(auto i : nums){
            rec.push_back(to_string(i));
        }
        sort(rec.begin(), rec.end(), [](const string &s1, const string &s2){ return s1 + s2 > s2 + s1;});
        string ans = "";
        for(auto s : rec){
            ans += s;
        }
        while(ans[0] == ‘0‘ && ans.length() > 1){
            return "0";
        }
        return ans;
    }

  

时间: 2024-10-12 22:36:05

【LeetCode】179. Largest Number的相关文章

【原创】leetCodeOj --- Largest Number 解题报告

原题地址: https://oj.leetcode.com/problems/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. Note: The result may

[leetcode sort]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. Note: The result may be very large, so you need to return a string instead of an i

【LeetCode】009 Palindrome Number

题目:LeetCode 009 Palindrome Number 题意:判断一个整数是否为回文数,不要用额外空间 思路:我不会不用额外空间的方法,需要利用一个长度为20以内的字符串.将整数先写入一个字符串,然后判断首位字符是否相等即可. 代码如下: 1 class Solution { 2 public: 3 bool isPalindrome(int x) { 4 string s = to_string(x); 5 int len = s.size(); 6 for(int i = 0;

【Leetcode】Super Ugly Number

题目链接:https://leetcode.com/problems/super-ugly-number/ 题目: Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8, 13

【leetcode】1090. Largest Values From Labels

题目如下: We have a set of items: the i-th item has value values[i] and label labels[i]. Then, we choose a subset S of these items, such that: |S| <= num_wanted For every label L, the number of items in Swith label L is <= use_limit. Return the largest

【leetcode】1347. Minimum Number of Steps to Make Two Strings Anagram

题目如下: Given two equal-size strings s and t. In one step you can choose any character of t and replace it with another character. Return the minimum number of steps to make t an anagram of s. An Anagram of a string is a string that contains the same c

【LeetCode】84. Largest Rectangle in Histogram——直方图最大面积

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height =[2,1,5,6,2,3]. The largest r

【leetcode】Kth Largest Element in an Array (middle)☆

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example,Given [3,2,1,5,6,4] and k = 2, return 5. 思路: 堆.讲解:二叉堆 class Solution { public: //新插入i结点 其父节点为(i

【LeetCode】268. Missing Number

Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. For example,Given nums = [0, 1, 3] return 2. Note:Your algorithm should run in linear runtime complexity. Could you i