当前位置:网站首页>Leetcode (Sword finger offer) - 45 Arrange the array into the smallest number
Leetcode (Sword finger offer) - 45 Arrange the array into the smallest number
2022-07-16 08:35:00 【Sheep yard】
Topic link : Click to open the link
The main idea of the topic : A little
Their thinking

Related enterprises
- Bytes to beat
- Huawei
- Microsoft (Microsoft)
AC Code
- Java
// Solution (1)
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]);
quickSort(strs, 0, strs.length - 1);
StringBuilder res = new StringBuilder();
for(String s : strs)
res.append(s);
return res.toString();
}
void quickSort(String[] strs, int l, int r) {
if(l >= r) return;
int i = l, j = r;
String tmp = strs[i];
while(i < j) {
while((strs[j] + strs[l]).compareTo(strs[l] + strs[j]) >= 0 && i < j) j--;
while((strs[i] + strs[l]).compareTo(strs[l] + strs[i]) <= 0 && i < j) i++;
tmp = strs[i];
strs[i] = strs[j];
strs[j] = tmp;
}
strs[i] = strs[l];
strs[l] = tmp;
quickSort(strs, l, i - 1);
quickSort(strs, i + 1, r);
}
}
// Solution (2)
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]);
Arrays.sort(strs, (x, y) -> (x + y).compareTo(y + x));
StringBuilder res = new StringBuilder();
for(String s : strs)
res.append(s);
return res.toString();
}
}- C++
// Solution (1)
class Solution {
public:
string minNumber(vector<int>& nums) {
vector<string> strs;
for(int i = 0; i < nums.size(); i++)
strs.push_back(to_string(nums[i]));
quickSort(strs, 0, strs.size() - 1);
string res;
for(string s : strs)
res.append(s);
return res;
}
private:
void quickSort(vector<string>& strs, int l, int r) {
if(l >= r) return;
int i = l, j = r;
while(i < j) {
while(strs[j] + strs[l] >= strs[l] + strs[j] && i < j) j--;
while(strs[i] + strs[l] <= strs[l] + strs[i] && i < j) i++;
swap(strs[i], strs[j]);
}
swap(strs[i], strs[l]);
quickSort(strs, l, i - 1);
quickSort(strs, i + 1, r);
}
};
// Solution (2)
class Solution {
public:
string minNumber(vector<int>& nums) {
vector<string> strs;
string res;
for(int i = 0; i < nums.size(); i++)
strs.push_back(to_string(nums[i]));
sort(strs.begin(), strs.end(), [](string& x, string& y){ return x + y < y + x; });
for(int i = 0; i < strs.size(); i++)
res.append(strs[i]);
return res;
}
};边栏推荐
猜你喜欢
随机推荐
Small program graduation project of wechat enterprise company (7) Interim inspection report
Win11卸载程序在哪里?Win11卸载软件的两种方法
CVPR | 基于密度与深度分解的自增强非成对图像去雾
Catégorisation des chiens et des chats - vgg16 bottleeck
Analysis of thread related methods wait, notify, notifyAll in object
测试基础4
Glue terraform ecology to kubernetes world
After some experiments, the metaphysics of batch size was broken
Redis的持久化机制、过期策略、淘汰策略
Differences and relations between malloc, vmalloc and kmalloc, free, kfree and vfree
集合系列开篇:为什么要学集合?
Pressing the volume key on the prototype can synchronously adjust the call volume of the mobile phone when Jerry is talking [chapter]
2022-07 Microsoft vulnerability announcement
Small program graduation project of wechat enterprise company (3) background function
从全球价值链视角看,京东云数智供应链对未来经济有何影响?
Wechat classroom appointment of applet completion works (1) summary of development
斜堆 - 原理与实现
From the perspective of global value chain, how will JD cloud digital intelligence supply chain affect the future economy?
[daily question 1] judge whether it is a complete binary tree
Teach you how to install CUDA by hand

![Leetcode 735 planetary collision [stack simulation] the leetcode road of heroding](/img/cb/fe68c558b526555cfa6992233407fe.png)







