Middle-题目120:179. Largest Number

本文介绍了一种将非负整数数组重新排列以形成最大可能整数的方法。通过自定义排序算法,比较任意两个整数连接后的大小来确定最优顺序。

Middle-题目120:179. Largest Number
题目原文:
Given a list of non negative integers, arrange them such that they form the largest number.

For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.

Note: The result may be very large, so you need to return a string instead of an integer.
题目大意:
给出一串由非负数组成的数组,求出连缀而成的最大整数。
注意:这个结果可能非常大,所以返回一个字符串。
题目分析:
观察例子,最大的数的排列顺序分别为9,5,34,3,30,观察到任意两个元素a和b,若a在b的前面则ab连缀的数一定比ba连缀的数大。据此自定义一个排序算法对数组排序并按序连缀即可。
源码:(language:java)

public class Solution {
    private class MyComparator implements Comparator<Integer> {
        public int compare(Integer o1, Integer o2) {
            // TODO Auto-generated method stub
            String s1=o1.toString()+o2.toString();
            String s2=o2.toString()+o1.toString();
            return s2.compareTo(s1);
        }
    }
    public String largestNumber(int[] nums) {
        List<Integer> list = new ArrayList<Integer>();
        for(int num:nums)
            list.add(num);
        Collections.sort(list, new MyComparator());
        String result = "";
        for(Integer i:list)
            result+=String.valueOf(i);
        if(result.startsWith("00"))
            result = "0";
        return result;
    }
}

成绩:
123ms,beats 64.21%,众数123ms,6.42%
Cmershen的碎碎念:
本题的关键在于找到合适的排序规则,一开始想到的是按字典序排列,但不能处理34>3>30的问题,后又想到补全字符串,但太慢。最后发现只要比较连缀结果的大小即可。by the way,lambda表达式咋写?我看到的ac代码大多用的是匿名类。

