题目:

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.

链接https://leetcode.com/problems/longest-substring-without-repeating-characters/

答案:

参考了这个链接后才明白解题思路,然后依据自己的理解写了代码。

大致思路是:从当前字符开始,往前看,没有出现过重复字符的字符串

代码:

 #define MAX_LETTER_NUM 255
class Solution {
public:
int lengthOfLongestSubstring(string s) {
if(s.empty())
{
return ;
} bool exist[MAX_LETTER_NUM];
int position[MAX_LETTER_NUM];
int index,rangeIndex;
int maxLen = ;
int start = ; for(index = ; index < MAX_LETTER_NUM; ++ index)
{
exist[index] = false;
position[index] = ;
} for(index = ; index < s.size(); ++ index)
{
if(exist[s[index]])
{
for(rangeIndex = start; rangeIndex <= position[s[index]]; ++ rangeIndex)
{
exist[s[rangeIndex]] = false;
}
start = position[s[index]] + ;
exist[s[index]] = true;
}else
{
exist[s[index]] = true;
maxLen = (maxLen < index - start + ) ? index - start + :maxLen;
} position[s[index]] = index;
} return maxLen;
}
};

leetcode-【中等题】3. Longest Substring Without Repeating Characters的更多相关文章

  1. 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters

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

  2. 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)

    Given a string, find the length of the longest substring without repeating characters. Example 1:    ...

  3. Leetcode第三题《Longest Substring Without Repeating Characters》

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

  4. 【LeetCode】3 、Longest Substring Without Repeating Characters

    题目等级:Medium 题目描述:   Given a string, find the length of the longest substring without repeating chara ...

  5. Leetcode经典试题:Longest Substring Without Repeating Characters解析

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

  6. LeetCode解题笔记 - 3. Longest Substring Without Repeating Characters

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

  7. LeetCode(3)Longest Substring Without Repeating Characters

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

  8. 刷题3. Longest Substring Without Repeating Characters

    一.题目 Longest Substring Without Repeating Characters,具体请自行搜索. 这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂. 但要做到bug ...

  9. LeetCode Hash Table 3. Longest Substring Without Repeating Characters

    HashMap的应用可以提高查找的速度,键key,值value的使用拜托了传统数组的遍历查找方式,对于判断一个字符或者字符串是否已经存在的问题可以非常好的解决.而本题需要解决的问题就是判断新遍历到的字 ...

  10. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

随机推荐

  1. Android 的 DatePicker、TimePicker或NumberPicker

    布局文件加上这个就可以,去除日期选择器.时间选择器或数值选择器的可编辑状态. android:descendantFocusability="blocksDescendants" ...

  2. 【转载】让你的MATLAB代码飞起来

    原文地址:http://developer.51cto.com/art/201104/255128_all.htm MATLAB语言是一种被称为是"演算纸"式的语言,因此追求的是方 ...

  3. (译) Conditional Variational Autoencoders 条件式变换自编码机

    Conditional Variational Autoencoders --- 条件式变换自编码机 Goal of a Variational Autoencoder: 一个 VAE(variati ...

  4. C++语法

    http://stackoverflow.com/questions/4269034/what-is-the-meaning-of-prepended-double-colon

  5. OPENVPN

    安装 方法1--源码安装yum -y install  pam pam-devel gcc gcc-c++ lzo lzo-devel openssl openssl-devel wget autom ...

  6. Changing SID Server 2012

    Changing SID Server 2012  Windows Server > Windows Server 2012 General Question 0 Sign in to vote ...

  7. D3树状图异步按需加载数据

    D3.js这个绘图工具,功能强大不必多说,完全一个Data Driven Document的绘图工具,用户可以按照自己的数据以及希望实现的图形,随心所欲的绘图. 图形绘制,D3默认采用的是异步加载,但 ...

  8. [2014.01.27]wfGifAnimator 动画GIF组件 3.0

    组件支持设置GIF帧延时和获取GIF的帧延迟. 组件支持添加或插入或更新帧(支持bmp/jpg/gif/wmf/emf/ico格式).删除帧.清空帧操作. 组件支持GIF动画缩放大小. 组件支持绘制线 ...

  9. 【SFTP】使用Jsch实现Sftp文件上传-支持断点续传和进程监控

    JSch是Java Secure Channel的缩写.JSch是一个SSH2的纯Java实现.它允许你连接到一个SSH服务器,并且可以使用端口转发,X11转发,文件传输等,当然你也可以集成它的功能到 ...

  10. android学习笔记57——电话管理器TelephoneyManager

    电话管理器TelephoneyManager