LeetCode第三题—— Longest Substring Without Repeating Characters(最长无重复子字符串)
题目描述
Given a string, find the length of the longest substring without repeating characters.
Example 1
Input: "abcabcbb"
Output: 3
Explanation: The answer is"abc", with the length of 3.
Example 2
Input: "bbbbb"
Output: 1
Explanation: The answer is"b", with the length of 1.
Example 3
Input: "pwwkew"
Output: 3
Explanation: 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.
My solution(94ms,39.2MB)
class Solution {
public int lengthOfLongestSubstring(String s) {
HashMap<String,Integer> map = new HashMap<String,Integer>();
int result=0,current=1;
for(int i=0;i<s.length();){
String temp = s.substring(i,i+1);
if (!map.containsKey(temp)) {
map.put(temp,1);
i++;
}else{
i = current;
current++;
int len = map.size();
if(len > result){ result = len;}
map.clear();
}
if(map.size()>result){
result = map.size();
}
}
return result;
}
}
LeetCode第三题—— Longest Substring Without Repeating Characters(最长无重复子字符串)的更多相关文章
- leetcode第三题--Longest Substring Without Repeating Characters
Problem:Given a string, find the length of the longest substring without repeating characters. For e ...
- leetcode第三题Longest Substring Without Repeating Characters java
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- LeetCode 第 3 题(Longest Substring Without Repeating Characters)
LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...
- 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)
Given a string, find the length of the longest substring without repeating characters. Example 1: ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
- [LeetCode] Longest Substring Without Repeating Characters最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
随机推荐
- linux 服务器修改密码
登录服务器后直接输入命令行passwd root 然后输入两次新密码就行
- python 中 random模块的用法
import random print( random.randint(1,10) ) # 产生 1 到 10 的一个整数型随机数 print( random.random() ) # 产生 0 到 ...
- 【gcc】更新下载编译gcc遇到的各种问题
帮学长的oj升级gcc版本.遇到了贼多问题.. [悲惨的开始] 安装gcc版本推荐ustc的mirror的下载,超快der... https://mirrors.ustc.edu.cn/gnu/gcc ...
- (数据科学学习手札60)用Python实现WGS84、火星坐标系、百度坐标系、web墨卡托四种坐标相互转换
一.简介 主流被使用的地理坐标系并不统一,常用的有WGS84.GCJ02(火星坐标系).BD09(百度坐标系)以及百度地图中保存矢量信息的web墨卡托,本文利用Python编写相关类以实现4种坐标系统 ...
- Level DB 小调研
一. 概况: 1. 背景: 随着信息技术的高速发展,数据存储量和流量呈现爆炸式增长.目前百度统计日 PV(日点击量)已超过 75 亿次,中国网民在百度上进行50 亿次的搜索请求,百度贴吧日 PV 十亿 ...
- linux7查看时间同步服务器的匹配源
当服务器时间与设定好的同步时间源的时间有差异的时候,一般都需要先查看本机的时间同步服务功能是否在正常的运转,以及同步的时间源是哪里,在这里为大家提供一个检查时间用的命令. linux/centos 7 ...
- UVA11427 Expect the Expected 概率dp+全概率公式
题目传送门 题意:小明每晚都玩游戏,每一盘赢的概率都是p,如果第一盘就赢了,那么就去睡觉,第二天继续玩:否则继续玩,玩到赢的比例大于p才去睡:如果一直玩了n盘还没完成,就再也不玩了:问他玩游戏天数的期 ...
- wireshark 分析 TCP 请求(转)
转自:http://supben.iteye.com/blog/2329780 先看一段代码 程序片段是一个RPC调用 ,根据简历id获取简历实体.本地IP 10.252.156.132, 远程ip ...
- Ubuntu16.04 使用PPA安装JDK8
安装Java 8 ( 支持 Ubuntu 10.04 - Ubuntu 16.04 ) 1.如果你在 Ubuntu 软件中心安装过 OpenJDK,请先使用如下命令将其删除: sudo apt-get ...
- 微信小程序-云开发
云开发 初始化 wx.cloud.init({ env: 'test-x1dzi' }) 云数据库 数据类型 String,Number,Boolean,Array,Object,Date,GeoPo ...