leetcode算法思想题

双指针

167:两数之和

public int[] twoSum(int[] numbers, int target) {
     
    if (numbers == null) return null;
    int i = 0, j = numbers.length - 1;
    while (i < j) {
     
        int sum = numbers[i] + numbers[j];
        if (sum == target) {
     
            return new int[]{
     i + 1, j + 1};
        } else if (sum < target) {
     
            i++;
        } else {
     
            j--;
        }
    }
    return null;
}

633:两数平方和

 public boolean judgeSquareSum(int target) {
     
     if (target < 0) return false;
     int i = 0, j = (int) Math.sqrt(target);
     while (i <= j) {
     
         int powSum = i * i + j * j;
         if (powSum == target) {
     
             return true;
         } else if (powSum > target) {
     
             j--;
         } else {
     
             i++;
         }
     }
     return false;
 }

345:反转字符串中的元音字符

private final static HashSet<Character> vowels = new HashSet<>(
        Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));/这些结构的操作不是很熟悉

public String reverseVowels(String s) {
     
    if (s == null) return null;
    int i = 0, j = s.length() - 1;
    char[] result = new char[s.length()];
    while (i <= j) {
     
        char ci = s.charAt(i);
        char cj = s.charAt(j);
        if (!vowels.contains(ci)) {
     
            result[i++] = ci;
        } else if (!vowels.contains(cj)) {
     
            result[j--] = cj;
        } else {
     
            result[i++] = cj;
            result[j--] = ci;
        }
    }
    return new String(result);
}

680:回文字符串

public boolean validPalindrome(String s) {
     
    for (int i = 0, j = s.length() - 1; i < j; i++, j--) {
     
        if (s.charAt(i) != s.charAt(j)) {
     
            return isPalindrome(s, i, j - 1) || isPalindrome(s, i + 1, j);
        }
    }
    return true;
}

private boolean isPalindrome(String s, int i, int j) {
     
    while (i < j) {
     
        if (s.charAt(i++) != s.charAt(j--)) {
     
            return false;
        }
    }
    return true;
}

88:归并两个有序数组

题目描述:把归并结果存到第一个数组上。需要从尾开始遍历,否则在 nums1 上归并得到的值会覆盖还未进行归并比较的值。

public void merge(int[] nums1, int m, int[] nums2, int n) {
     
    int index1 = m - 1, index2 = n - 1;
    int indexMerge = m + n - 1;
    while (index1 >= 0 || index2 >= 0) {
     
        if (index1 < 0) {
     
            nums1[indexMerge--] = nums2[index2--];
        } else if (index2 < 0) {
     
            nums1[indexMerge--] = nums1[index1--];
        } else if (nums1[index1] > nums2[index2]) {
     
            nums1[indexMerge--] = nums1[index1--];
        } else {
     
            nums1[indexMerge--] = nums2[index2--];
        }
    }
}

141:判断链表是否存在环

使用双指针,一个指针每次移动一个节点,一个指针每次移动两个节点,如果存在环,那么这两个指针一定会相遇

public boolean hasCycle(ListNode head) {
     
    if (head == null) {
     
        return false;
    }
    ListNode l1 = head, l2 = head.next;
    while (l1 != null && l2 != null && l2.next != null) {
     
        if (l1 == l2) {
     
            return true;
        }
        l1 = l1.next;
        l2 = l2.next.next;
    }
    return false;
}

524:最长子序列

public String findLongestWord(String s, List<String> d) {
     
    String longestWord = "";
    for (String target : d) {
     
        int l1 = longestWord.length(), l2 = target.length();
        if (l1 > l2 || (l1 == l2 && longestWord.compareTo(target) < 0)) {
     
            continue;
        }
        if (isSubstr(s, target)) {
     
            longestWord = target;
        }
    }
    return longestWord;
}

private boolean isSubstr(String s, String target) {
     
    int i = 0, j = 0;
    while (i < s.length() && j < target.length()) {
     
        if (s.charAt(i) == target.charAt(j)) {
     
            j++;
        }
        i++;
    }
    return j == target.length();
}

参考:https://github.com/CyC2018/CS-Notes/blob/master/notes/Leetcode%20%E9%A2%98%E8%A7%A3%20-%20%E5%8F%8C%E6%8C%87%E9%92%88.md

你可能感兴趣的:(leetcode专题,leetcode,指针,java)