当前位置:网站首页>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
边栏推荐
- Add data analysis tools in Excel
- MySQL Index Optimization Practice I
- sql 数据库执行问题
- Dynamic agent explanation (July 16, 2020)
- USB(十六)2022-04-28
- Two kinds of curves in embedded audio development
- ROS2专题(03):ROS1和ROS2的区别【01】
- Unity3d learning notes 5 - create sub mesh
- [compilation principle] lexical analysis design and Implementation
- 1. Sum of two numbers
猜你喜欢
One week learning summary of STL Standard Template Library
Solution of intelligent supply chain collaboration platform in electronic equipment industry: solve inefficiency and enable digital upgrading of industry
LDO voltage stabilizing chip - internal block diagram and selection parameters
2021ICPC上海 H.Life is a Game Kruskal重构树
Install a new version of idea. Double click it to open it
经纬度PLT文件格式说明
Description of longitude and latitude PLT file format
LM12丨Rolling Heikin Ashi二重K线滤波器
ROS2专题(03):ROS1和ROS2的区别【02】
给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」
随机推荐
UE4_ Ue5 panoramic camera
POJ2392 SpaceElevator [DP]
Inftnews | the wide application of NFT technology and its existing problems
Solution: prompt "unsupported video format" when inserting avi format video into the message
FreeLink开源呼叫中心设计思想
One week learning summary of STL Standard Template Library
B_ QuRT_ User_ Guide(37)
Caip2021 preliminary VP
The efficient s2b2c e-commerce system helps electronic material enterprises improve their adaptability in this way
聊聊支付流程的设计与实现逻辑
Vulnerability recurrence ----- 49. Apache airflow authentication bypass (cve-2020-17526)
七月第一周
Tree background data storage (using webmethod) [easy to understand]
windows设置redis开启自动启动
Freelink open source call center design idea
家用电器行业渠道商协同系统解决方案:助力家电企业快速实现渠道互联网化
Installing spss25
生鲜行业数字化采购管理系统:助力生鲜企业解决采购难题,全程线上化采购执行
LDO稳压芯片-内部框图及选型参数
Explain