結果

問題 No.927 Second Permutation
ユーザー ks2mks2m
提出日時 2019-11-22 23:02:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 192 ms / 2,000 ms
コード長 784 bytes
コンパイル時間 1,833 ms
コンパイル使用メモリ 77,788 KB
実行使用メモリ 54,736 KB
最終ジャッジ日時 2024-04-19 12:22:24
合計ジャッジ時間 7,394 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 97 ms
52,788 KB
testcase_01 AC 96 ms
52,988 KB
testcase_02 AC 109 ms
53,740 KB
testcase_03 AC 108 ms
54,104 KB
testcase_04 AC 108 ms
53,956 KB
testcase_05 AC 110 ms
53,896 KB
testcase_06 AC 102 ms
52,808 KB
testcase_07 AC 107 ms
53,768 KB
testcase_08 AC 144 ms
53,996 KB
testcase_09 AC 112 ms
53,756 KB
testcase_10 AC 169 ms
54,512 KB
testcase_11 AC 189 ms
54,640 KB
testcase_12 AC 174 ms
54,248 KB
testcase_13 AC 129 ms
53,116 KB
testcase_14 AC 173 ms
54,272 KB
testcase_15 AC 152 ms
54,272 KB
testcase_16 AC 192 ms
54,704 KB
testcase_17 AC 189 ms
54,264 KB
testcase_18 AC 192 ms
54,424 KB
testcase_19 AC 192 ms
54,736 KB
testcase_20 AC 172 ms
54,380 KB
testcase_21 AC 178 ms
54,552 KB
testcase_22 AC 186 ms
54,324 KB
testcase_23 AC 186 ms
54,296 KB
testcase_24 AC 186 ms
54,368 KB
testcase_25 AC 163 ms
54,260 KB
testcase_26 AC 152 ms
54,400 KB
testcase_27 AC 146 ms
54,176 KB
testcase_28 AC 174 ms
54,520 KB
testcase_29 AC 176 ms
54,336 KB
権限があれば一括ダウンロードができます

ソースコード

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