[Leetcode Week1]Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
Description
Given a string, find the length of the longest substring without repeating characters..
Example
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.
Solution
int lengthOfLongestSubstring(char* s) {
int len = strlen(s);
if (len == 0)
return 0;
int charbucket[300] = {0};
int startidx, i;
int maxlen = 1, substrlen = 0;
for (startidx = 0; startidx < len; startidx++) {
memset(charbucket, 0, sizeof(charbucket));
for (i = startidx; i < len; i++) {
if (charbucket[s[i]] == 0) {
charbucket[s[i]] = 1;
} else {
break;
}
}
substrlen = 0;
for (i = 0; i < 300; i++) {
if (charbucket[i] == 1)
substrlen++;
}
if (substrlen > maxlen)
maxlen = substrlen;
}
return maxlen;
}
解题描述
这道题目的是找到给定字符串最长的子串的长度,且子串的字符不能重复。我的突破口是针对字符不能重复这个点,使用一个类似C++中的布尔数组,记录子串中出现的字符。然后通过不断查找到最长字串的长度更新结果值。
[Leetcode Week1]Longest Substring Without Repeating Characters的更多相关文章
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- [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 ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- 【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
随机推荐
- Python 3基础教程32-正则
本文介绍Python的正则,通过本文介绍和一个练习,对正则有一个基本了解就可以. # 正则表达式 ''' 正则表达式是有一些特殊字符组成,能够帮你找到一些符合一定规则的字符串 先来了解几个符号所代表的 ...
- 【page.json】配置说明
页面.json用来对本页面的窗口表现进行配置.它只能针对window配置,并且会覆盖 app.json 的 window 中相同的配置项. { /** * 以下是页面顶部导航栏设置 **/ " ...
- Visual Studio 2012安装包
点击下载
- 配置cas可外网访问
把应用程序tomcat下的conf下的context.xml里配置内容修改 如把: D:\apache-tomcat-APP\conf\context.xml <Resource name=&q ...
- k8s第一个实例创建redis集群服务
1.创建redis-master-controller.yaml apiVersion: v1 kind: ReplicationController metadata: name: redis-ma ...
- 微信小程序-腾讯地图显示偏差问题
原文地址: http://fanjiajia.cn/2018/08/30/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F-%E8%85%BE%E8%AE%A ...
- 201621044079 week05-继承、多态、抽象类与接口
作业05-继承.多态.抽象类与接口 1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 接口 interface关键字 implements has-a;comparable co ...
- 子查询 做where条件 做 from的临时表 ,做select的一个字段 等
子查询 做where条件 做 from的临时表 ,做select的一个字段 等
- CSS兼容性总结
一.针对IE6的 !important 必须写在前面,例如: background:#9C6 !important;background:#999; 二.CSS HACK //IE6 专用 _heig ...
- BZOJ4448 SCOI2015情报传递(离线+树链剖分+树状数组)
即滋磁单点修改,询问路径上小于某数的值有多少个.暴力树剖套个主席树(或者直接树上主席树,似乎就1个log了?感觉不一定比两个log快)即可,然而不太优美. 开始觉得可以cdq,然而就变成log^3了. ...