HashMap

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

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,168评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,494评论 0 23
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,833评论 19 139
  • 医院上班,第一天见习。友好的师姐说下午可以休息,下午补了昨晚失眠的觉,又去上哈根达斯的班。晚上回来,再继续写见习笔...
    长白山的鹿阅读 1,337评论 0 1
  • 前言 看到很多django的部署都是nginx + uwsgi 来实现,我只能说赶紧抛弃uwsgi投入gunico...
    kkyflying阅读 4,066评论 0 1

友情链接更多精彩内容