LeetCode 3
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的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- Spring入门(1)-第一个Spring项目
1. 创建maven项目,maven相关配置如下: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi= ...
- 在Android4.0中Contacts拨号盘界面剖析(源码)
通过在 ViewPager 的适配器对象中,发现过一下三行代码 private DialpadFragment mDialpadFragment; private CallLogFragment ...
- easyUI之tree
转自:http://www.cnblogs.com/CoreCaiNiao/archive/2010/08/20/1804387.html http://blog.csdn.net/l27775918 ...
- 【LINUX】编程笔记
a storage class can only be specified for objects and functions extern修饰一个struct报错,错误原因如上,C++中存储类修 ...
- 多线程访问winform控件出现异常的解决方法
一. 多线程访问winform控件出现异常的解决方法 1. 问题描述<1> 如果创建某控件的线程之外的其他线程试图调用该控件,则会引发一个 InvalidOperationExcept ...
- stm32上的Lava虚拟机开发进度汇报(4)
这段时间颓废了,基本上没在弄这个东西. 主要是因为前段时间把代码基本上写完之后,但是问题一大堆,除了自己写的几个简单测试程序,其他现成的东西没有一个是能正常运行的,很是郁闷,也不知道哪里有错误,所以放 ...
- DynamoDB Local for Desktop Development
Would you like to be able to write and test code that uses the Amazon DynamoDB API even if you have ...
- cdoj 1250 喵哈哈的矩阵 数学题
喵哈哈的矩阵 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1250 Desc ...
- Codeforces Round #116 (Div. 2, ACM-ICPC Rules) C. Letter 暴力
C. Letter Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/180/problem/C D ...
- iOS开发技巧系列---详解KVC(我告诉你KVC的一切)
KVC(Key-value coding)键值编码,单看这个名字可能不太好理解.其实翻译一下就很简单了,就是指iOS的开发中,可以允许开发者通过Key名直接访问对象的属性,或者给对象的属性赋值.而不需 ...