C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters
提交网址: https://leetcode.com/problems/longest-substring-without-repeating-characters/
Total Accepted: 149135 Total Submissions: 674678 Difficulty: Medium
Given a string, find the length of the longest substring without repeating characters.
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.
分析:
使用哈希表,保存每个字符上一次出现的位置,时间复杂度为O(n).
AC代码:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int loc[256]; // 哈希表保存字符上一次出现的位置,ASCII码是8位,一般用前7位,是128(2^7)个可用字符。IBM机器的扩展使用了最高位,故用2^8个(256)
memset(loc, -1, sizeof(loc));
int curStartIdx = -1, max_len = 0; //curStartIdx为当前子串的开始位置,初始化为-1
for(int i = 0; i < s.size(); i++)
{
if(loc[s[i]] > curStartIdx) //如果当前字符出现过,那么当前子串的起始位置为这个字符上一次出现的位置+1
{
curStartIdx = loc[s[i]];
}
if(i - curStartIdx > max_len) // 使用贪心算法进行子串延伸,关键!!!
{
max_len = i - curStartIdx;
}
loc[s[i]] = i; //如果当前字符没出现过,将其位置记录在loc数组中
}
return max_len;
}
};
You are here!
Your runtime beats 61.72% of cppsubmissions.
C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告的更多相关文章
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- 【LeetCode】Longest Substring Without Repeating Characters 解题报告
[题意] Given a string, find the length of the longest substring without repeating characters. For exam ...
- [LeetCode] 3. 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 解题报告
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- leetcode:Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode][Python]Longest Substring Without Repeating Characters
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- LeetCode之Longest Substring Without Repeating Characters
[题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...
- Leetcode 3. Longest Substring Without Repeating Characters (Medium)
Description Given a string, find the length of the longest substring without repeating characters. E ...
随机推荐
- 奖品列表组件【仿swiper】
最近lz在做项目的一些优化,发现我的项目里有个奖品列表的功能:我们之前是引入swiper这个库去做的:swiper库的滑动效果确实比较好看,但是js文件以及css文件相对是比较大的:考虑到这个小小的需 ...
- Windows远程桌面相关
3389端口是Windows系统远程终端服务以及系统远程桌面服务默认所使用的端口,原本是为了管理人员能够方便的远程维护管理计算机而设计的,但如今已经成为了黑客们最喜爱的一种人侵途径,入侵者通过3389 ...
- company_base_info_staff_job
"company_base_info_staff_job": { "engine": "python script", # datax or ...
- Windows下安装Kafka
一.安装JDK 二.安装zooeleeper 下载安装包:http://zookeeper.apache.org/releases.html#download 下载后解压到一个目录: 1.进入Zook ...
- windows服务定时任务
其实定时任务时不时会碰到,只不过解决方案也不是只有一个,网上也有很多文章,但是没有一篇说得很清楚,尤其是安装环节,今天就着重说一下安装, 其他步骤带过,C#开发windows服务,开发,安装,调试 1 ...
- window10 Docker仓库访问
window10 Docker仓库访问 docer官网 docker仓库 windown10 安装docker可以参考 window10安装docker 配置了加速器以后还访问不了,点击托盘处dock ...
- Ubuntu下安装Pycharm出现unsupported major.minor version 52.0
(一)原因 Ubuntu下pycharm安装:https://jingyan.baidu.com/article/60ccbceb4e3b0e64cab19733.html pycharm激活:htt ...
- 用kattle将数据从SQLserver中导入到vertica中
今天简单的学习了一下ETL工具kattle了,只是简单的上手,不过这也已经够我去做POC了. 首先大体介绍一下kattle,Kettle是一款国外开源的ETL工具,纯java编写,可以在Window. ...
- swoole+websocket+redis实现一对一聊天
如同web端的QQ和微信一样,这是一个web端的聊天程序. 环境:ubuntu + php + swoole扩展 + redis + mysql Redis 实现每个连接websocket的服务都唯一 ...
- 第一次app经验
第一次做一个app 发现 需要和前端沟通好而且 还要注意细节 效果图细节不要忘记 尽量多穿数据不要少传数据 而且 对接 注意细节