当前位置:网站首页>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
边栏推荐
- One week learning summary of STL Standard Template Library
- MySQL Index Optimization Practice II
- 进度播报|广州地铁七号线全线29台盾构机全部完成始发
- UE4_ Ue5 panoramic camera
- UE4_ Use of ue5 blueprint command node (turn on / off screen response log publish full screen display)
- Mysql索引优化实战二
- UE4_UE5结合罗技手柄(F710)使用记录
- Mysql索引优化实战一
- HDU 4747 mex "recommended collection"
- js 获取对象的key和value
猜你喜欢
Wechat forum exchange applet system graduation design completion (6) opening defense ppt
Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
Add data analysis tools in Excel
Matlab-SEIR传染病模型预测
Description of longitude and latitude PLT file format
B_ QuRT_ User_ Guide(36)
漏洞复现----49、Apache Airflow 身份验证绕过 (CVE-2020-17526)
S2b2b mall solution of intelligent supply chain in packaging industry: opening up a new ecosystem of e-commerce consumption
Gee (IV): calculate the correlation between two variables (images) and draw a scatter diagram
给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」
随机推荐
One week learning summary of STL Standard Template Library
2022第六季完美童模陕西总决赛圆满落幕
统计电影票房排名前10的电影并存入还有一个文件
Oracle-数据库的备份与恢复
B_ QuRT_ User_ Guide(37)
【编译原理】词法分析设计实现
在软件工程领域,搞科研的这十年!
USB(十四)2022-04-12
Wechat forum exchange applet system graduation design completion (7) Interim inspection report
v-for遍历对象
Coreseek:第二步建索引及測试
产业共融新势能,城链科技数字峰会厦门站成功举办
Deep understanding of MySQL lock and transaction isolation level
建筑建材行业SRM供应商云协同管理平台解决方案,实现业务应用可扩展可配置
turbo intruder常用脚本
生鲜行业数字化采购管理系统:助力生鲜企业解决采购难题,全程线上化采购执行
PCB wiring rules of PCI Express interface
云原生正在吞噬一切,开发者该如何应对?
做自媒体视频剪辑怎么赚钱呢?
Force deduction solution summary 648 word replacement