当前位置:网站首页>Li Kou daily question 917

Li Kou daily question 917

2022-07-26 10:18:00 Fabulouskkk

Yesterday was Hard Today is Easy 了 hhhh
 Insert picture description here
Two pointers traverse from front to back , One goes back and forth , Two pointers pointing to letters at the same time is a direct exchange , Then together ++、–
Otherwise, each ++,–

public static String reverseOnlyLetters(String s) {
    
        char[] chars = new char[s.length()];
        for (int i = 0; i < chars.length; i++) {
    
            chars[i] = s.charAt(i);
        }
        int leftIndex = 0;
        int rightIndex = chars.length - 1;
        while (leftIndex < rightIndex) {
    
            if (Character.isLetter(chars[leftIndex]) && Character.isLetter(chars[rightIndex])) {
    
                swap(chars, leftIndex, rightIndex);
                leftIndex++;
                rightIndex--;
            } else if (!Character.isLetter(chars[rightIndex])) {
    
                rightIndex--;
            } else if (!Character.isLetter(chars[leftIndex])) {
    
                leftIndex++;
            }
        }
        return new String(chars);
    }
    private static void swap(char[] chars, int leftIndex, int rightIndex) {
    
        char temp = chars[leftIndex];
        chars[leftIndex] = chars[rightIndex];
        chars[rightIndex] = temp;
    }

char Type array to String When outputting new String Object of type , Otherwise, the output address …

原网站

版权声明
本文为[Fabulouskkk]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/201/202207181750364609.html