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.

问题:给定一个数组,求将数组元素拼凑成一个值最大的整数。

将整型数组的转化为字符串数组,用 std::sort 对字符串数组排序,使得排序后元素直接连接成为最大值整数。

使用默认的比较函数排序,会得到不一定是最大值,例如 {360, 36} 排序后得到的是 36036 ,明显不是小于 36360。解决这个问题,就是重写比较函数,使得排序后的两个元素连接成为较大的整数即可,很简洁。

     int static comp(string s1, string s2){
return (s1 + s2) > (s2 + s1);
} string largestNumber(vector<int>& nums) {
vector<string> numsStr;
for (int i = ; i < nums.size(); i++) {
numsStr.push_back(to_string(nums[i]));
} std::sort(numsStr.begin(), numsStr.end(), comp); string res = "";
for (int i = ; i < numsStr.size(); i++) {
res += numsStr[i];
} int i = ;
while (i < res.size()- && res[i] == '') {
i++;
}
res = res.substr(i); return res;
}

[LeetCode] 179. Largest Number 解题思路的更多相关文章

  1. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  2. leetcode 179. Largest Number 、剑指offer33 把数组排成最小的数

    这两个题几乎是一样的,只是leetcode的题是排成最大的数,剑指的题是排成最小的 179. Largest Number a.需要将数组的数转换成字符串,然后再根据大小排序,这里使用to_strin ...

  3. Java for LeetCode 179 Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  4. [LeetCode] 179. Largest Number 最大组合数

    Given a list of non negative integers, arrange them such that they form the largest number. Example ...

  5. Java 特定规则排序-LeetCode 179 Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  6. JavaScript中sort方法的一个坑(leetcode 179. Largest Number)

    在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...

  7. [leetcode]179. Largest Number最大数

    Given a list of non negative integers, arrange them such that they form the largest number. Input: [ ...

  8. leetcode 179. Largest Number 求最大组合数 ---------- java

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  9. LeetCode 179 Largest Number 把数组排成最大的数

    Given a list of non negative integers, arrange them such that they form the largest number.For examp ...

随机推荐

  1. Neutron中的Service类

    Service是OpenStack中非常重要的一个概念,各个服务的组件都以Service类的方式来进行交互. Neutron中的Service类继承自rpc中的Service,总体的继承关系为 neu ...

  2. [week4]每周总结与工作计划

    计算机网络 TAT 小白dp 28号还有一场 背单词 背马克思 python目标80% 熟悉coursera c++模版和 仿函数 人文修养 开学数据库,itercast的sql*2 itercast ...

  3. Com原理及應用——Com對象和接口

    1.COM对象的理解 COM对象类似于C++语言中类的概念,类的每个实例代表一个COM对象,它也包括属性(即状态)和方法(即操作),状态反映对象的存在,方法就是接口. 2.COM对象的标识-CLSID ...

  4. leetCode 48.Rotate Image (旋转图像) 解题思路和方法

    Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...

  5. cocos2d-x CCAction:动作(转)

    透明度变化的功能挺不错.   瞬时动作 瞬时动作不需要时间,立即完成 [cpp]   //放置,=setPosition()   pRole->runAction(CCPlace::create ...

  6. Coding Your Life

    前几天看到篇文章,写的是科技让人变得陌生,balabala,总的说来就科技让邻居是男是女不知道了,朋友见面少了之类的.其实我觉得,也不能全怪科技发展的太快,而是人心都飘到网路上了,像我这一辈已经老去的 ...

  7. 汉字转拼音(pinyin4j-2.5.0.jar)

    import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCase ...

  8. HTML中<b>标签和<strong>便签的区别

    最近碰到的问题,自己写的时候因为<b>标签比较简短偶尔使用,看到别人有使用<strong>标签的,本人不懂区别,在网上找的别人的东西,觉得很有道理,跟大家分享看看~~ 链接:h ...

  9. springMVC如何判断入参是默认参数还是请求传过来的参数?

    springMVC如何判断入参是默认参数还是请求传过来的参数?

  10. MS SQL Sever数据库还原

    一.右键 数据库 二.点击 [还原文件和文件组(E)...],弹出下图的窗口界面 1.在 目标数据库 的输入框填写你的数据库名(注意这是新建一个数据库供还原使用,不能还原到已有的数据库) 三.点击[源 ...