zoukankan      html  css  js  c++  java
  • LeetCode 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 integer.

    好奇怪这题居然没有记录,不过确实再做跟没做过一样,完全想不出来。。。

    class Cmp {
    public:
        bool operator() (const string& a, const string& b) {
            return cmp(a + b, b + a, a.size() + b.size());
        }
        
        bool cmp(const string a, const string b, int len) {
            int pos = 0;
            while (pos < len) {
                if (a[pos] != b[pos]) {
                    return a[pos] < b[pos];
                }
                pos++;
            }
            return false;        
        }
    };
    class Solution {
    public:
        string largestNumber(vector<int>& nums) {
            vector<string> strnums;
            for (int num : nums) {
                strnums.push_back(num2str(num));
            }
            
            sort(strnums.begin(), strnums.end(), Cmp());
            
            string res;
            int len = strnums.size();
            for (int i=len - 1; i>=0; i--) {
                if (res.size() == 0 && strnums[i][0] == '0') {
                    continue;
                }
                res = res + strnums[i];
            }
            if (res.size() == 0) {
                res = "0";
            }
            return res;
        }
        
        string num2str(int num) {
            char buf[20] = {0};
            sprintf(buf, "%d", num);
            return string(buf);
        }
    };
    
  • 相关阅读:
    持续集成概念
    性能测试,负载测试,压力测试有什么区别
    安全测试
    接口测试及常用接口测试工具
    python-Csv 实战
    Python3 + Appium学习链接
    python-Txt实践
    python-ddt实践
    保险--总结
    selenium与页面的交互
  • 原文地址:https://www.cnblogs.com/lailailai/p/4625517.html
Copyright © 2011-2022 走看看