程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Leetcode Largest Number c++ solution,leetcodelargest

Leetcode Largest Number c++ solution,leetcodelargest

編輯:C++入門知識

Leetcode Largest Number c++ solution,leetcodelargest


Total Accepted: 16020 Total Submissions: 103330

   

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.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

會超過內存限制;把string改成char *就過了,但很難看!

 

    bool comp(const string& a, const string& b) {
        string x = a, y = b;
        int diff = x.length() - y.length();
        if (diff > 0) {
            char c = y[0];
            y.insert(y.end(), diff, c);
        } else if (diff < 0) {
            char c = y[0]; //lastChar(x);
            x.insert(x.end(), -diff, c);
        }
        for (int i = 0; i < x.length(); i++) {
            if (x[i] < y[i])    return false;
            if (x[i] > y[i])    return true;
        }
        return comp(a+b, b+a);
    }

class Solution {
public:
    string largestNumber(vector<int> &num) {
        int len = num.size();
        string* snum = new string[len];
        for (int i = 0; i < len; i++) {
            snum[i] = to_string(num[i]);
        }

        stable_sort(snum, snum + len, comp);
        string ret;
        int i = 0;
        for (; i < len && snum[i] == "0"; i++);
        for (; i < len; i++)
            ret += snum[i];
        return ret.empty() ? "0" : ret;
    }
};

 

把string改成char*

    #define MAX_BUF 10
    bool comp(char* a, char* b) {
        char* x = a,  *y = b;
        char *short_, c[2];
        int diff = strlen(x) - strlen(y);
        short_ = diff > 0 ? y : x;
        sprintf(c, "%c", short_[0]);
        for (int i = 0; i < abs(diff); i++)
            strcat(short_, c);
        int len = strlen(x);
        auto restore = [&]() {
            for (int i = 0; i < abs(diff); i++)
                short_[len - 1 - i] = '\0';
        };
        for (int i = 0; i < len; i++) {
            if (x[i] < y[i])    {
                restore();
                return false;
            }
            if (x[i] > y[i])    {
                restore();
                return true;
            }
        }
        restore();
        if (strlen(x) == strlen(y)) return false;
        char temp1[MAX_BUF*2], temp2[MAX_BUF*2];
        sprintf(temp1, "%s%s", a, b);
        sprintf(temp2, "%s%s", b, a);
        return comp(temp1, temp2);
    }
class Solution {
public:
    string largestNumber(vector<int> &num) {
        int len = num.size();

        char** snum = new char*[len];
        for (int i = 0; i < len; i++) {
            snum[i] = new char[MAX_BUF];
            sprintf(snum[i], "%d", num[i]);
        }
        sort(snum, snum + len, comp);
        string ret;
        int i = 0;
        for (; i < len && strcmp(snum[i],"0") == 0; i++);
        for (; i < len; i++)
            ret += snum[i];
        return ret.empty() ? "0" : ret;
    }
};

 重點來了,用python只需要幾行

class Solution:
    # @param num, a list of integers
    # @return a string
    def largestNumber(self, num):
        num = sorted([str(x) for x in num], cmp = self.compare)
        ans = ''.join(num).lstrip('0')
        return ans or '0'

    def compare(self, a, b):
        return [1, -1][a + b > b + a]

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved