zoukankan      html  css  js  c++  java
  • JZ032把数组排成最小的数

    把数组排成最小的数

    题目描述

    输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。

    题目链接: 把数组排成最小的数

    代码

    import java.util.Arrays;
    
    /**
     * 标题:把数组排成最小的数
     * 题目描述
     * 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
     * 题目链接:
     * https://www.nowcoder.com/practice/8fecd3f8ba334add803bf2a06af1b993?tpId=13&&tqId=11185&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
     */
    public class Jz32 {
    
        public String printMinNumber(int[] numbers) {
            if (numbers == null || numbers.length == 0) {
                return "";
            }
            int n = numbers.length;
            String[] nums = new String[n];
            for (int i = 0; i < n; i++) {
                nums[i] = numbers[i] + "";
            }
            Arrays.sort(nums, (s1, s2) -> (s1 + s2).compareTo(s2 + s1));
            String result = "";
            for (String str : nums) {
                result += str;
            }
            return result;
        }
    
        public static void main(String[] args) {
            Jz32 jz32 = new Jz32();
            int[] numbers = new int[]{3, 32, 321};
            String s = jz32.printMinNumber(numbers);
            System.out.println(s);
        }
    }
    

    【每日寄语】 完美的背后是艰辛与无数风雨的洗礼,宁可脱一层皮也要飞起来,越努力越幸运。

  • 相关阅读:
    spoj freetour II
    hdu5977 Garden of Eden
    bzoj 1047 理想的正方形
    Python学习-22.Python中的函数——type
    Python学习-21.Python的代码注释
    Python学习-20.Python的Urllib模块
    Python学习-19.Python的Http模块
    Python学习-18.Python中的错误处理(三)
    Python学习-17.Python中的错误处理(二)
    Python学习-16.Python中的错误处理
  • 原文地址:https://www.cnblogs.com/kaesar/p/15713669.html
Copyright © 2011-2022 走看看