原题链接: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.

Solution:

  • 易忽略全为0的情况
  • 字符串比较算法
  • tostring()的运用
  • sort函数

代码:

class Solution
{
public:
//比较函数
static bool compare(string s1, string s2)
{
return (s1 + s2) > (s2 + s1);
} string largestNumber(vector<int>& nums)
{
if(nums.empty())
return ""; vector<string> strNums; vector<int>::const_iterator iter = nums.begin();
while (iter != nums.end())
{
//把整形转换成字符串类型
strNums.push_back(to_string((long long)*iter));
iter ++;
} //借用sort函数对转换后的数字字符串排序
sort(strNums.begin(), strNums.end(),compare); string res = "";
vector<string>::const_iterator it = strNums.begin();
while(it != strNums.end())
{
res.append(*it);
it ++;
} //输入的非负整数全部为0的情况下,输出一个0
int index = 0;
while (index < res.length() && res[index] == '0')
index ++; if (index == res.length())
return "0"; 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 解题思路

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

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

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

  4. LeetCode: Largest Number 解题报告 以及Comparator, CompareTo 应用

    Largest Number Given a list of non negative integers, arrange them such that they form the largest n ...

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

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

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

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

  7. Java for LeetCode 179 Largest Number

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

  8. 【原创】leetCodeOj --- Largest Number 解题报告

    原题地址: https://oj.leetcode.com/problems/largest-number/ 题目内容: Given a list of non negative integers, ...

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

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

随机推荐

  1. HDU5972Regular Number(ShiftAnd算法 bitset)

    题意 题目链接 第一行的\(n\)表示模式串长度为\(n\) 接下来\(n\)行,每行开头有一个整数\(num\)表示匹配串中该位置的字符可以在\(num\)个桅子花出现,接下来输入这\(num\)个 ...

  2. SourceTree 跳过登陆

    当前只有Win的版本,Mac自行百度(笑) 很多人用git命令行不熟练,那么可以尝试使用sourcetree进行操作. 然鹅~~sourcetree又一个比较严肃的问题就是,很多人不会跳过注册或者操作 ...

  3. 8.5折!图表控件TeeChart特价中...

    著名图表控件TeeChart去年除了在优势的.NET方面表现依旧出色外,还推出了通过Xamarin和MONO实现的Android,iOS和Mac OSX的跨平台方案,让C#开发者也能开发移动APP. ...

  4. 某地理位置模拟APP从壳流程分析到破解

    工具与环境 Xposed IDA 6.8 JEB 2.2.5 Fiddler2 010Editor NEXUS 5  Android 4.4 好久不玩逆向怕调试器生锈,拿出来磨磨! 高手莫要见笑,仅供 ...

  5. 开源时序服务器influxdb使用

    文档 https://influxdb.com/docs/v0.9/introduction/overview.html 配置文件 /etc/opt/influxdb/influxdb.conf re ...

  6. ansible使用8-Best Practices

    Content Organization production # inventory file for production servers stage # inventory file for s ...

  7. jsp:jsp包含文件的两种方式

    第一种:include指令 include指令:当JSP转换成Servlet时引入指定文件(指令元素),这是一种静态包含,它运行的时候不会单独编译成.class文件,它生成一个新的整体.class文件 ...

  8. 【转载】SQL执行计划

    要理解执行计划,怎么也得先理解,那各种各样的名词吧.鉴于自己还不是很了解.本文打算作为只写懂的,不懂的懂了才写. 在开头要先说明,第一次看执行计划要注意,SQL Server的执行计划是从右向左看的. ...

  9. 如何在VMware ubuntu linux虚拟机中安装VMware tools

    VMware Tools可以实现在主机<->虚拟机之间拷贝文字.文件等功能.本文讲述如何在VMware ubuntu linux虚拟机中安装VMware tools. 测试环境: VMwa ...

  10. leetcode: 数组

    1. longest-consecutive-sequence Given an unsorted array of integers, find the length of the longest ...