本题来自:力扣-每日一题
力扣 (LeetCode) 全球极客挚爱的技术成长平台https://leetcode.cn/
题解:
class Solution {public String getSmallestString(String s) {int len = s.length();int[] arr = new int[len];for (int i = 0; i < len; i++) {arr[i] = Integer.parseInt(String.valueOf(s.charAt(i)));}for(int i = 0;i < len - 1;i++){if((arr[i] > arr[i+1])&&((arr[i] + arr[i+1]) % 2 == 0)) {int x = arr[i];arr[i] = arr[i+1];arr[i+1] = x;break;}}String newstr = "";for(int x:arr){newstr += (x + "");}return newstr;}
}
思路:
不需要单独判断奇数或者偶数,只需要知道他们的奇偶性相同即可,也就是两数相加等于偶数即可