[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码位点)来排,如果要根据大小排序,需要传入一个 ...
随机推荐
- shell基础及变量符号
kernel主要的功能: 1.内存的管理 2.设备驱动程序 3.文件系统的管理 4.进程的管理 5.网络系统 vim /etc/profile.d/ profile(主配置文件) .d(子配置文件 ...
- 【Ecshop】修改处理用户购物车的行为
Ecshop v2.7.3的购物车处理方面在现在看来有比较反用户体验的设计: 用户未登录时加入购物车的商品,在用户登录后会被清空而不是加入到登录用户的购物车中: 用户登录后加入购物车的商品,在退出后会 ...
- bs4的简单应用之防止xss攻击和文本截断
BeautifulSoup可以过滤html标签,根据这个功能我们可以防止xss攻击和进行文本过滤 1. 安装 pip install beautifulsoup4 2.导入.使用 from bs4 i ...
- POJ3436------ACM Computer Factory
题目链接 ACM Computer Factory Description As you know, all the computers used for ACM contests must be i ...
- unity安装记录
官网直接下载(http://unity3d.com/cn/) 下载完后,会要求输入账号密码.我是先在官网进行一个账号注册,然后登陆即可. 首先会要你输入一个已有的邮箱,进行认证.进入自己邮箱,点击链接 ...
- 《鸟哥的Linux私房菜》学习笔记(0)——磁盘与文件系统管理
一.Linux的登陆流程 login: 用户名:每个用户名都有一个用户ID(用户标识符),计算机处理的就是用户ID(数字)而不是用户名(字符),. 认证机制:Authentication,比如密码或者 ...
- Cakephp在Controller中显示sql语句
Cakephp在Controller中查询语句一般是: $this->Model->find(); 那么这条语句对应的sql语句是什么呢? 可以通过下面方法显示: 1. $dbo = Co ...
- laravel5.2总结--路由
1 基本路由 1.1 定义路由的文件 app/Http/routes.php 1.2 最基本的路由: Route::get(''index", function () { ret ...
- 了解Windows Server以及Hyper-V许可模式
在2015年11月,微软宣布对Windows Server 2016以及Hyper-V的许可模式进行重大变更,并于2016年第三季度正式生效,Windows Server 2016标准版及数据中心版的 ...
- Python+Selenium练习篇之14-获取当前页面的title
前面文章介绍了如何获取当前页面的URL的值,本文介绍如何获取当前页面的title,这个也可以作为测试结果的依据,通过得到的title和预期的值对比,可以支持我们判断页面跳转正确. 相关脚本代码如下: ...