File tree Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * words中的单词与pattern逐个字母对比,如果map中value为-1则保存映射关系,
3
+ * 否则进行比较是否为同一映射关系,相同则继续比较,否则表明该字符不符合
4
+ * */
5
+ public List<String> findAndReplacePattern(String[] words, String pattern) {
6
+ List<String> results = new ArrayList<>();
7
+ Set characters = new HashSet<Character>();
8
+ Map<Character,Integer> map = new HashMap<>();//map保存字符映射关系
9
+ out:
10
+ for (int i = 0; i < words.length; i++) {//遍历words
11
+ for (int j = 0; j < pattern.length(); j++) {//初始化map
12
+ map.put(pattern.charAt(j), -288);
13
+ }
14
+ for (int k = 0; k < words[i].length(); k++) {
15
+ if (map.get(pattern.charAt(k)) == -288 && !characters.contains(words[i].charAt(k))) {
16
+ map.put(pattern.charAt(k), pattern.charAt(k) - words[i].charAt(k));
17
+ characters.add(words[i].charAt(k));
18
+ } else {
19
+ if (words[i].charAt(k) != -map.get(pattern.charAt(k)) + pattern.charAt(k)) {
20
+ characters.clear();
21
+ continue out;
22
+ }
23
+ }
24
+ }
25
+ results.add(words[i]);
26
+ characters.clear();
27
+ }
28
+ return results;
29
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int firstUniqChar(String s) {
3
+ if (s.trim().length() == 0)return -1;
4
+ int result = Integer.MAX_VALUE;
5
+ Map<Character,Integer> map = new HashMap<>();
6
+ for (int i = 0; i < s.length(); i++){
7
+ if (map.containsKey(s.charAt(i))){
8
+ map.put(s.charAt(i),Integer.MAX_VALUE);
9
+ }else {
10
+ map.put(s.charAt(i),i);
11
+ }
12
+ }
13
+ for (int i : map.values()){
14
+ result = Math.min(i,result);
15
+ }
16
+ if (result == Integer.MAX_VALUE)result=-1;
17
+ return result;
18
+ }
19
+ }
You can’t perform that action at this time.
0 commit comments