当前位置:网站首页>Force buckle 179, max
Force buckle 179, max
2022-06-27 06:04:00 【cqwoniu】
One 、 Title Description
Given a set of nonnegative integers nums, Rearrange the order of each number ( Each number cannot be split ) Make it the largest integer .
Be careful : The output can be very large , So you need to return a string instead of an integer .
Two 、 Description of ideas
There is a skill to solve this problem , The sorting conditions are different from normal . When we sort an array , Just compare the two elements ( hypothesis x and y Is any two elements of the array ) The size is ok :
as long as x > y, So if it is in descending order y It should be in x Behind
as long as x < y, So if it is in descending order x It should be in y Behind
For this subject , hypothesis x and y Is any two elements in the array , that x Yes, it needs to be y Is it in front of or behind ? It depends on xy and yx Which is bigger and which is smaller :
If xy > yx, that y Should be in x Behind
If xy < yx, that x Should be in y Behind
such as ,x = 30, y = 3, that xy = 303,yx = 330, because xy < yx , therefore 30 It should be in line 3 Behind , So we can get the maximum value
3、 ... and 、 Code
//179、 The maximum number
public String largestNumber(int[] nums) {
// Sort
quickSort(nums,0,nums.length-1);
if(nums[0]==0)
return "0";
StringBuilder sb = new StringBuilder();
for(int num:nums){
sb.append(num);
}
return sb.toString();
}
public void quickSort(int[] nums, int l, int h) {
if (l >= h)
return;
int pivot = nums[h];
int less = l;
int great = h;
int i = l;
while (i <= great) {
String xy = nums[i] + "" + pivot;
String yx = pivot + "" + nums[i];
if (xy.compareTo(yx) > 0) {
swap(nums, i, less);
less++;
i++;
} else if (xy.compareTo(yx) < 0) {
swap(nums, i, great);
great--;
} else {
i++;
}
}
quickSort(nums,l,less-1);
quickSort(nums,great+1,h);
}
public void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
边栏推荐
- Implementation of easyexcel's function of merging cells with the same content and dynamic title
- My opinion on test team construction
- JVM class loading mechanism
- 下载cuda和cudnn
- NEON优化1:软件性能优化、降功耗怎么搞?
- Go log -uber open source library zap use
- 【Cocos Creator 3.5.1】input.on的使用
- Kubesphere cluster configuration NFS storage solution - favorite
- Code is data
- 项目-h5列表跳转详情,实现后退不刷新,修改数据则刷新的功能(记录滚动条)
猜你喜欢

Codeforces Round #802 (Div. 2)

js实现双向数据绑定

openstack实例重启状态就会变成错误处理方法,容器搭建的openstack重启计算节点compute服务方法,开机提示Give root password for maintenance处理方法

Proxy reflect usage details

Open the door small example to learn ten use case diagrams

Double position relay jdp-1440/dc110v

How to check the frequency of memory and the number of memory slots in CPU-Z?

IAR systems fully supports Centrino technology 9 series chips

LeetCode 0086.分隔链表

Codeforces Round #802 (Div. 2)
随机推荐
Luogu p2939 [usaco09feb]revamping trails G
【QT小作】使用结构体数据生成读写配置文件代码
Neon optimization 1: how to optimize software performance and reduce power consumption?
JVM调优思路
竣达技术丨多品牌精密空调集中监控方案
Two position relay hjws-9440
【Cocos Creator 3.5.1】event. Use of getbutton()
IAR systems fully supports Centrino technology 9 series chips
Codeforces Round #802 (Div. 2)
vscode korofileheader 的配置
[FPGA] design and implementation of frequency division and doubling based on FPGA
NEON优化1:软件性能优化、降功耗怎么搞?
427-二叉树(617.合并二叉树、700.二叉搜索树中的搜索、98. 验证二叉搜索树、530.二叉搜索树的最小绝对差)
Implementation of easyexcel's function of merging cells with the same content and dynamic title
How to check the frequency of memory and the number of memory slots in CPU-Z?
QListWidget中的内容不显示
expect脚本中使用scp命令的方法,expect脚本中scp命令获取不了值的问题完美解决方法
我对于测试团队建设的意见
Two position relay xjls-8g/220
表单校验 v-model 绑定的变量,校验失效的解决方案