当前位置:网站首页>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;
}
边栏推荐
- 请问用flinksql写入数据到clickhouse需要引入什么依赖吗?
- 案例实践 --- Resnet经典卷积神经网络(Mindspore)
- Golang:go模版引擎的使用
- Image lossless compression software which works: try completely free JPG - C image batch finishing compression reduces weight tools | latest JPG batch dressing tools download
- Upgrade to heavyweight lock, lock reentrancy will lead to lock release?
- SaaS安全认证综合指南
- 七夕来袭——属于程序员的浪漫
- pytest接口自动化测试框架 | 使用函数返回值的形式传入参数值
- Chapter 9 of Huawei Deep Learning Course - Convolutional Neural Network and Case Practice
- mysql查看cpu使用情况
猜你喜欢

研发过程中的文档管理与工具

Golang: go open web service

Data Analysis 6

app 自动化 通过工具查看app 元素 (三)

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

Self-made a remote control software - VeryControl

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

【HDLBits 刷题】Circuits(1)Combinational Logic

JVM内存模型之深究模型特征

VSCode 快捷键及通用插件推荐
随机推荐
Vim三种模式
sum of special numbers
Monitor the width and height of the parent element, adapt to the size of the plug-in
22牛客多校1 J.Serval and Essay (启发式合并)
pytest接口自动化测试框架 | 集成Allure测试报告
C语言学习概览(二)
Electromagnetic compatibility introductory tutorial (6) test project
研发过程中的文档管理与工具
配置我的kitty
根据指定区域内容生成图片并进行分享总结
监听父元素宽高,自适应插件大小
第02章 MySQL的数据目录【1.MySQL架构篇】【MySQL高级】
nodetype中值1、2、3分别代表什么意思
pytest interface automation testing framework | pass in parameter values in the form of function return values
日志导致线程Block的这些坑,你不得不防
05-SDRAM: Arbitration
The socket option
Gethostbyname \ getaddrinfo DNS domain name IP address is not safe
Golang: go open web service
mysql查看cpu使用情况