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

小技巧为排序中的比较函数,另外记住不能有=号

class Solution {
public:
static bool compareFunc(string &s1, string& s2) {
return s1+s2 > s2+s1;
}
string largestNumber(vector<int>& nums) {
string res;
int n = nums.size();
vector<string> vecStr;
for (int i = ; i < n; i++) {
vecStr.push_back(to_string(nums[i]));
}
sort(vecStr.begin(), vecStr.end(), compareFunc);
if(vecStr[] == "") return ""; for (int i = ; i < n; i++) {
res += vecStr[i];
}
return res;
}
};

Largest Number的更多相关文章

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

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

  2. [LeetCode] Largest Number 最大组合数

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

  3. 【leetcode】Largest Number

    题目简述: Given a list of non negative integers, arrange them such that they form the largest number. Fo ...

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

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

  5. LeetCode-179. Largest Number

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

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

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

  7. 【leetcode】Largest Number ★

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

  8. Java for LeetCode 179 Largest Number

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

  9. 179. Largest Number -- 数字字符串比较大小

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

随机推荐

  1. 【leetcode❤python】 160. Intersection of Two Linked Lists

    #-*- coding: UTF-8 -*- #两种方法#方法1:#计算出A和B两个链表的长度分别为m.n;#长度长的链表先走m-n步,之后再一次遍历寻找#方法2:#先走到一个链表的尾部,从尾部开始走 ...

  2. 选出N个不重复的随机数

    <script type="text/javascript"> var str="0123456789"; var arr=[]; var n; w ...

  3. 有限状态机(Python)

    有限状态机(Finite-state machine, FSM),又称有限状态自动机,简称状态机,是表示有限个状态以及在这些状态之间的转移和动作等行为的数学模型.FSM是一种算法思想,简单而言,有限状 ...

  4. c# this关键字的理解

    this关键字引用类的当前实例 1/限定被相似的名称隐藏的成员 2/将对象作为参数传递到其他方法 3/声明索引器 实际案例参考: //成员类 public class Employee { priva ...

  5. MSDN for VS2012 的安装

    在VS2012中,由于MSDN默认不内置,VS2008 以上的就没有独立的 MSDN 了 ,而是被 Microsoft Help Viewer 取代了. 该组件包含在 VS2012 的 ISO 安装镜 ...

  6. HDU-4528 小明系列故事——捉迷藏 BFS模拟

    题意:链接 分析:每一个D或者是E点往四面延伸,并且赋一个特殊的值,能看到D点的点赋值为1,能看到E点的点赋值为1000,这是因为最多100步,因此最后可以根据除以1000和对1000取模来得出某个状 ...

  7. 关于JSPatch热修复

    今天和同事聊到JSPatch热修复,我们来看下JSPatch,确实解决了APP升级的痛点. 刚好,已经有这么一个第三方平台实现了后台管理,全套服务(网址是:http://jspatch.com/),先 ...

  8. 20145224&20145238《信息安全系统设计基础》实验三

    20145224陈颢文20145238荆玉茗 <信息安全系统设计基础>第五次实验报告 课程:信息安全系统设计基础 班级: 1452 姓名:荆玉茗 陈颢文 学号:20145238 20145 ...

  9. Clojure学习笔记(二)——函数式编程

    定义 “函数式编程”是一种编程范式(programming paradigm),即如何编写程序的方法论.主要思想是把运算过程尽量写成一系列嵌套的函数调用. 举例来说,现在有这样一个数学表达式: (1 ...

  10. "编写高质量代码"一书笔记

    总结 css架构结构 :       base  :   共用样式   common: 通用控件样式   page: 页面级样式   js 架构结构:   base 位于三层最底层,职责一是封装不同浏 ...