Skip to content
Merged
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
39 changes: 39 additions & 0 deletions Week 08/id_078/LeetCode_387_078 - Copy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。
//
// 案例:
//
//
//s = "leetcode"
//返回 0.
//
//s = "loveleetcode",
//返回 2.
//
//
//
//
// 注意事项:您可以假定该字符串只包含小写字母。
// Related Topics 哈希表 字符串


import java.util.HashMap;

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int firstUniqChar(String s) {
HashMap<Character, Integer> hashMap = new HashMap<Character, Integer>();
int n = s.length();
for (int i = 0; i < n; i++) {
char c = s.charAt(i);
hashMap.put(c, hashMap.getOrDefault(c, 0) + 1);
}
for (int i = 0; i < n; i++) {
if (hashMap.get(s.charAt(i)) == 1) {
return i;
}
}
return -1;
}

}
//leetcode submit region end(Prohibit modification and deletion)
70 changes: 70 additions & 0 deletions Week 08/id_078/LeetCode_917_078.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//给定一个字符串 S,返回 “反转后的” 字符串,其中不是字母的字符都保留在原地,而所有字母的位置发生反转。
//
//
//
//
//
//
// 示例 1:
//
// 输入:"ab-cd"
//输出:"dc-ba"
//
//
// 示例 2:
//
// 输入:"a-bC-dEf-ghIj"
//输出:"j-Ih-gfE-dCba"
//
//
// 示例 3:
//
// 输入:"Test1ng-Leet=code-Q!"
//输出:"Qedo1ct-eeLg=ntse-T!"
//
//
//
//
// 提示:
//
//
// S.length <= 100
// 33 <= S[i].ASCIIcode <= 122
// S 中不包含 \ or "
//
// Related Topics 字符串



//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public String reverseOnlyLetters(String S) {
if(S.length() <= 1)return S;
//左右指针
int pL = 0; int pR = S.length() - 1;
StringBuilder sb = new StringBuilder();
//移动指针
while(pL < S.length() && pR >= 0){
//如果左端不是字符
if(!isOkchar(S.charAt(pL))){
sb.append(S.charAt(pL));
pL++;
//右端不是字符
}else if(!isOkchar(S.charAt(pR))){
pR--;
}else{
//都是字符
sb.append(S.charAt(pR));
pL++;
pR--;
}
}
//指针没到头的话
sb.append(S.substring(pL,S.length()));
return sb.toString();
}
public boolean isOkchar(char c){
return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z';
}
}
//leetcode submit region end(Prohibit modification and deletion)