排序后,双指针移动比较,大的就赋值即可:
import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); StringBuilder r = new StringBuilder(sc.nextLine()); String str = sc.nextLine(); char[] v = new char[str.length()]; for(int i = 0; i < str.length(); ++i){ v[i] = str.charAt(i); } Arrays.sort(v); int i = v.length - 1, j = 0; while(i >= 0 && j < r.length()){ if(r.charAt(j) < v[i]){ r.setCharAt(j, v[i]); j++; i--; }else j++; } System.out.println(r); } }