• 剑指Offer——把数组排成最小的数


    1、题目描述

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

    2、代码实现

    package com.baozi.offer;
    
    /**
     * 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。
     * 例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
     *
     * @author BaoZi
     * @create 2019-07-13-22:00
     */
    public class Offer25 {
        public static void main(String[] args) {
            Offer25 offer25 = new Offer25();
            int[] array = new int[]{3, 32, 321};
    //        String[] chars = new String[]{"c", "cb", "cba"};
            String result = offer25.PrintMinNumber(array);
    //        String result1 = offer25.PrintMinNumber(chars);
            System.out.println(result);
    //        System.out.println(result1);
        }
    
        public String PrintMinNumber(String[] numbers) {
            if (numbers.length == 0 || numbers == null) {
                return "";
            }
            int length = numbers.length;
            String[] strs = new String[length];
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < length; i++) {
                strs[i] = numbers[i] + "";
            }
            //重写比较大小的方法
            java.util.Arrays.sort(strs, new java.util.Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    String s1 = o1 + o2;
                    String s2 = o2 + o1;
                    return s1.compareTo(s2);
                }
            });
            for (int i = 0; i < length; i++) {
                sb.append(strs[i]);
            }
            return sb.toString();
        }
    
        public String PrintMinNumber(int[] numbers) {
            if (numbers.length == 0 || numbers == null) {
                return "";
            }
            int length = numbers.length;
            String[] strs = new String[length];
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < length; i++) {
                strs[i] = numbers[i] + "";
            }
            //重写比较大小的方法
            java.util.Arrays.sort(strs, new java.util.Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    String s1 = o1 + o2;
                    String s2 = o2 + o1;
                    return s1.compareTo(s2);
                }
            });
            for (int i = 0; i < length; i++) {
                sb.append(strs[i]);
            }
            return sb.toString();
        }
    }
    

      

  • 相关阅读:
    进入Docker 报错
    Oauth一直无限重定向
    jquery.click() not working in iOS
    IOS 系统 form 表单提交的问题;一直当前页面刷新
    Word 搜索功能显示bug
    百度脑图bug
    微信分享链接添加参数后缩略图不显示如何解决?
    微信分享到朋友圈 QQ空间 代码实现
    文献复现 | The support of human genetic evidence for approved drug indications
    生物医药领域 | 知名投资人 | VC | PE | IBD | 公募 | 私募
  • 原文地址:https://www.cnblogs.com/BaoZiY/p/11182310.html
Copyright © 2020-2023  润新知