結果

問題 No.927 Second Permutation
ユーザー ks2m
提出日時 2019-11-22 23:02:28
言語 Java
(openjdk 23)
結果
AC  
実行時間 225 ms / 2,000 ms
コード長 784 bytes
コンパイル時間 2,614 ms
コンパイル使用メモリ 77,744 KB
実行使用メモリ 54,720 KB
最終ジャッジ日時 2024-10-11 04:39:17
合計ジャッジ時間 9,244 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		String x = sc.next();
		sc.close();

		int[] a = new int[10];
		for (int i = 0; i < x.length(); i++) {
			a[x.charAt(i) - '0']++;
		}

		StringBuilder sb = new StringBuilder();
		for (int i = 9; i >= 0; i--) {
			while (a[i] > 0) {
				sb.append(i);
				a[i]--;
			}
		}

		boolean flg = false;
		for (int i = sb.length() - 1; i > 0; i--) {
			if (sb.charAt(i) != sb.charAt(i - 1)) {
				char ch = sb.charAt(i);
				sb.setCharAt(i, sb.charAt(i - 1));
				sb.setCharAt(i - 1, ch);
				flg = true;
				break;
			}
		}
		if (!flg || sb.charAt(0) == '0') {
			System.out.println(-1);
			return;
		}
		System.out.println(sb.toString());
	}
}
0