[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接口测试(三)——Excell文件读取进行参数化
python进行http请求时,需要对参数进行参数化,此时就可以运用Excel进行,具体如下: 1.梳理出请求中那些参数需要参数化,然后新建一个Excel,如图: 2.读取Excel中的内容,在读取前 ...
- django中判断当前user具有是否有对模块的增删改查权限
首先简单了解一下user的一些属性 User对象 User对象是认证系统的核心.用户对象通常用来代表网站的用户,并支持例如访问控制.注册用户.关联创建者和内容等.在Django认证框架中只有一个用户类 ...
- 数据挖掘算法:DBSCAN算法的C++实现
(期末考试快到了,所以比较粗糙,请各位读者理解..) 一. 概念 DBSCAN是一种产生划分聚类的基于密度的聚类算法,簇的个数由算法自动地确定.低密度区域中的点被视为噪声而忽略,因此DBSCAN ...
- C++学习---- static关键字用法总结
static: 作用:存储在静态数据区的变量会在程序刚开始运行时就完成初始化,也是唯一的一次初始化.共有两种变量存储在静态存储区:全局变量和static变量. 1.隐藏:(静态全局变量和静态函数) ...
- MongoDB复制二:复制集的管理
1.修改oplog的大小 需要在每个机器上都配置.先在secondary上操作,最后在primary上操作. 1)以单机的方式重启复制集的实例 db.shutdownServer() 在新的端口中启 ...
- vue 介绍的拓展
计算属性 <div id="example"> <p>Original message: "{{ message }}"</p&g ...
- 算法(13)Contiguous Array
题目:找出数组的一个子数组,要求这个子数组中0和1的数量相等,找出最大长度的这样的数组! 思路:也是受网上算法的启发吧,用一个 语言:如何初始化一个unordered_map<int,int&g ...
- calendar components
calendar components 日历 angular, react, vue ??? react https://github.com/intljusticemission/react-big ...
- Android中常见的坑有哪些?
对于安卓开发入门级程序猿而言,由于不熟悉代码.工具等等,掉进一些坑中是难免的,今天小编在网上看到一位大神总结的Android开发中比较常见的坑及其原因和解决办法,赶脚还不错,分享出来,给大家提个醒. ...
- Luogu2662 牛场围栏(最短路)
小凯的疑惑升级版的升级版.答案若存在不会超过30002-3000,暴力dp似乎勉强可以过.当然这不优美. 注意到如果能拼出长度为l的围栏,就一定能拼出长度为l+kx的围栏,其中x为最短的(或任意一个) ...