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 ...
随机推荐
- C# 6.0 的新特性
1. 自动的属性初始化器Auto Property initialzier 之前的方式: public class AutoPropertyBeforeCsharp6 { private string ...
- SSH与SSL
1. SSL SSH 即Secure Shell,它主要由三部分组成: 第一部分:连接协议 [SSH-CONNECT] 将多个加密隧道分成逻辑通道.它运行在用户认证协议上.它提供了交互式登录话路.远程 ...
- Java邮件服务学习之一:邮件服务概述
java可以提供邮件服务:一般理解的邮件服务就是可以发送和接收邮件的客户端,另外就是使用java编写邮件服务端:两者区别在于客户端只负责给终端客户收发邮件,就相当于小区楼下的那一排排的铁皮邮箱盒,而邮 ...
- UVaLive 7362 Farey (数学,欧拉函数)
题意:给定一个数 n,问你0<= a <=n, 0 <= b <= n,有多少个不同的最简分数. 析:这是一个欧拉函数题,由于当时背不过模板,又不让看书,我就暴力了一下,竟然A ...
- 编译小结(6)认识Automake
我前面说了很多如何用gcc或 Makefile怎么编译的东东,但在Linux下装过软件的都应当见过,很多源码安装的包是用Automake 来编译的.输入下"./configur ...
- android开发教程(八)——环境搭建之java-ndk
目录 android ndk是android用于开发本地代码的开发工具包.它提供C/C++交叉编译工具.android内核.驱动.已有的C/C++代码,都需要ndk来支持开发. 目前支持以下平台:ar ...
- UML学习(二)-----类图
UML学习(二)-----类图 http://www.cnblogs.com/silent2012/archive/2011/09/07/2169946.html http://www.cnblogs ...
- HTML第一天学习笔记
- VIM复制粘贴大全!
原文地址:http://lsong17.spaces.live.com/blog/cns!556C21919D77FB59!603.entry 内容: 用vim这么久 了,始终也不知道怎么在vim中使 ...
- VHD_Update_diskpart
###################功能说明########################该脚本用来对离线VHD文件更新,导入系统补丁############################### ...