From 74dacdcb1ec8aa992f5e4707d3adbcac51cb2b3c Mon Sep 17 00:00:00 2001 From: miaoswen <55910248+miaoswen@users.noreply.github.com> Date: Sun, 15 Dec 2019 13:52:39 +0800 Subject: [PATCH] Week 08 --- Week 08/id_321/LeetCode_151_321.java | 25 +++++++++++++++++++ Week 08/id_321/LeetCode_205_321.java | 19 ++++++++++++++ Week 08/id_321/LeetCode_387_321.java | 37 ++++++++++++++++++++++++++++ Week 08/id_321/LeetCode_680_321.java | 29 ++++++++++++++++++++++ Week 08/id_321/LeetCode_72_321.java | 29 ++++++++++++++++++++++ 5 files changed, 139 insertions(+) create mode 100644 Week 08/id_321/LeetCode_151_321.java create mode 100644 Week 08/id_321/LeetCode_205_321.java create mode 100644 Week 08/id_321/LeetCode_387_321.java create mode 100644 Week 08/id_321/LeetCode_680_321.java create mode 100644 Week 08/id_321/LeetCode_72_321.java diff --git a/Week 08/id_321/LeetCode_151_321.java b/Week 08/id_321/LeetCode_151_321.java new file mode 100644 index 000000000..deb06f26e --- /dev/null +++ b/Week 08/id_321/LeetCode_151_321.java @@ -0,0 +1,25 @@ +package week08; + +public class Solution151 { + + class Solution { + public String reverseWords(String s) { + StringBuffer res = new StringBuffer(); + s = s.trim(); + int i = s.length() - 1, j = s.length(); + while (i > 0) { + if (s.charAt(i) == ' ') { + res.append(s.substring(i + 1, j)); + res.append(' '); + while (s.charAt(i) == ' ') { + i--; + } + j = i + 1; + } + i--; + } + return res.append(s.substring(0, j)).toString(); + } + } + +} diff --git a/Week 08/id_321/LeetCode_205_321.java b/Week 08/id_321/LeetCode_205_321.java new file mode 100644 index 000000000..b07d2e00c --- /dev/null +++ b/Week 08/id_321/LeetCode_205_321.java @@ -0,0 +1,19 @@ +package week08; + +public class Solution205 { + + class Solution { + public boolean isIsomorphic(String s, String t) { + char[] ch1 = s.toCharArray(); + char[] ch2 = t.toCharArray(); + int len = s.length(); + for (int i = 0; i < len; i++) { + if (s.indexOf(ch1[i]) != t.indexOf(ch2[i])) { + return false; + } + } + return true; + } + } + +} diff --git a/Week 08/id_321/LeetCode_387_321.java b/Week 08/id_321/LeetCode_387_321.java new file mode 100644 index 000000000..51b1552fb --- /dev/null +++ b/Week 08/id_321/LeetCode_387_321.java @@ -0,0 +1,37 @@ +package week08; + +import java.util.HashMap; + +public class Solution387 { + + public int firstUniqChar(String s) { + + int array[] = new int[26]; + for (char ch : s.toCharArray()) { + array[ch - 'a']++; + } + for (int i = 0; i < s.length(); i++) { + if (array[s.charAt(i) - 'a'] == 1) { + return i; + } + } + return -1; + } + + public int firstUniqChar2(String s) { + HashMap count = new HashMap(); + int n = s.length(); + + for (int i = 0; i < n; i++) { + char c = s.charAt(i); + count.put(c, count.getOrDefault(c, 0) + 1); + } + + for (int i = 0; i < n; i++) { + if (count.get(s.charAt(i)) == 1) + return i; + } + return -1; + } + +} diff --git a/Week 08/id_321/LeetCode_680_321.java b/Week 08/id_321/LeetCode_680_321.java new file mode 100644 index 000000000..9bce88eef --- /dev/null +++ b/Week 08/id_321/LeetCode_680_321.java @@ -0,0 +1,29 @@ +package week08; + +public class Solution680 { +//左右两个指针,判断左右两边是否相等 当不等时 左边跳过一个字符或者右边跳过一个字符,只能跳过一次 + public boolean validPalindrome(String s) { + int left = 0; + int right = s.length() - 1;// "abca" + while (left < right) {// 结束条件:left=right的时候 + + if (s.charAt(left) != s.charAt(right)) { + return isPalindrome(s, left, right - 1) || isPalindrome(s, left + 1, right); + } + // 左右两边的字符相等。 + left++; + right--; + } + 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; + } + +} diff --git a/Week 08/id_321/LeetCode_72_321.java b/Week 08/id_321/LeetCode_72_321.java new file mode 100644 index 000000000..becdbdf0a --- /dev/null +++ b/Week 08/id_321/LeetCode_72_321.java @@ -0,0 +1,29 @@ +package week08; + +public class Solution72 { + + public int minDistance(String word1, String word2) { + + int len1 = word1.length(); + int len2 = word2.length(); + int[][] dp = new int[len1 + 1][len2 + 1]; + + for (int i = 1; i <= len2; i++) { + dp[0][i] = dp[0][i - 1] + 1; + } + for (int j = 1; j <= len1; j++) { + dp[j][0] = dp[j - 1][0] + 1; + } + for (int i = 1; i <= len1; i++) { + for (int j = 1; j <= len2; j++) { + if (word1.charAt(i - 1) == word2.charAt(j - 1)) { + dp[i][j] = dp[i - 1][j - 1]; + } else { + dp[i][j] = Math.min(Math.min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1; + } + } + } + return dp[len1][len2]; + + } +}