HashMap

作者: gyDBD | 来源:发表于2018-02-26 23:09 被阅读0次

HashMap 拥有的方法:

#Easy Word Pattern

Given a pattern and a string str, find if str follows the same pattern.

Here follow means a full match, such that there is a bijection between a letter in pattern and a non-emptyword in str.

Examples:

pattern = "abba", str = "dog cat cat dog" should return true.

pattern = "abba", str = "dog cat cat fish" should return false.

pattern = "aaaa", str = "dog cat cat dog" should return false.

pattern = "abba", str = "dog dog dog dog" should return false.

思路:因为要做到pattern str的一一对应,pattern的char 和str中的String一一对应,但是hashmap是个一对多的表,所以需要两个HashMap

首先需要判断char数组和String数组长度是否相等

#Hard Word Pattern II 

Given a pattern and a string str, find if str follows the same pattern.

Here follow means a full match, such that there is a bijection between a letter in pattern and a non-emptysubstring in str.

Examples:

pattern = "abab", str = "redblueredblue" should return true.

pattern = "aaaa", str = "asdasdasdasd" should return true.

pattern = "aabb", str = "xyzabcxzyabc" should return false.

思路:

必须要用递归才能遍历到所有的词,而且因为要确保一一对应,仍旧需要我们用两个表,这里可以用HashMap和HashSet,而不一定要用两个HashMap,

如果说这个char已经在map中有对应,那就看看是不是String的i这个位置以这个词开头,如果不是就false, 如果不在map中对应,就要String开始遍历可能的对应,每次记得要map和set一起加,不成功就map和set一起再减掉

#Medium 49.Group Anagrams

Given an array of strings, group anagrams together.

For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], 

Return:

[

  ["ate", "eat","tea"],

  ["nat","tan"],

  ["bat"]

]

HashMap, We use the s sorted as the key, if containsKey so add s; if not, map .putkey and result.add. At last, Collections.sort()

#Medium 525.Contiguous Array

Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1.

Example 1:

Input:[0,1]Output:2Explanation:[0, 1] is the longest contiguous subarray with equal number of 0 and 1.

Example 2:

Input:[0,1,0]Output:2Explanation:[0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.

思路:使用HashMap, 如果不存在这个difference放入map, 如果存在就得到一个0,1的最大连续如果得到现在nums[i-1] ==0 就-1,如果不是就+1;difference[i] = difference[i - 1] + (nums[i-1] == 0 ? -1 : 1);

#Hard 76. Minimum Window Substring

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).

For example,

S = "ADOBECODEBANC"

T = "ABC"

Minimum window is "BANC".

思路:使用一个map(数组)来记录pattern需要的字母的个数,使用一个map来记录现在我们看到的字母了,count来记录总共的需要字母个数。一旦count==0我们让start指针往后移,minLen来记录

#HARD 30. Substring with Concatenation of All Words

You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.

For example, given:

s: "barfoothefoobarman"

words: ["foo", "bar"]

You should return the indices: [0,9].

思路:用两个hashmap来实现,第一个hashmap是存我们的words中需要的字符串个数,然后利用for循环遍历,每次存下一个HashMap来看目前是不是已经满足了要求。

错误:map.containsKey

map.getOrDefault 用于得到目前的个数默认为0,然后再加1

相关文章

网友评论

      本文标题:HashMap

      本文链接:https://www.haomeiwen.com/subject/merkxftx.html