题解 | #把数组排成最小的数#

把数组排成最小的数

https://www.nowcoder.com/practice/8fecd3f8ba334add803bf2a06af1b993?tpId=265&rp=1&ru=%2Fexam%2Foj%2Fta&qru=%2Fexam%2Foj%2Fta&sourceUrl=%2Fexam%2Foj%2Fta%3FjudgeStatus%3D3%26page%3D1%26pageSize%3D50%26search%3D%26tpId%3D13%26type%3D265&difficulty=&judgeStatus=3&tags=&title=&gioEnter=menu

利用了STL,思路易懂,但是如果舍弃STL实现有点难度

class Solution {
  public:
    string PrintMinNumber(vector<int> numbers) {
      std::string res;
      if (numbers.empty()) {
        return res;
      }
      
      std::sort(numbers.begin(), numbers.end(), [](int a, int b) -> bool {return std::to_string(a) + std::to_string(b) < std::to_string(b) + std::to_string(a);});
      
      for (int i = 0; i < numbers.size(); ++i) {
        res += std::to_string(numbers[i]);
      }
      
      return res;
    }
};
全部评论

相关推荐

迟缓的斜杠青年巴比Q了:简历被投过的公司卖出去了,我前两天遇到过更离谱的,打电话来问我有没有意向报班学Java学习,服了,还拿我学校一个学长在他们那报班学了之后干了华为OD当招牌
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务