Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Week 08/id_321/LeetCode_151_321.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

}
19 changes: 19 additions & 0 deletions Week 08/id_321/LeetCode_205_321.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

}
37 changes: 37 additions & 0 deletions Week 08/id_321/LeetCode_387_321.java
Original file line number Diff line number Diff line change
@@ -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<Character, Integer> count = new HashMap<Character, Integer>();
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;
}

}
29 changes: 29 additions & 0 deletions Week 08/id_321/LeetCode_680_321.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
29 changes: 29 additions & 0 deletions Week 08/id_321/LeetCode_72_321.java
Original file line number Diff line number Diff line change
@@ -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];

}
}