Longest Substring Without Repeating Characters

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.

 /*************************************************************************
> File Name: LeetCode003.c
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: Tue 17 May 2016 17:10:02 PM CST
************************************************************************/ /************************************************************************* Longest Substring Without Repeating Characters 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. ************************************************************************/ #include "stdio.h" /* 8ms */
int lengthOfLongestSubstring(char* s)
{
int n = strlen(s); if( n <= )
{
return n;
} int i = , j = ;
int maxLen = ;
int exist[] = { };
while( j < n )
{
if( exist[s[j]] )
{
maxLen = maxLen>(j-i) ? maxLen : (j-i);
while( s[i] != s[j] )
{
exist[s[i]] = ;
i++;
}
i++;
j++;
}
else
{
exist[s[j]] = ;
j++;
}
}
maxLen = maxLen>(n-i) ? maxLen : (n-i);
return maxLen;
} /* 36ms */
int lengthOfLongestSubstring2(char* s)
{
int len = ;
int p= , q=, i; for( q=; q<strlen(s); q++ )
{
for( i=p; i<q; i++ )
{
if( s[q] == s[i] )
{
if( q-p > len ) len = q-p;
p = i+;
}
}
}
len = len>q-p?len:q-p;
return len;
} int main()
{
char* s = "abccab";
int ret = lengthOfLongestSubstring(s);
printf("%d\n", ret); return ;
}

LeetCode 3的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  10. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. 你想成为优秀的Java程序员吗?

    Java是全世界最受欢迎的3大编程语言之一,它可以开发出许多实用的WEB应用程序和桌面应用程序,更重要的一点,Java是跨平台的语言——编写一次,可以再任何地方运行.另外,Java也很容易入门,如果你 ...

  2. 第二百八十九天 how can I 坚持

    今天好伤啊,太把自己当回事了. 现在在弟弟这,下午和他一块看了看西客站附近的房子,感觉暂时好难,只是暂时的,一切都会好起来的. 弟弟上班也挺不容易,不该来给他添麻烦,替他心疼. 确实不知道该咋办了,好 ...

  3. 采用现代Objective-C

    多年来,Objective-C语言已经有了革命性的发展.虽然核心理念和实践保持不变,但语言中的部分内容经历了重大的变化和改进.现代化的Objective-C在类型安全.内存管理.性能.和其他方面都得到 ...

  4. 回车跳到下一个EDIT

    1.按下方法procedure TForm2.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);begin if Key ...

  5. HDU 3416 Marriage Match IV (求最短路的条数,最大流)

    Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...

  6. Edit Box多行显示时如何使滚动条始终在下方

    两种方法: ①  CEdit *pEdit = ((CEdit*)GetDlgItem(IDC_EDIT_RXDATA)); pEdit->LineScroll(pEdit->GetLin ...

  7. fixed 定位 苹果手机输入框触发时内容全部隐藏

    问题出现在东钿微信公众号用户注册页面 页面中只有两个输入框 页面没有超过一屏,悬浮按钮也要出现在本页面 ,开始布局页面的时候没什么问题,然后我在我自己手机上测试 ,输入手机号码,非常奇怪的问题出现了, ...

  8. ObjC-观察者模式

    观察者模式是设计模式的一种,又称为发布者/订阅者模式,其定义了一种一对多的关系,多个观察者可以监听一个对象.当该对象的状态发生改变时,会通知所有的观察者,观察者会自己进行更新. 观察者模式能够将观察者 ...

  9. 利用procdump+Mimikatz 绕过杀软获取Windows明文密码(转)

    Mimikatz现在已经内置在Metasploit’s meterpreter里面,我们可以通过meterpreter下载.但是你如果觉得还要考虑杀毒软件,绑定payload之类的东西太过复杂,我们可 ...

  10. Codeforces Round #334 (Div. 2) B. More Cowbell 二分

    B. More Cowbell Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/604/probl ...