leetcode167,125,344,345 对撞指针

167-q.jpg
    public int[] twoSum(int[] numbers, int target) {

        if(numbers.length < 2)
            throw new IllegalArgumentException("Illegal argument numbers");

        int l = 0, r = numbers.length - 1;
        while(l < r){

            if(numbers[l] + numbers[r] == target){
                int[] res = {l+1, r+1};
                return res;
            }
            else if(numbers[l] + numbers[r] < target)
                l ++;
            else
                r --;
        }

        throw new IllegalStateException("The input has no solution");
    }
125-q.png
   public boolean isPalindrome(String s) {
        if (s == null || s.trim().isEmpty()) {
            return true;
        }
        int l = 0, r = s.length() - 1;
        while (l < r) {
            if(!Character.isLetterOrDigit(s.charAt(l))) {
                l++;
                continue;
            }
            if(!Character.isLetterOrDigit(s.charAt(r))) {
                r--;
                continue;
            }
            if (Character.toLowerCase(s.charAt(l)) == Character.
                    toLowerCase(s.charAt(r))) {
                l++;
                r--;
            } else {
                return false;
            }
        }
        return true;
    }
344-q.png
    public String reverseString(String s) {
        int n = s.length();
        if (n == 0 || n == 1)
            return s;
        int min = 0, max = n - 1;
        char temp;
        char[] chars=s.toCharArray();
        while(min < max){
            temp =chars[min];
            chars[min] = chars[max];
            chars[max] = temp;
            min++;
            max--;
        }
        return s;
    }
345.png
  public String reverseVowels(String s) {
        String vowels = "aoeiuAOEIU";
        char[] a = s.toCharArray();
        int i = 0;
        int j = a.length - 1;
        while (i < j) {
            while (i < j && !vowels.contains(a[i] + "")) {
                i++;
            }
            while (i < j && !vowels.contains(a[j] + "")) {
                j--;
            }
            if (i < j) {
                char c = a[i];
                a[i++] = a[j];
                a[j--] = c;
            }
        }
        return new String(a);
    }

你可能感兴趣的:(leetcode167,125,344,345 对撞指针)