7-168 Dream to EC-Final 分数 20 作者 吕佳霖 单位 中国计量大学 ICPC Asia East Continent Final, one of the oldest, largest, and most prestigious programming competitions in China, is attracting more and more competitors from all over the country to participate in, including CJLUACMers. Moreover, it's a huge dream for most of them. During these years, the EC-Final was always hosted by Northwestern Polytechnical University (NWPU) in Xi'an City, Shann Xi Province. (Reference: http://m.haiwainet.cn/middle/3542417/2019/1218/content_31684163_1.html) As a competitor of ICPC EC-Final in 2019, Derrick took part in this programming competition and visited Xi'an City. However, Derrick had another dream that would be never come true, which was travelling in the ancient Xi'an City in Tang Dynasty. 微信图片_20210307183647(1).jpg In order to discover the landscape of the ancient Xi'an City, Derrick has already searched enough information about it. Just as Hundreds and Thousands of houses are like a Go game, and twelve streets are like a vegetable garden(百千家似围棋局,十二街如种菜畦)written by Juyi Bai. This poetry vividly demonstrates the structure of the ancient Xi'an City, it consists of several vertical streets and horizontal roads of the same lengths but different widths. Supposed that the shape of whole city is an n×n square, which composed of n vertical streets x 1 ​ , x 2 ​ ,..., x n ​ and n horizontal roads y 1 ​ , y 2 ​ ,..., y n ​ within the same lengths and different widths from centre. Derrick is curious about calculating the area of all intersections, because he wants to calculate the traffic flow accurately at each intersection in the ancient Xi'an City. However, Derrick considers this task is quite easy for you, so Derrick will spin the whole square with 45 degree at first, maybe clockwise or maybe counterclockwise. After that, you are asked to calculate the total area of all intersections for each row and each column. Input Specification: There are multiple test cases. The first line of the input contains an integer T, indicating the number of test cases. For each test case: The first line contains two integers n (1≤n≤1000) and d, indicating the size of the square and the direction of rotating the square. When d=1, it means that the direction of rotation is clockwise. When d=0, it means that the direction of rotation is counterclockwise. The second line contains n integers x 1 ​ , x 2 ​ ,..., x n ​ (0≤x i ​ ≤5×10 4 ), indicating the width of vertical streets at the i-th position from centre. The third line contains n integers y 1 ​ , y 2 ​ ,..., y n ​ (0≤y j ​ ≤5×10 4 ), indicating the width of horizontal roads at the j-th position from centre. It's guaranteed that the sum of n of all test cases will not exceed 2×10 4 . Output Specification: For each case, in the first line output 2n−1 integers separated by a space, indicating the total area of intersections for each row after rotating 45 degree. Similarly, in the second line print 2n−1 integers separated by a space, indicating the total area of intersections for each column after rotating 45 degree. Note: Please, DO NOT output extra spaces at the end of each line, or your answer may be considered incorrect! Sample Input: 1 3 0 1 2 3 3 2 1 Sample Output: 3 8 14 8 3 1 4 10 12 9 Hint: For example, the initial state of 3×3 square, the clockwise state of 3×3 square and the counterclockwise state of 3×3 square are shown as the following, respectively: 微信图片_20210307204029.png 代码长度限制 16 KB Java (javac) 时间限制 2000 ms 内存限制 64 MB Python (python2) 时间限制 2000 ms 内存限制 64 MB Python (python3) 时间限制 2000 ms 内存限制 64 MB 其他编译器 时间限制 1000 ms 内存限制 64 MB 栈限制 8192 KB翻译成中文
07-22
时间限制:1000ms 内存限制:512MB 输入文件名:s.in 输出文件名:s.out 题目描述: 给定一个长度为 N 的只含小写字母的字符串 S,每次操作可以选择一个位置 1 ≤ x ≤ N 和一个小写字母 c,接着把 Sₓ 改为 c。 你需要操作若干次,使得操作结束后,每相邻 K 个字符都不同,求最少的操作次数。保证 1 ≤ K ≤ 13。 输入格式: 第一行一个整数 T,表示测试数据组数。 接下来 T 组测试数据,对于每组测试数据: 第一行两个整数 N, K,分别表示字符串长度和子串的长度。 第二行一个长度为 N 的字符串 S。 输出格式: 对于每组测试数据,输出一行一个整数表示最少的操作次数。 样例输入 1(input1): 5 6 3 abaaba 5 2 hooch 8 3 cherykid 9 3 abbabbaba 9 3 aabbaabba 样例输出 1(output1): 2 1 0 3 4 样例 #1 解释: 对于第一组数据,可以用 2 次操作把 S 改为 abcabc,每相邻三个字符组成的字符串分别是 abc、bca、cab、abc,都满足包含的字符互不相同。 对于第三组数据,请注意可以不进行操作。 数据范围: 对于所有数据,保证 1 ≤ T ≤ 10⁵,1 ≤ ∑N ≤ 10⁶,1 ≤ K ≤ 13,S 中只含有小写字符。 子任务说明: 子任务 1:∑N ≤ 10,K ≤ 10,分值 10 子任务 2:∑N ≤ 100,K ≤ 10,分值 10 子任务 3:∑N ≤ 10³,K ≤ 13,分值 10 子任务 4:∑N ≤ 10⁵,K ≤ 3,分值 20 子任务 5:∑N ≤ 10⁵,K ≤ 13,分值 20 子任务 6:∑N ≤ 10⁶,K ≤ 13,分值 30 用C++ 14 With O2的语言写一段代码,不要超时,不要注释,用万能头,变量名用一个字母表示,最终代码写好后随便写5组边缘样例测试一下刚刚写的代码(把代码放进编译器输出检查),然后根据刚刚的代码测试修改代码,将这两段代码里的变量名用一个字母替代。代码加上空格,使代码有可读性(实在拿不到全分,也可以拿部分分,尽量拿!)注意加上文件读写!!!!!!!!!!
07-12
内容概要:本文介绍了一个基于冠豪猪优化算法(CPO)的无人机三维路径规划项目,利用Python实现了在复杂三维环境中为无人机规划安全、高效、低能耗飞行路径的完整解决方案。项目涵盖空间环境建模、无人机动力学约束、路径编码、目标代价函数设计以及CPO算法的核心实现。通过体素网格建模、动态障碍物处理、路径平滑技术和约束融合机制,系统能够在高维、密集障碍环境下快速搜索出满足飞行可行性、安全性与能效最优的路径,并支持在线重规划以适应动态环境变化。文中还提供了关键模块的代码示例,包括环境建模、路径评估和CPO优化流程。; 适合人群:具备一定Python编程基础和优化算法基础知识,从事无人机、智能机器人、路径规划或智能优化算法研究的相关科研人员与工程技术人员,尤其适合研究生及有一定工作经验的研发工程师。; 使用场景及目标:①应用于复杂三维环境下的无人机自主导航与避障;②研究智能优化算法(如CPO)在路径规划中的实际部署与性能优化;③实现目标(路径最短、能耗最低、安全性最高)耦合条件下的工程化路径求解;④构建可扩展的智能无人系统决策框架。; 阅读建议:建议结合文中模型架构与代码示例进行实践运行,重点关注目标函数设计、CPO算法改进策略与约束处理机制,宜在仿真环境中测试不同场景以深入理解算法行为与系统鲁棒性。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值