当前位置:网站首页>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
边栏推荐
- 高效的S2B2C电商系统,是这样帮助电子材料企业提升应变能力的
- 移动端异构运算技术 - GPU OpenCL 编程(基础篇)
- 树后台数据存储(採用webmethod)[通俗易懂]
- ROS2专题(03):ROS1和ROS2的区别【01】
- Spark 离线开发框架设计与实现
- S2b2b mall solution of intelligent supply chain in packaging industry: opening up a new ecosystem of e-commerce consumption
- v-for遍历对象
- Technology at home and abroad people "see" the future of audio and video technology
- 2021icpc Shanghai h.life is a game Kruskal reconstruction tree
- Three questions TDM
猜你喜欢
ROS2专题(03):ROS1和ROS2的区别【01】
Wechat forum exchange applet system graduation design completion (8) graduation design thesis template
Puce à tension stabilisée LDO - schéma de bloc interne et paramètres de sélection du modèle
USB (XV) 2022-04-14
Ros2 topic (03): the difference between ros1 and ros2 [01]
城联优品作为新力量初注入,相关上市公司股价应声上涨150%
Explain
Mysql索引优化实战二
LeeCode -- 6. Z 字形变换
Deep understanding of MySQL lock and transaction isolation level
随机推荐
USB(十五)2022-04-14
VS扩展工具笔记
LM12丨Rolling Heikin Ashi二重K线滤波器
电子设备行业智能供应链协同平台解决方案:解决低效, 赋能产业数字化升级
统计电影票房排名前10的电影并存入还有一个文件
1. Sum of two numbers
深入理解Mysql锁与事务隔离级别
三问TDM
HDU 4747 mex "recommended collection"
Cloud native is devouring everything. How should developers deal with it?
648. Word replacement
Solve the problem of duplicate request resource paths /o2o/shopadmin/o2o/shopadmin/getproductbyid
Wechat forum exchange applet system graduation design completion (8) graduation design thesis template
USB(十六)2022-04-28
海内外技术人们“看”音视频技术的未来
The 19th Zhejiang Provincial Collegiate Programming Contest VP记录+补题
Spark 离线开发框架设计与实现
Install Fedora under RedHat
CAIP2021 初赛VP
Wechat forum exchange applet system graduation design completion (6) opening defense ppt