当前位置:网站首页>Sword finger offer 45.61 Sort (simple)
Sword finger offer 45.61 Sort (simple)
2022-06-26 14:14:00 【hedgehog:】
45.
subject :


idea : Custom sort . First convert to a string array , Then arrange the string order by quick sort .
Reference resources : Power button k god

Code :
class Solution {
public String minNumber(int[] nums) {
String[] strs = new String[nums.length];
for(int i = 0; i < nums.length; i++)
strs[i] = String.valueOf(nums[i]);
// Quick sort
quickSort(strs, 0, strs.length - 1);
StringBuilder res = new StringBuilder();
for(String s : strs)
res.append(s);
return res.toString();
}
// Quick line up
private String[] quickSort(String[] strs, int low, int high) {
if (low < high) {
// Use datum , Each express train will low-high In two parts
int base = partition(strs, low, high);
quickSort(strs, low, base - 1);
quickSort(strs, base + 1, high);
}
return strs;
}
// Partition , Returns the size boundary
public int partition(String[] strs, int low, int high) {
int base = low;
String baseValue = strs[low];
while (low < high) {
// Compare Increasing
// Be careful to move first high Move back low
while(low < high &&(strs[high] + strs[base]).compareTo(strs[base] + strs[high]) >= 0)
high--;
while(low < high && (strs[low] + strs[base]).compareTo(strs[base] + strs[low]) <= 0)
low++;
// The top two while It also limits low<high, So there won't be low>high
String tmp = strs[low];
strs[low] = strs[high];
strs[high] = tmp;
}
strs[base] = strs[low];
strs[low] = baseValue;
// Return to the new dividing line
return low;
}
}result :

61.
subject :


idea : This question has nothing to do with playing cards , Is whether it can form shunzi , as well as 0 Can replace other cards .
I wrote it : Insert sort first , Secondly, count the spacing of cards gap Quantity and sum 0 The number of , Judge the situation again .
Reference resources k god : Just judge the interval between the largest and smallest cards . The biggest one is the last one , The smallest subscript is joker Number .
Code :
// I :
class Solution {
public boolean isStraight(int[] nums) {
int pre,cur;
// Insertion sort
for(int i=1;i<nums.length;i++){
pre=i-1;
cur=nums[i];
while(pre>=0 && nums[pre]>cur){
nums[pre+1]=nums[pre];
pre--;
}
nums[pre+1]=cur;
}
// Traverse
int zeros=0;
int gaps=0;
if(nums[0]==0)
zeros++;
for(int i=1;i<nums.length;i++){
if(nums[i]==0)
zeros++;
else if(nums[i]==nums[i-1])
return false;
else if(nums[i-1]!=0)
gaps=gaps+nums[i]-nums[i-1]-1;
}
if(gaps<=zeros||gaps==0)
return true;
else
return false;
}
}
// Reference resources :
class Solution {
public boolean isStraight(int[] nums) {
int joker = 0;
Arrays.sort(nums); // Array sorting
for(int i = 0; i < 4; i++) {
if(nums[i] == 0) joker++; // Count the number of size Kings
else if(nums[i] == nums[i + 1]) return false; // If there's a repetition , Return early false
}
return nums[4] - nums[joker] < 5; // Top brand - The smallest card < 5 Then it can form shunzi
}
}result :

边栏推荐
- 虫子 STL string 下 练习题
- Wechat applet SetData dynamic variable value sorting
- Tips for using nexys A7 development board resources
- Zero basics of C language lesson 8: Functions
- Exercises under insect STL string
- GEE——全球人类居住区网格数据 1975-1990-2000-2014
- [cqoi2015] task query system
- Freefilesync folder comparison and synchronization software
- When drawing with origin, capital letter C will appear in the upper left corner of the chart. The removal method is as follows:
- 数学建模经验分享:国赛美赛对比/选题参考/常用技巧
猜你喜欢

ThreadLocal giant pit! Memory leaks are just Pediatrics

常用控件及自定义控件

ArcGIS cannot be opened and displays' because afcore cannot be found ' DLL, solution to 'unable to execute code'

Free machine learning dataset website (6300+ dataset)

9項規定6個嚴禁!教育部、應急管理部聯合印發《校外培訓機構消防安全管理九項規定》

Correlation analysis related knowledge

Use performance to see what the browser is doing

9项规定6个严禁!教育部、应急管理部联合印发《校外培训机构消防安全管理九项规定》

hands-on-data-analysis 第三单元 模型搭建和评估

C language | Consortium
随机推荐
Zero basics of C language lesson 7: break & continue
Jenkins build prompt error: eacces: permission denied
[scoi2016] lucky numbers
Never use redis expired monitoring to implement scheduled tasks!
9项规定6个严禁!教育部、应急管理部联合印发《校外培训机构消防安全管理九项规定》
Calculate the distance between two points (2D, 3D)
Win10 home vs pro vs enterprise vs enterprise LTSC
虫子 内存管理 上
Hard (magnetic) disk (II)
Range of types
Self created notes (unique in the whole network, continuously updated)
虫子 STL string上
Embedded virlog code running process
What is the use of index aliases in ES
GEE——全球人类居住区网格数据 1975-1990-2000-2014
Practice with the topic of bit operation force deduction
MySQL configuration improves data insertion efficiency
Zero basics of C language lesson 8: Functions
Obtain information about hard disk and volume or partition (capacity, ID, volume label name, etc.)
常用控件及自定义控件
https://leetcode-cn.com/problems/ba-shu-zu-pai-cheng-zui-xiao-de-shu-lcof/