输入一个字符串输出去重后的最小值

  • 给定一个正整数,给出消除重复数字以后最小的整数,注意需要考虑长整数。
  • 输入示例:423234
  • 输出示例:234
//Ctrl+J你值得拥有
import java.util.Scanner;
import java.util.Arrays;

public class Main {

    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        String string = scanner.nextLine();
        int[] src = {string.charAt(0) - '0'};
        int[] dec = null;
        for(int i=0;i<string.length();i++){
            int find = string.charAt(i) - '0';
            int index = binarySearch(src, find,0, src.length - 1);
            if (index != -1) {
              //  System.out.println("index is :" + index + " value is :" + src[index]);
                continue;
            } else {
                dec = searchInsert(src, find);
                src = dec;
            }
        }
        printArray(src);
    }

    private static int[] searchInsert(int[] src, int i) {
        int left = 0;
        int right = src.length - 1;
        while (left <= right) {
            int middle = (right + left) / 2;
            if (src[middle] < i) {
                left = middle + 1;
            } else {
                right = middle - 1;
            }
        }
        int[] dec = Arrays.copyOf(src, src.length + 1);
        int decL = dec.length - 1;
        while (decL >= left) {
            if (decL == left) {
                dec[left] = i;
                break;
            }
            dec[decL] = dec[decL - 1];
            decL--;
        }
        return dec;
    }

    private static int binarySearch(int[] src, int i, int left, int right) {
        while (left <= right) {
            int middle = (right + left) / 2;
            if (src[middle] == i) {
                return middle;
            } else if (src[middle] < i) {
                left = middle + 1;
            } else {
                right = middle - 1;
            }
        }
        return -1;
    }

    public static void printArray(int[] src){
        System.out.println();
        for(int i=0;i<src.length;i++){
            System.out.print(src[i]);
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值