当前位置:网站首页>Given an array, such as [7864, 284, 347, 7732, 8498], now you need to splice the numbers in the array to return the "largest possible number."
Given an array, such as [7864, 284, 347, 7732, 8498], now you need to splice the numbers in the array to return the "largest possible number."
2022-07-07 23:27:00 【Yuyang】
/** * Give an array , Such as [7864, 284, 347, 7732, 8498], * Now you need to splice the numbers in the array , If spliced in sequence, it is :786428434777328498, The number splicing order in the array can be arbitrary , * Programming , return 「 The most likely number to spell 」.( Take the above array as an example , return 849878647732347284) * @param arrays * @return */
Their thinking : Disassemble every number in the array , Store the value on the stack ( Obvious stack data structure ); Sort ( The idea of bubble sorting is used ) , The sorting process compares the size through the size comparison between the high and low values , such as 8,70, Sort by this 8 Belong to the big one ; Finally, the array is output from the back to the front to complete the splicing
public static String getMaxNum(long[] arrays) {
if (arrays == null || arrays.length == 0) {
return "0";
}
// Store all disassembled data sets
Map<Long, Stack<Integer>> numMaps = Maps.newHashMap();
for (long tn : arrays) {
long t = tn;
Stack<Integer> qu = new Stack<>();
do {
qu.push((int) t % 10);
t = t / 10;
} while (t > 0);
numMaps.put(tn, qu);
}
for (int i = 0; i < arrays.length; i++) {
for (int j = 0; j < arrays.length - i - 1; j++) {
Stack<Integer> f = numMaps.get(arrays[j]);
Stack<Integer> s = numMaps.get(arrays[j + 1]);
// Compare the size
int fn = f.size() - 1;
int sn = s.size() - 1;
do {
if (f.get(fn) < s.get(sn)) {
break;
}
if (f.get(fn) > s.get(sn)) {
long tn = arrays[j];
arrays[j] = arrays[j + 1];
arrays[j + 1] = tn;
break;
}
fn--;
sn--;
} while (fn >= 0 && sn >= 0);
if (fn == -1 || sn == -1) {
fn = fn == -1 ? 0 : fn;
sn = sn == -1 ? 0 : sn;
if (f.get(fn) > s.get(sn)) {
long tn = arrays[j];
arrays[j] = arrays[j + 1];
arrays[j + 1] = tn;
}
}
}
}
StringBuffer sb = new StringBuffer();
for (int i = arrays.length - 1; i >= 0; i--)
sb.append(arrays[i]);
return sb.toString();
}perform Case data
long a[] = new long[]{764, 76, 77};
long b[] = new long[]{767, 76, 77};
long c[] = new long[]{7864, 284, 347, 7732, 8498};Execution results

边栏推荐
- HDU 4747 mex "recommended collection"
- USB (XIV) 2022-04-12
- B_QuRT_User_Guide(36)
- 高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏
- USB(十六)2022-04-28
- Conversion between commonsmultipartfile and file
- Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
- 2021icpc Shanghai h.life is a game Kruskal reconstruction tree
- MySQL Index Optimization Practice I
- Freelink open source call center design idea
猜你喜欢

Matlab 信号处理【问答随笔·2】
![MATLAB signal processing [Q & A essays · 2]](/img/be/0baa92767c3abbda9b0bff47cb3a75.png)
MATLAB signal processing [Q & A essays · 2]

Mysql索引优化实战一

Adults have only one main job, but they have to pay a price. I was persuaded to step back by personnel, and I cried all night

成年人只有一份主业是要付出代价的,被人事劝退后,我哭了一整晚

STL标准模板库(Standard Template Library)一周学习总结

UE4_UE5结合罗技手柄(F710)使用记录

USB(十五)2022-04-14

漏洞复现----49、Apache Airflow 身份验证绕过 (CVE-2020-17526)

Unity3d Learning Notes 6 - GPU instantiation (1)
随机推荐
Happy gathering time
The efficient s2b2c e-commerce system helps electronic material enterprises improve their adaptability in this way
移动端异构运算技术 - GPU OpenCL 编程(基础篇)
UE4_UE5全景相机
Talk about the design and implementation logic of payment process
ROS2专题(03):ROS1和ROS2的区别【01】
Count the top 10 films at the box office and save them in another file
B_QuRT_User_Guide(37)
FPGA基础篇目录
B_ QuRT_ User_ Guide(37)
伸展树(一) - 图文解析与C语言实现
做自媒体视频剪辑怎么赚钱呢?
MySQL Index Optimization Practice II
2021icpc Shanghai h.life is a game Kruskal reconstruction tree
UE4_ Ue5 panoramic camera
HDU 4747 Mex「建议收藏」
B_QuRT_User_Guide(36)
Explain
Puce à tension stabilisée LDO - schéma de bloc interne et paramètres de sélection du modèle
Matlab 信号处理【问答随笔·2】