原题链接: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. Angularjs ui router,路由嵌套 父controller执行问题

    解决方式来源:https://stackoverflow.com/questions/25316591/angularjs-ui-router-state-reload-child-state-onl ...

  2. SQL DATEDIFF语法及时间函数 Sql 查询当天、本周、本月记录

    SQL DATEDIFF语法及时间函数 Sql 查询当天.本周.本月记录 转:http://blog.csdn.net/Json1204/article/details/7863801?locatio ...

  3. synchronized + volatile + ThreadLocal

    线程的共享 synchronized  +  volatile + ThreadLocal <1> synchronized 锁住的是对象,当用它来锁住一个类时,实际上也是锁的一个对象. ...

  4. Controller的使用

  5. 从零开始的全栈工程师——js篇2.19(BOM)

    一.BOM 浏览器对象模型 1.window.open(url,ways) url 是打开的网页地址ways 打开的方式 _self 2.window.close() 关闭当前页面 3.window. ...

  6. tp3.2水印上传文件

    <html> <html lang="en"><head>    <meta charset="UTF-8">  ...

  7. 【起航计划 025】2015 起航计划 Android APIDemo的魔鬼步伐 24 App->Notification->Notifying Service Controller service中使用Notification

    这个例子介绍了如何在Service中使用Notification,相关的类为NotifyingController和NotifyingService. 在Service中使用Notification的 ...

  8. [RDLC]心得整理(一)

    2014年在做项目的时候, 过用过RDLC, 之后便在没有使用过了. 最近又有项目使用rdlc, 感觉有些陌生,然后重新阅读了以前的笔记,想做一下整理. 常见问题: 1. 为什么rdlc报表出来的pd ...

  9. 然之协同系统3.5(OA+CRM+CASH+TEAM)

    平台: Ubuntu 类型: 虚拟机镜像 软件包: mariadb-server 10.0.25 nginx 1.10.0 php7.0.4 collaboration commercial crm ...

  10. Java集合集锦

    1.介绍Collection框架的结构 集合是Java中的一个非常重要的一个知识点,主要分为List.Set.Map.Queue三大数据结构.它们在Java中的结构关系如下: Collection接口 ...