1528-重新排列字符串

重新排列字符串

  1. 我第一次做法

        public String restoreString(String s, int[] indices) {
            if (s == null || indices == null) {
                return null;
            }
    
            char[] result = new char[indices.length];
            for (int i = 0; i < indices.length; i++) {
                result[indices[i]] = s.charAt(i);
            }
    
            return String.valueOf(result);
        }
    
  2. 其他解法

     public static String restoreString1(String s, int[] indices) {
            if (s == null || indices == null) {
                return null;
            }
    
            char[] chars = s.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                if (i != indices[i]) {
                    int targetIndex = indices[i];
                    char tmp = chars[targetIndex];
                    chars[targetIndex] = chars[i];
                    chars[i] = tmp;
    
                    int tmp2 = indices[targetIndex];
                    indices[targetIndex] = indices[i];
                    indices[i] = tmp2;
    
                    // 由于替换完成后,并不能确定,再继续执行遍历会将剩余的元素完全替换,则重置便利游标
                    // 举例: "aiohn" {3,1,4,2,0}
                    i=0;
                }
            }
    
            return String.valueOf(chars);
        }
    

你可能感兴趣的:(1528-重新排列字符串)