当前位置:网站首页>hot100 哈希
hot100 哈希
2022-07-26 07:17:00 【wzf6667】
- 无重复字符的最长子串
给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
示例 1:
输入: s = “abcabcbb”
输出: 3
解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。
采用滑动窗口,具体算法为:
for循环里的i为窗口右值,设定一个左值为0,如果当前窗口中不包含重复元素,则右边界向右滑动,如果包含,则左边界为重复元素位置加1。
注意在选取左边界的时候,当前字符不包含在当前最长有效子段中,如:abba,我们先添加a,b进map,此时left=0,我们再添加b,发现map中包含b,而且b包含在最长有效子段中,就是1)的情况,我们更新 left=map.get(b)+1=2,此时子段更新为 b,而且map中仍然包含a,map.get(a)=0; 随后,我们遍历到a,发现a包含在map中,且map.get(a)=0,如果我们像1)一样处理,就会发现 left=map.get(a)+1=1,实际上,left此时 应该不变,left始终为2,子段变成 ba才对。
class Solution {
public int lengthOfLongestSubstring(String s) {
Map<Character,Integer> map = new HashMap<>();
int max = 0;
int left = 0;
for(int i = 0; i < s.length();i++){
if(map.containsKey(s.charAt(i))){
left = Math.max(left,map.get(s.charAt(i))+1);
}
max = Math.max(max,i-left+1);
map.put(s.charAt(i),i);
}
return max;
}
}
- 电话号码的字母组合
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
示例 1:
输入:digits = “23”
输出:[“ad”,“ae”,“af”,“bd”,“be”,“bf”,“cd”,“ce”,“cf”]
递归回溯
注意add完之后不需要删除letters最后一个字母
class Solution {
public List<String> letterCombinations(String digits) {
List<String> combinations = new ArrayList<String>();
if (digits.length() == 0) {
return combinations;
}
Map<Character, String> phoneMap = new HashMap<Character, String>() {
{
put('2', "abc");
put('3', "def");
put('4', "ghi");
put('5', "jkl");
put('6', "mno");
put('7', "pqrs");
put('8', "tuv");
put('9', "wxyz");
}};
digui(combinations,phoneMap,digits,0,new StringBuffer());
return combinations;
}
public void digui(List<String> combinations,Map<Character, String> phoneMap,String digits, int index, StringBuffer letters){
if(index == digits.length()){
combinations.add(letters.toString());
return;
}
String content = phoneMap.get(digits.charAt(index));
for(int i = 0; i < content.length(); i++){
char c = content.charAt(i);
letters.append(c);
digui(combinations,phoneMap,digits,index+1,letters);
letters.deleteCharAt(index);
}
}
}
边栏推荐
- 屏:框贴、0贴合、全贴合
- Getting started with kernel PWN (5)
- PR字幕制作
- How regular expressions write variables
- 3.0.0 alpha 重磅发布!九大新功能、全新 UI 解锁调度系统新能力
- 4、数据的完整性
- HCIP --- MPLS技术
- College degree sales career, from the third tier 4K to the first tier 20k+, I am very satisfied with myself
- Manifest merger failed with multiple errors, see logs
- Usage of unity3d object pool
猜你喜欢

Drools (3): drools basic syntax (1)

NFT数字藏品系统开发:NFT数藏 的最佳数字营销策略有哪些

Apache Dolphinscheduler3.0.0-beta-1 版本发布,新增FlinkSQL、Zeppelin任务类型

Hands on practice - teach you how to make an intelligent fish tank with STM32
![Leetcode:1898. maximum number of removable characters [if you want to delete some IDX from a pile of things, don't use pop]](/img/e6/a17902a73ff6a9d4393c96a019b78e.png)
Leetcode:1898. maximum number of removable characters [if you want to delete some IDX from a pile of things, don't use pop]

Solve the problem that Chrome browser is tampered with by drug bullies

Apache dolphin scheduler & tidb joint meetup | focus on application development capabilities under the development of open source ecosystem

数据平台调度升级改造 | 从Azkaban 平滑过度到 Apache DolphinScheduler 的操作实践
![[arm learning (8) AXF tool analysis]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[arm learning (8) AXF tool analysis]

LTS(Light-Task-Scheduler)
随机推荐
QT: modal, modeless, text box, button, single line input box
4、数据的完整性
[romance understood by technical talents] tidb community has prepared a gift for your partner for the "Tanabata Festival". Reply: if I want to challenge, I can participate in the activity!
Leetcode:1898. maximum number of removable characters [if you want to delete some IDX from a pile of things, don't use pop]
Weekly tip 142: multi parameter constructors and explicit
ModuleNotFoundError: No module named ‘pip‘解决办法
Contents mismatch at: 08000000H (Flash=FFH Required=00H) ! Too many errors to display !
File server fastdfs
Why can't extern compile variables decorated with const?
Deep learning visualization
[arm learning (8) AXF tool analysis]
Opengauss simple version installation error
JIT中的IR工具与JITWatch的下载,编译及使用
倒计时2日!基于 Apache DolphinScheduler&TiDB 的交叉开发实践,从编写到调度让你大幅提升效率
Airiot IOT platform enables the container industry to build a welding station information monitoring system
数据平台调度升级改造 | 从Azkaban 平滑过度到 Apache DolphinScheduler 的操作实践
Tips when entering a formula in latex
Check the top 10 best graphics software of the year, meet 99% of your chart needs, and collect it quickly
[749. Isolate virus]
unity3d-对象池的用法