当前位置:网站首页>给出一个数组,如 [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};
执行结果
边栏推荐
- Network security - phishing
- Gee (III): calculate the correlation coefficient between two bands and the corresponding p value
- Ros2 topic (03): the difference between ros1 and ros2 [01]
- Kubernetes' simplified data storage storageclass (creation, deletion and initial use)
- LDO稳压芯片-内部框图及选型参数
- Byte hexadecimal binary understanding
- 1. Sum of two numbers
- Binary tree
- Wechat forum exchange applet system graduation design completion (7) Interim inspection report
- 十四、数据库的导出和导入的两种方法
猜你喜欢
微信论坛交流小程序系统毕业设计毕设(5)任务书
Lecture 30 linear algebra Lecture 5 eigenvalues and eigenvectors
PMP项目管理考试过关口诀-1
ROS2专题(03):ROS1和ROS2的区别【01】
Wechat forum exchange applet system graduation design (3) background function
微信论坛交流小程序系统毕业设计毕设(7)中期检查报告
USB(十五)2022-04-14
聊聊支付流程的设计与实现逻辑
PCI-Express接口的PCB布线规则
Installing spss25
随机推荐
【微服务|SCG】gateway整合sentinel
Puce à tension stabilisée LDO - schéma de bloc interne et paramètres de sélection du modèle
Matlab-SEIR传染病模型预测
微信论坛交流小程序系统毕业设计毕设(7)中期检查报告
Two kinds of curves in embedded audio development
ArcGIS:字段赋值_属性表字段计算器(Field Calculator)依据条件为字段赋值
Oracle-数据库的备份与恢复
海内外技术人们“看”音视频技术的未来
Advantages and disadvantages of rest ful API
Statistical method for anomaly detection
Grid
深入理解Mysql锁与事务隔离级别
微信论坛交流小程序系统毕业设计毕设(8)毕业设计论文模板
ArcGIS:矢量要素相同字段属性融合的两种方法
Adrnoid Development Series (XXV): create various types of dialog boxes using alertdialog
Installing spss25
V20变频器手自动切换(就地远程切换)的具体方法示例
力扣解法汇总648-单词替换
微信论坛交流小程序系统毕业设计毕设(4)开题报告
网络安全-永恒之蓝