From b03f841ed18190c9b42997998de839731a98cc88 Mon Sep 17 00:00:00 2001 From: HaoWangCold <1208922591@qq.com> Date: Wed, 11 Dec 2019 13:09:10 +0800 Subject: [PATCH] Week08_Leetcode_387_917 --- Week 08/id_078/LeetCode_387_078 - Copy.java | 39 ++++++++++++ Week 08/id_078/LeetCode_917_078.java | 70 +++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 Week 08/id_078/LeetCode_387_078 - Copy.java create mode 100644 Week 08/id_078/LeetCode_917_078.java diff --git a/Week 08/id_078/LeetCode_387_078 - Copy.java b/Week 08/id_078/LeetCode_387_078 - Copy.java new file mode 100644 index 000000000..963c1212e --- /dev/null +++ b/Week 08/id_078/LeetCode_387_078 - Copy.java @@ -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 hashMap = new HashMap(); + 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) diff --git a/Week 08/id_078/LeetCode_917_078.java b/Week 08/id_078/LeetCode_917_078.java new file mode 100644 index 000000000..134868765 --- /dev/null +++ b/Week 08/id_078/LeetCode_917_078.java @@ -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)