[LeetCode] 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.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
#include <vector>
#include <iostream>
#include <algorithm>
#include <sstream>
using namespace std; class Solution {
public:
string largestNumber(vector<int> &num) {
struct MyCompared
{
bool operator ()(int lft,int rgt)
{
long unsigned int lidx=,ridx=;
while(lidx!=)
if(lft>=*lidx) lidx*=;
else break;
while(ridx!=)
if(rgt>=*ridx) ridx*=;
else break;
lidx*=;
ridx*=;
return lft*ridx+rgt > rgt*lidx+lft;
}
}myCompared;
if(num.size()==) return "";
sort(num.begin(),num.end(),myCompared);
ostringstream os;
if(num[]==) return "";
for(int i =;i<num.size();i++)
os<<num[i];
return os.str();
}
}; int main()
{
vector<int > num{,};
Solution sol;
cout<<sol.largestNumber(num)<<endl;
return ;
}
[LeetCode] Largest Number 排序的更多相关文章
- [LeetCode] Largest Number 最大组合数
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- LeetCode: Largest Number 解题报告 以及Comparator, CompareTo 应用
Largest Number Given a list of non negative integers, arrange them such that they form the largest n ...
- LeetCode——Largest Number
Description: Given a list of non negative integers, arrange them such that they form the largest num ...
- Leetcode Largest Number c++ solution
Total Accepted: 16020 Total Submissions: 103330 Given a list of non negative integers, arrange t ...
- [LeetCode] Largest Number At Least Twice of Others 至少是其他数字两倍的最大数
In a given integer array nums, there is always exactly one largest element. Find whether the largest ...
- LeetCode() Largest Number
全排列,超时,知道超时,只是想验证一下. class Solution { public: string largestNumber(vector<int>& nums) { so ...
- Java 特定规则排序-LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- LeetCode之“排序”:Largest Number
题目链接 题目要求: Given a list of non negative integers, arrange them such that they form the largest numbe ...
- JavaScript中sort方法的一个坑(leetcode 179. Largest Number)
在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...
随机推荐
- MySQL的备份与恢复理解与备份策略
MySQL的备份主要分为逻辑备份和物理备份 逻辑备份 在MySQL中逻辑备份的最大优点是对各种存储引擎都可以用同样的方法来备份.而物理备份则不同,不同的存储引擎有着不同的备份方法.Mysql中的逻辑备 ...
- CentOS下安装php gd库报错Error: php56w-common conflicts with php-common-5.3.3-48.el6_8.x86_64
因为服务器缺少php gd库,因为系统是centos,就是用yum去安装,一安装就报错如下: [root@iZ28sdxghs2Z ~]# yum install php-gd Loaded plug ...
- kali下将Python2.x切换至Python3.x
注:我是将Python2切换到Python3.6版本的,下面文件夹名,请注意变更. 1.首先在/usr/local/下创建一个Python-3.6 注意文件夹名(根建议据自己安装版本命名)mkdir ...
- 一个batch的数据如何做反向传播
一个batch的数据如何做反向传播 对于一个batch内部的数据,更新权重我们是这样做的: 假如我们有三个数据,第一个数据我们更新一次参数,不过这个更新只是在我们脑子里,实际的参数没有变化,然后使用原 ...
- 721. Accounts Merge
https://leetcode.com/problems/accounts-merge/description/ class UnionFound { public: unordered_map&l ...
- dfs 的全排列
#include <iostream> #include <algorithm> #include <cstdio> #include <string> ...
- TCP/IP网络编程之基于UDP的服务端/客户端
理解UDP 在之前学习TCP的过程中,我们还了解了TCP/IP协议栈.在四层TCP/IP模型中,传输层分为TCP和UDP这两种.数据交换过程可以分为通过TCP套接字完成的TCP方式和通过UDP套接字完 ...
- cakephp中使用 find('count')方法
对于find('count',array('group'=>'user_id')); Model.php中这样描述: /** * Handles the before/after filter ...
- document.domain跨子域
document.domain 用来得到当前网页的域名.比如在地址栏里输入: javascript:alert(document.domain); //www.315ta.com 我们也可以给docu ...
- android 内存说明
MemoryInfo的Field如下 dalvikPrivateDirty: The private dirty pages used by dalvik. dalvikPss :The propor ...