[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 基础篇 16 递归和二分数查找与编码补充回顾
编码回顾补充: 回顾编码问题: 编码相当于密码本,关系到二进制与看懂的文字的的对应关系. 最早期的密码本: ascii码:只包含英文字母,数字,特殊字符. ...
- LeetCode - 38. Count and Say(36ms)
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- js日期插件bootstrap-datetimepicker的使用
js日期插件——bootstrap-datetimepicker的使用心得: 在大多说的web项目中,都有日期选择器的使用.如果自己写一个日期选择器的话,费时又费力,而且不一定能写出来_(:3 」∠) ...
- NO6——KMP
int next[N]; char str1[M],str2[N]; //str1 长,str2 短 //len1,len2,对应str1,str2的长 void get_next(int len2) ...
- 从微软msdn阅读事件的使用
微软文章:如何:在 Windows 窗体应用程序中使用事件 地址:https://msdn.microsoft.com/zh-cn/library/0y0987sc.aspx 文章:C#事件的订阅与触 ...
- Linux TCP协议使用的变量
Linux /proc/sys/net/ipv4/* 变量 TCP变量:somaxconn - INTEGER listen()的backlog参数的上限,在用户态为SOMAXCONN.默认是1 ...
- webstorm-前端javascript开发神器中文教程和技巧分享(转)
webstorm是一款前端javascript开发编辑的神器,此文介绍webstorm的中文教程和技巧分享. webstorm8.0.3中文汉化版下载: 百度网盘下载:http://pan.baidu ...
- SSH答疑解惑系列(二)——java.lang.reflect.InvocationTargetException异常
在项目中遇到了invocationTargetException的问题,在这里跟大家分享一下. 报错信息如下: 使用反射时,比如执行invoke方法,如果被反射执行的方法体抛出了Exception,这 ...
- Jboss提示:Server already running on localhost
最近在做项目中,经常遇到JBoss报如下提示:Server already running on localhost.这时Jboss显示已启动,但页面显示不出来.提示中给出了两种解决办法,运行新的服务 ...
- ui-grid表格怎么实现内容居中
这次是思想落后了,只关注怎么使用原生的ui-grid样式来实现这一需求,后来发现可以通过此列的cellTemplate来为列指定内容,从而可以使用css调整样式. ps:其实有时候换种思路,豁然开朗. ...