diff --git a/docs/notes/Leetcode 题解 - 双指针.md b/docs/notes/Leetcode 题解 - 双指针.md index f595ca46..471cc0bd 100644 --- a/docs/notes/Leetcode 题解 - 双指针.md +++ b/docs/notes/Leetcode 题解 - 双指针.md @@ -102,13 +102,23 @@ Explanation: 1 * 1 + 2 * 2 = 5 Given s = "leetcode", return "leotcede". ``` -使用双指针指向待反转的两个元音字符,一个指针从头向尾遍历,一个指针从尾到头遍历。 +

+ +使用双指针,一个指针从头向尾遍历,一个指针从尾到头遍历,当两个指针都遍历到元音字符时,交换这两个元音字符。 + +为了快速判断一个字符是不是元音字符,我们将全部元音字符添加到集合 HashSet 中,从而以 O(1) 的时间复杂度进行该操作。 + +- 时间复杂度为 O(N):只需要遍历所有元素一次 +- 空间复杂度 O(1):只需要使用两个额外变量 + +

```java private final static HashSet 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) { diff --git a/docs/pics/399b459d-db9e-4e77-b879-e6492c7d382b.gif b/docs/pics/399b459d-db9e-4e77-b879-e6492c7d382b.gif new file mode 100644 index 00000000..66d52391 Binary files /dev/null and b/docs/pics/399b459d-db9e-4e77-b879-e6492c7d382b.gif differ diff --git a/docs/pics/a7cb8423-895d-4975-8ef8-662a0029c772.png b/docs/pics/a7cb8423-895d-4975-8ef8-662a0029c772.png new file mode 100644 index 00000000..29f9d362 Binary files /dev/null and b/docs/pics/a7cb8423-895d-4975-8ef8-662a0029c772.png differ diff --git a/notes/Leetcode 题解 - 双指针.md b/notes/Leetcode 题解 - 双指针.md index 22e58f37..2b433bc4 100644 --- a/notes/Leetcode 题解 - 双指针.md +++ b/notes/Leetcode 题解 - 双指针.md @@ -102,13 +102,23 @@ Explanation: 1 * 1 + 2 * 2 = 5 Given s = "leetcode", return "leotcede". ``` -使用双指针指向待反转的两个元音字符,一个指针从头向尾遍历,一个指针从尾到头遍历。 +

+ +使用双指针,一个指针从头向尾遍历,一个指针从尾到头遍历,当两个指针都遍历到元音字符时,交换这两个元音字符。 + +为了快速判断一个字符是不是元音字符,我们将全部元音字符添加到集合 HashSet 中,从而以 O(1) 的时间复杂度进行该操作。 + +- 时间复杂度为 O(N):只需要遍历所有元素一次 +- 空间复杂度 O(1):只需要使用两个额外变量 + +

```java private final static HashSet 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) { diff --git a/notes/pics/399b459d-db9e-4e77-b879-e6492c7d382b.gif b/notes/pics/399b459d-db9e-4e77-b879-e6492c7d382b.gif new file mode 100644 index 00000000..66d52391 Binary files /dev/null and b/notes/pics/399b459d-db9e-4e77-b879-e6492c7d382b.gif differ diff --git a/notes/pics/a7cb8423-895d-4975-8ef8-662a0029c772.png b/notes/pics/a7cb8423-895d-4975-8ef8-662a0029c772.png new file mode 100644 index 00000000..29f9d362 Binary files /dev/null and b/notes/pics/a7cb8423-895d-4975-8ef8-662a0029c772.png differ