题目:

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.

对于这个题目我的理解就是对一个string求最长的子串(里面的字母不能重复)。

这道题目是一道字符串的题目,首先就是应该复习一下c++中字符串string类的用法,下面是一个博客中的示范使用c++的代码,我抄录了下来,https://www.cnblogs.com/engraver-lxw/p/7581540.html为作者网页:

#include<iostream>
#include<string>
using namespace std; int main()
{
string str1 = "hello";
string* str2 = new string("hello");
string str3 = "world"; //获取字符串长度
int length = str1.length();
cout << "调用str.length()函数获取字符串长度:" << length << endl;
cout << endl; //字符串连接
string str4 = str1 + str3;
cout << "字符串连接结果:" << str4 << endl;
cout << endl; //字符串比较
if (str1 < str3)
cout << "字符串比较:" << "str1<str2" << endl;
cout << endl; //获取字符串的第一个字符
string::const_iterator it = str1.begin();
cout << *it << endl;
cout << endl; //获取字符串的最后一个字符
it = str1.end();//end是指向最后一个字符后面的元素,而且不能输出,所以cout << *it << endl;这样输出会报错
it--;
cout << *it << endl;
cout << endl; //倒置串
reverse(str1.begin(), str1.end());
cout << "倒置串:" << str1 << endl;
cout << endl; //字符串转字符数组
//不推荐的用法,但是需要了解
string a = "abc123";
const char *b;//这里必须为const char *,不能用char *,不然下一句会报错
b = a.c_str();
cout << "a:" << a << endl;
cout << "b:" << b << endl;
a = "asd456";
cout << "a:" << a << endl;
cout << "b:" << b << endl;
//推荐用法
string c = "abc123";
char *d = new char[];
strcpy(d, c.c_str());//因为这里没有直接赋值,所以指针类型可以不用const char *
cout << "c:" << c << endl;
cout << "d:" << d << endl;
c = "asd456";
cout << "c:" << c << endl;
cout << "d:" << d << endl;
cout << endl; //查找串
//find-从指定位置起向后查找,直到串尾
string st1("babbabab");
cout << st1.find('a') << endl;//1,默认从位置0(即第1个字符)开始查找
cout << st1.find('a', ) << endl;//4 在st1中,从位置2(b,包括位置2)开始,查找a,返回首次匹配的位置
cout << (st1.find('c', ) == -) << endl;//1
cout << (st1.find('c', ) == ) << endl;//1 两句均输出1,原因是计算机中-1和4294967295都表示为32个1(二进制)
string st2("aabcbcabcbabcc");
str1 = "abc";
cout << st2.find(str1, ) << endl;//6,从st2的位置2(b)开始匹配,返回第一次成功匹配时匹配的串(abc)的首字符在st2中的位置,失败返回-1
cout << st2.find("abcdefg", , ) << endl;//6 取abcdefg得前3个字符(abc)参与匹配,相当于st2.find("abc", 2) //rfind-从指定位置起向前查找,直到串首
cout << st1.rfind('a', ) << endl;//6 //find_first_of-在源串中从位置pos起往后查找,只要在源串中遇到一个字符,该字符与目标串中任意一个字符相同,就停止查找,返回该字符在源串中的位置;若匹配失败,返回-1
string str6("bcgjhikl");
string str7("kghlj");
cout << str6.find_first_of(str7, ) << endl;//2,从str1的第0个字符b开始找,g与str2中的g匹配,停止查找,返回g在str1中的位置2 //find_last_of-与find_first_of函数相似,只不过查找顺序是从指定位置向前
string str("abcdecg");
cout << str.find_last_of("hjlywkcipn", ) << endl;//5,从str的位置6(g)开始向前找,g不匹配,再找c,c匹配,停止查找,返回c在str中的位置5 //find_first_not_of-在源串中从位置pos开始往后查找,只要在源串遇到一个字符,与目标串中的任意字符都不相同,就停止查找,返回该字符在源串中的位置;若遍历完整个源串,都找不到满足条件的字符,则返回-1
cout << str.find_first_not_of("kiajbvehfgmlc", ) << endl;//3 从源串str的位置0(a)开始查找,目标串中有a,匹配,..,找d,目标串中没有d(不匹配),停止查找,返回d在str中的位置3 //find_last_not_of-与find_first_not_of相似,只不过查找顺序是从指定位置向前
cout << str.find_last_not_of("kiajbvehfgmlc", ) << endl;// system("pause");
return ; }

用这个很自然的思路,当然也很自然的gg了,其实样例倒是可以过,不过当时的想法只是粗略的

