当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

JVM常用指令

Openresty usage document

Multithreading basic part2

Small program of C language practice (consolidate and deepen the understanding of knowledge points)

Create a basic WDM driver and use MFC to call the driver

Program ape learning Tiktok short video production

汇编语言-王爽 第8章 数据处理的两个基本问题-笔记

创建一个基础WDM驱动,并使用MFC调用驱动

leetcode298周赛记录

cpu-z中如何查看内存的频率和内存插槽的个数?
随机推荐
Spark's projection
Senior [Software Test Engineer] learning route and necessary knowledge points
Luogu p2939 [usaco09feb]revamping trails G
Contents in qlistwidget are not displayed
【QT小记】QT元对象系统简单认识
表单校验 v-model 绑定的变量,校验失效的解决方案
Matlab quickly converts two-dimensional coordinates of images into longitude and latitude coordinates
Altium Designer 19 器件丝印标号位置批量统一摆放
Asp. Net core6 websocket simple case
My opinion on test team construction
Navigation [machine learning]
[FPGA] design and implementation of frequency division and doubling based on FPGA
IP网络通信的单播、组播和广播
【Cocos Creator 3.5.1】this. node. Use of getposition (this.\u curpos)
NEON优化1:软件性能优化、降功耗怎么搞?
Dual position relay dls-34a dc0.5a 220VDC
DAST black box vulnerability scanner part 6: operation (final)
【QT小作】使用结构体数据生成读写配置文件代码
NLP-D62-nlp比赛D31&刷题D15
Create a basic WDM driver and use MFC to call the driver