lintcode:组成最大的数
给出一组非负整数,重新排列他们的顺序把他们组成一个最大的整数。
注意事项
最后的结果可能很大,所以我们返回一个字符串来代替这个整数。
样例
给出 [1, 20, 23, 4, 8],返回组合最大的整数应为8423201。
解题
本质上是一种排序,但是排序规则如何定义?
对于20 23 可以组成2023 和2320 显然2320更大应该排在前面
对于5 51 可以组成551 和 515 显然551更大应该排在前面
所以在比较两个数大小的规则应该将两个数链接起来后再比较大小
对于left、right
我们应该比较:leftright 和rightleft 的大小
通过字符串比较如下
public class Solution {
/**
*@param num: A list of non negative integers
*@return: A string
*/
public String largestNumber(int[] num) {
// write your code here
ArrayList<String> list = new ArrayList<String>();
for(int i:num)
list.add(i+"");
Collections.sort(list,new Comparator<String>(){
public int compare(String left,String right){
String leftright = left + right;
String rightleft = right + left;
return leftright.compareTo(rightleft);
}
});
String result="";
for(int i =list.size()-1;i>=0;i--)
result+= list.get(i);
// if(result.equals("00") ||result.equals("0000")||result.equals("00000"))
// return "0";
// 去除左边无效的 0
int i = 0;
while(i< result.length() && result.charAt(i) =='0')
i++;
if(i==result.length())
return "0";
return result.substring(i);
}
}
修改快排的规则
public class Solution {
/**
*@param num: A list of non negative integers
*@return: A string
*/
public String largestNumber(int[] num) {
// write your code here
String result = "";
quickSort(num,0,num.length - 1);
for(int i = 0;i<num.length;i++)
result += num[i];
// 去除左边无效的 0
int i = 0;
while(i< result.length() && result.charAt(i) =='0')
i++;
if(i==result.length())
return "0";
return result.substring(i);
}
// 可以理解为逆序排序,排序后直接链接起来就是答案
public void quickSort(int[] num,int low,int high){
if(low>high)
return;
// for(int kk:num)
// System.out.print(kk+" ");
// System.out.println();
int i= low;
int j= high;
int x = num[i];
while(i<j){
while(i<j && compare(x,num[j]))
j--;
if(i<j){
num[i] = num[j];
i++;
}
while(i<j && compare(num[i],x))
i++;
if(i<j){
num[j] = num[i];
j--;
}
}
num[i] = x;
quickSort(num,low,i-1);
quickSort(num,i+1,high);
}
// AB > BA 说明A应该在前面,B应该在后面
public boolean compare(int A,int B){
String left = A+"";
String right =B+"";
String leftright = left + right;
String rightleft = right + left;
return leftright.compareTo(rightleft) >0;
}
}
lintcode:组成最大的数的更多相关文章
- lintcode 落单的数(位操作)
题目1 落单的数 给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字. 链接:http://www.lintcode.com/zh-cn/problem/single ...
- LintCode之回文数
题目描述: 我的代码: public class Solution { /* * @param num: a positive number * @return: true if it's a pal ...
- [LintCode] Two Sum 两数之和
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- [LintCode] Happy Number 快乐数
Write an algorithm to determine if a number is happy. A happy number is a number defined by the foll ...
- [LintCode] Ugly Number 丑陋数
Write a program to check whether a given number is an ugly number`. Ugly numbers are positive number ...
- [LintCode] 寻找缺失的数
class Solution { public: /** * @param nums: a vector of integers * @return: an integer */ int findMi ...
- [Lintcode two-sum]两数之和(python,双指针)
题目链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 给一个整数数组,找到两个数使得他们的和等于一个给定的数target. 备份一份,然后排序.搞两个 ...
- [LintCode/LeetCode]——两数和、三数和、四数和
LintCode有大部分题目来自LeetCode,但LeetCode比较卡,下面以LintCode为平台,简单介绍我AC的几个题目,并由此引出一些算法基础. 1)两数之和(two-sum) 题目编号: ...
- Lintcode 82.落单的数
-------------------------------- 这道题好坑啊,自己说是2*n+1个数字,结果有组测试数据竟然传了个空数组进来... 经典位算法: n^n==0 n^0==n AC代码 ...
随机推荐
- c中static作用
1. static 变量 静态变量的类型 说明符是static. 静态变量当然是属于静态存储方式,但是属于静态存储方式的量不一定就是静态变量. 例如外部变量虽属于静态 存储方式,但不一定是静态变量 ...
- 网页设计师必备的10个CSS技巧
CSS是网页设计师的基础,对CSS的了解能使他们能够设计出更加美观别致的网页.使用CSS技巧来巧妙地处理CSS是非常令设计师着迷的事情.在CSS的深海世界里有很多有意思的东西,你只需要找到最适合你的就 ...
- Windows平台下Python2.7中pip的安装方法
本文允许转载,转载请保留全文! [请先阅读][说明&总目录]http://www.cnblogs.com/tbcaaa8/p/4415055.html 1. 文件下载 需要下载并运行ez_se ...
- responsive menu
http://responsive-nav.com/#instructions https://github.com/viljamis/responsive-nav.js http://tympanu ...
- c读取文本文档
想数一下文本文档一共有多少行,写了个小程序 1.用fopen()以只读方式打开文件 2.用fgetc()获取文件流中的字符内容 3.如果字符内容为'\n'换行符,count++ 最后输出count的值 ...
- Hibernate学习---第五节:普通组件和动态组件
一.普通组件映射配置 1.创建组件类,代码如下: package learn.hibernate.bean; /** * 组件类 */ public class Phones { private St ...
- throttle/debounce: 为你的cpu减减压(前端性能优化)
何为throttle, 何为debounce? 谷歌翻译给出的意思:throttle 掐死??? debounce 去抖 好吧,按理解我们习惯翻译成 ——节流. 那么在什么场景下需要用到? 场景一 ...
- 【CodeForces】【311C】Fetch the Treasures
最短路 神题一道…… //CF 311C #include<queue> #include<cstdio> #include<cstdlib> #include&l ...
- Poj 1050 分类: Translation Mode 2014-04-04 09:31 103人阅读 评论(0) 收藏
To the Max Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39058 Accepted: 20629 Desc ...
- nsight 使用问题
slot 0 offset 0 stride DXGI_FORMAT_r32b32g32_FLOAT 这样一个memory 100.0000, 100.0000,10.0000,1.0000 stri ...