把他们都当作是一定是小写字母了,没有仔细的思考。

先把这份错误的代码贴出来:

 class Solution {
public:
int lengthOfLongestSubstring(string s) {
int used[]={};
string::iterator it=s.begin();
string::iterator itForRequest=s.end();
itForRequest--;
int ans=;
for(;it<=itForRequest;it++)
{
string::iterator temp=it;
int sum=;
for(int i=;i<=;i++)
{
used[i]=;
}
while(true)
{
if(temp>itForRequest) break;
if(used[*temp-'a']==)
{
used[*temp-'a']++;
temp++;
sum++;
}
else
{
if(sum>ans) ans=sum;
break;
}
}
}
return ans;
}
};

当然后来发现这个方法也没毛病,完全可行,毕竟ASII码表也就不到一百个数字,我把每个数字

搞一个数组元素就完全没问题了,当然也发现了前面代码的一点小bug主要就是在之前代码的20

行的判断内部应该加上sum>ans的判断,否则如果只有单个字母就会导致边界条件的错误,现在

把正确代码贴上来:

 class Solution {
public:
int lengthOfLongestSubstring(string s) {
int used[]={};
string::iterator it=s.begin();
string::iterator itForRequest=s.end();
itForRequest--;
int ans=;
for(;it<=itForRequest;it++)
{
string::iterator temp=it;
int sum=;
for(int i=;i<=;i++)
{
used[i]=;
}
while(true)
{
if(temp>itForRequest) break;
if(used[*temp]==)
{
used[*temp]++;
temp++;
sum++;
if(sum>ans) ans=sum;
}
else
{
if(sum>ans) ans=sum;
break;
}
}
}
return ans;
}
};

leetcode题解 3. Longest Substring Without Repeating Characters的更多相关文章

  1. 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  2. 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...

  3. 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters

    一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...

  4. 【LeetCode OJ】Longest Substring Without Repeating Characters

    题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...

  5. 【LeetCode】003. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  6. 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串

    题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...

  7. 【LeetCode】3. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  8. 【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)

    Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...

  9. LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)

    Given a string, find the length of the longest substring without repeating characters. For example, ...

随机推荐

  1. 20190404用户及用户组管理(week1_day4)

    useradd userdel usermod groupadd groupdel 用户管理 为什么需要有用户? 1. linux是一个多用户系统 2. 权限管理(权限最小化) 用户:存在的目录是为了 ...

  2. Vue基础进阶 之 列表过渡

    在前面的博客我们一直在操作单个元素的过渡,如果是对多个元素过渡,例如列表,这时就要用到<transition-group>这个组件了: 如何使用:将要操作的列表元素放在<transi ...

  3. P2472 [SCOI2007]蜥蜴(网络流)

    P2472 [SCOI2007]蜥蜴 把每个点拆成2个点,两点之间连边的边权为石柱高度 新建虚拟源点$S$和汇点$T$ $S$向所有有蜥蜴的点连边,边权1 其他边都连$inf$ 剩下就是裸的$dini ...

  4. 使用docker部署ambari的若干要点

    ambari部署各个组件 使用ambari进行部署时主要需要的组件包括: ambari-server: 主要部署的控制节点,负责控制agent进行部署. mysql: server存储的数据库.也支持 ...

  5. 剑指offer(55)链表中环的入口节点

    题目描述 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 题目分析 1.一快一慢指针,先找到碰撞点. 2.然后碰撞点到入口节点的距离就是头结点到入口节点的距离. 具体原理可 ...

  6. Learning-Python【18】:Python常用模块(1)—— time、datetime、randrom

    time 模块:与时间相关的功能的模块 在 Python 中,时间分为三种: 1.时间戳:是一个时间的表示,根据不同的语言,可以是整数或浮点数,是从1970年1月1日0时0分0秒到现在经历的秒数 2. ...

  7. node:express:error---填坑之路

    express版本4.0之后需要安装的东西 npm install -g express npm install -g express-generator jade转换成ejs(修改为html引擎,打 ...

  8. js同步、异步、回调的执行顺序以及闭包的理解

    首先,记住同步第一.异步第二.回调最末的口诀 公式表达:同步=>异步=>回调 看一道经典的面试题: for (var i = 0; i < 5; i++) { setTimeout( ...

  9. redhat7.2安全基线BI

    (一)   Redhat linux7.2安全基线基本型(BI) 1.   密码复杂度策略 /etc/pam.d/system-auth文件中,增加内容 password requisite pam_ ...

  10. 利用iframe实现无刷新提交

    服务器里边: