LeetCOde------3.Longest Substring Without Repeating Characters
2016-09-14 15:44
337 查看
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given
which the length is 3.
Given
with the length of 1.
Given
with the length of 3. Note that the answer must be a substring,
a subsequence and not a substring.
Examples:
Given
"abcabcbb", the answer is
"abc",
which the length is 3.
Given
"bbbbb", the answer is
"b",
with the length of 1.
Given
"pwwkew", the answer is
"wke",
with the length of 3. Note that the answer must be a substring,
"pwke"is
a subsequence and not a substring.
public class Solution{ public static int lengthOfLongestSubstring(String s) { if(s==null) return 0; char[] arr = s.toCharArray(); int pre = 0; HashMap<Character, Integer> map = new HashMap<Character, Integer>(); for (int i = 0; i < arr.length; i++) { if (!map.containsKey(arr[i])) { map.put(arr[i], i); } else { pre = Math.max(pre, map.size()); i = map.get(arr[i]); map.clear(); } } return Math.max(pre, map.size()); } }
相关文章推荐
- Android 雷达扫描效果、动画SweepGradient
- opencv安装教程
- CCF 201409-2 我100
- 抽象类
- MAC 读写NTFS硬盘
- Oracle学习笔记之第七节手动数据库链介绍
- Spring之AOP(二)
- 序列化与反序列化
- linux中一个简单的TCP套接字程序(2)
- Android 开源项目及其学习
- STM32F4的system_stm32f4xx.c文件需要根据不同板卡进行修改
- innerHTML、outerHTML、innerText、outerText的区别
- 追着嘟嘟学c#系列入门第八篇-----c#的关系运算符和逻辑运算符【蓝鸥出品】
- RDD行动Action操作(6)–saveAsHadoopFile、saveAsHadoopDataset
- iOS 开发中你是否遇到这些经验问题(一)
- TensorFlow学习笔记(7)----TensorBoard_2
- Accp 14章 实现客户姓名的添加 修改 显示 关键代码
- 三元运算符性能更优
- 十四章示例4
- python reload(sys)找不到,name 'reload' is not defined