当前位置:网站首页>给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」
给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」
2022-07-07 21:52:00 【漁陽】
/** * 给出一个数组,如 [7864, 284, 347, 7732, 8498], * 现在需要将数组中的数字拼接起来,如按顺序依次拼接为:786428434777328498,数组中的数字拼接顺序可以任意, * 编写程序,返回「最大的可能拼出的数字」。(以上面数组为例,返回849878647732347284) * @param arrays * @return */
解题思路:拆解数组中的每一个数,将数值存放到栈中(明显的栈形数据结构);排序(用到了冒泡排序的思路) ,排序过程比较大小通过各数值之间的高位到低位的大小比较,比如8,70,按照这个排序8属于大者;最后将数组从后向前输出即完成拼接
public static String getMaxNum(long[] arrays) {
if (arrays == null || arrays.length == 0) {
return "0";
}
//存放所有拆解后的数据集
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]);
//比较大小
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();
}执行Case数据
long a[] = new long[]{764, 76, 77};
long b[] = new long[]{767, 76, 77};
long c[] = new long[]{7864, 284, 347, 7732, 8498};执行结果

边栏推荐
- 微信论坛交流小程序系统毕业设计毕设(6)开题答辩PPT
- LDO voltage stabilizing chip - internal block diagram and selection parameters
- FreeLink开源呼叫中心设计思想
- Network security - joint query injection
- 13、 System optimization
- The 19th Zhejiang Provincial Collegiate Programming Contest 2022浙江省赛 F.EasyFix 主席树
- Technology at home and abroad people "see" the future of audio and video technology
- Oracle-数据库的备份与恢复
- 智慧社区和智慧城市之间有什么异同
- re1攻防世界逆向
猜你喜欢

Wechat forum exchange applet system graduation design completion (1) development outline

微信论坛交流小程序系统毕业设计毕设(1)开发概要

js 获取对象的key和value

Wechat forum exchange applet system graduation design completion (7) Interim inspection report

Solve the problem of duplicate request resource paths /o2o/shopadmin/o2o/shopadmin/getproductbyid

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

UE4_UE5蓝图command节点的使用(开启关闭屏幕响应-log-发布全屏显示)

【微服务|SCG】gateway整合sentinel

13、 System optimization

LDO voltage stabilizing chip - internal block diagram and selection parameters
随机推荐
Wechat forum exchange applet system graduation design (3) background function
Wechat forum exchange applet system graduation design completion (7) Interim inspection report
USB(十四)2022-04-12
网格(Grid)
Solve the problem of duplicate request resource paths /o2o/shopadmin/o2o/shopadmin/getproductbyid
USB (十八)2022-04-17
Brush question 3
Redhat下安装fedora
OC variable parameter transfer
CXF call reports an error. Could not find conduct initiator for address:
经纬度PLT文件格式说明
为什么市场需要低代码?
微信论坛交流小程序系统毕业设计毕设(3)后台功能
Unity3D学习笔记6——GPU实例化(1)
Two kinds of curves in embedded audio development
ROS2专题(03):ROS1和ROS2的区别【02】
./ setup. Insufficient sh permission
Ros2 topic (03): the difference between ros1 and ros2 [02]
About idea cannot find or load the main class
648. 单词替换