当前位置:网站首页>179. 最大数
179. 最大数
2022-08-01 07:44:00 【Mr Gao】
179. 最大数
给定一组非负整数 nums,重新排列每个数的顺序(每个数不可拆分)使之组成一个最大的整数。
注意:输出结果可能非常大,所以你需要返回一个字符串而不是整数。
示例 1:
输入:nums = [10,2]
输出:“210”
示例 2:
输入:nums = [3,30,34,5,9]
输出:“9534330”
解题代码如下:
int f(int num1,int num2){
int a[18];
int b[18];
int sizea=0;
int sizeb=0;
int i;
while(num1){
a[sizea++]=num1%10;
num1=num1/10;
}
if(sizea==0){
sizea=1;
a[0]=0;
}
while(num2){
b[sizeb++]=num2%10;
num2=num2/10;
}
if(sizeb==0){
sizeb=1;
b[0]=0;
}
int sa=sizea,sb=sizeb;
for(i=0;i<sa;i++){
a[sa+sb-i-1]=a[sa-1-i];
// printf("%d ", a[sa+sb-i-1]);
}
for(i=0;i<sb;i++){
a[sb-i-1]=b[sb-1-i];
// printf("%d ", a[sb-i-1]);
}
for(i=0;i<sb;i++){
b[sa+sb-i-1]=b[sb-1-i];
// printf("%d ", b[sa+sb-i-1]);
}
for(i=0;i<sa;i++){
b[sa-i-1]=a[sb+sa-1-i];
}
sizea=sa+sb;
sizeb=sa+sb;
while(sizea!=0&&sizeb!=0){
if(a[sizea-1]>b[sizeb-1]){
return 1;
}
if(a[sizea-1]<b[sizeb-1]){
return -1;
}
sizea--;
sizeb--;
}
if(sizea==sizeb){
return 0;
}
if(sizea>sizeb){
return 1;
}else{
return -1;
}
}
void quick(int *a,int low,int high){
if(low<high){
int l=low,h=high,p=a[low];
while(low<high){
while(low<high&&(f(a[high],p)==-1||f(a[high],p)==0)){
high--;
}
a[low]=a[high];
while(low<high&&(f(a[low],p)==1||f(a[low],p)==0)){
low++;
}
a[high]=a[low];
}
a[low]=p;
quick(a,l,low-1);
quick(a,low+1,h);
}
}
int f2(int *a,int num){
int size=0;
while(num){
a[size++]=num%10;
num=num/10;
}
return size;
}
char * largestNumber(int* nums, int numsSize){
char *re=(char *)malloc(sizeof(char)*1000);
int size=0;
int *a=(int *)malloc(sizeof(int)*9);
quick(nums,0,numsSize-1);
int i;
for( i=0;i<numsSize;i++){
if(nums[i]!=0){
break;
}
// printf("%d ",nums[i]);
}
if(i==numsSize){
re[size++]='0';
re[size]='\0';
return re;
}
for(int i=0;i<numsSize;i++){
int sizearr=f2(a,nums[i]);
for(int j=sizearr;j>0;j--){
re[size++]=a[j-1]+'0';
}
if(sizearr==0){
re[size++]='0';
}
}
re[size]='\0';
return re;
}
边栏推荐
- Data Analysis 6
- C语言学习概览(二)
- 小程序全面屏手势配置案例
- app 自动化 通过工具查看app 元素 (三)
- Generate pictures based on the content of the specified area and share them with a summary
- MATLAB program design and application of MATLAB 2.5
- Vim扩展内容
- 金山打字通 官网 下载
- 22 Grab the Seat 1 C.Grab the Seat (Geometry + Violence)
- How to use Photoshop to composite star trail photos, post-processing method of night sky star trail photos
猜你喜欢

app 自动化 打开app (二)

22牛客多校1 J.Serval and Essay (启发式合并)

如何使用Photoshop合成星轨照片,夜空星轨照片后期处理方法

Three aspects of Ali: How to solve the problem of MQ message loss, duplication and backlog?

special day to remember

图片无损压缩软件哪个好用:试试完全免费的JPG-C 图片批量修整压缩减肥工具吧 | 最新jpg批量修整工具下载

sum of special numbers

VoLTE基础学习系列 | 企业语音网简述

插入排序—直接插入排序和希尔排序

日志导致线程Block的这些坑,你不得不防
随机推荐
VoLTE基础学习系列 | 什么是SIP和IMS中的Forking
22 Niu Ke Duo School 1 I. Chiitoitsu (Probability dp)
升级为重量级锁,锁重入会导致锁释放?
根据指定区域内容生成图片并进行分享总结
pytest接口自动化测试框架 | 集成Allure测试报告
Data Analysis 5
【Unity3D】相机
特殊的日子,值得纪念
Go 支持 OOP: 用 struct 代替 class
mysql查看cpu使用情况
JSON 与 JS 对象的区别
Flink SQL - client, how to deal with the source side and to increase the target, the SQL - client including mapping table and the JOB such as
国内外最顶级的8大plm项目管理系统
app 自动化 打开app (二)
Do I need to introduce any dependencies to write data to clickhouse using flinksql?
VoLTE Basic Learning Series | Enterprise Voice Network Brief
POJ1251丛林之路题解
C语言学习概览(一)
聊一聊ICMP协议以及ping的过程
VSCode插件推荐(Rust环境)