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

把数组排成最小的数

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;
    }
};
全部评论

相关推荐

04-18 15:58
已编辑
门头沟学院 设计
kaoyu:这一看就不是计算机的,怎么还有个排斥洗碗?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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