当前位置:网站首页>给出一个数组,如 [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};执行结果

边栏推荐
- UE4_UE5结合罗技手柄(F710)使用记录
- Adrnoid开发系列(二十五):使用AlertDialog创建各种类型的对话框
- Byte hexadecimal binary understanding
- Inftnews | web5 vs Web3: the future is a process, not a destination
- leetcode-520. Detect capital letters -js
- U盘拷贝东西时,报错卷错误,请运行chkdsk
- 微信论坛交流小程序系统毕业设计毕设(6)开题答辩PPT
- RE1 attack and defense world reverse
- LeeCode -- 6. Z 字形变换
- Network security - Eternal Blue
猜你喜欢

LeeCode -- 6. Zigzag transformation

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

V20变频器手自动切换(就地远程切换)的具体方法示例

Wechat forum exchange applet system graduation design completion (4) opening report

Wechat forum exchange applet system graduation design completion (8) graduation design thesis template

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

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

微信论坛交流小程序系统毕业设计毕设(3)后台功能

LDO稳压芯片-内部框图及选型参数

13、 System optimization
随机推荐
UE4_UE5结合罗技手柄(F710)使用记录
PCI-Express接口的PCB布线规则
The text editor of markdown class should add colors to fonts (including typora, CSDN, etc.)
CXF call reports an error. Could not find conduct initiator for address:
Install Fedora under RedHat
Classification and prediction of heartbeat signal
聊聊支付流程的设计与实现逻辑
Add data analysis tools in Excel
Network security sqlmap and DVWA explosion
解决:信息中插入avi格式的视频时,提示“unsupported video format”
JMeter interface automated test read case, execute and write back result
Description of longitude and latitude PLT file format
Solution: prompt "unsupported video format" when inserting avi format video into the message
Grid
经纬度PLT文件格式说明
Network security - phishing
GEE(四):计算两个变量(影像)之间的相关性并绘制散点图
Unity3D学习笔记4——创建Mesh高级接口
在软件工程领域,搞科研的这十年!
JMeter-接口自动化测试读取用例,执行并结果回写