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 ...
随机推荐
- HDU 4791 Alice's Print Service (2013长沙现场赛,二分)
Alice's Print Service Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- codeforces Ebony and Ivory(水题)
A. Ebony and Ivory time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- [iOS UI进阶 - 3.0] 触摸事件的基本处理
A.需要掌握和练习的 1.介绍事件类型2.通过按钮的事件处理引出view的事件处理3.响应者对象 --> UIResponder --> UIView4.view的拖拽* 实现触摸方法,打 ...
- 自定义使用AVCaptureSession 拍照,摄像,载图
转载自 http://blog.csdn.net/andy_jiangbin/article/details/19823333 拍照,摄像,载图总结 1 建立Session 2 添加 input ...
- 使用Jena RDF API 开发脚本语言管理资源描述框架模型
摘要 资源描述框架(Resource Description Framework RDF)是一种以XML格式描述元数据的标准格式.Jena是一种用于将关系数据库或是文本文件中所表示的数据建立为元数据模 ...
- 关于三目运算符与if语句的效率与洛谷P2704题解
题目描述 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用“H” 表示),也可能是平原(用“P”表示),如下图.在每一格平原地形上最 ...
- CSS 边框的宽度
边框的宽度 您可以通过 border-width 属性为边框指定宽度. 为边框指定宽度有两种方法:可以指定长度值,比如 2px 或 0.1em:或者使用 3 个关键字之一,它们分别是 thin .me ...
- Js面向对象和数据类型内存分配(转)
一 Js基本数据类型以及内存情况 1 Undefined Undefined类型只有一个值undefined,在使用了声明但未初始化的变量的时候,这个变量值就是undefined 1 var hi; ...
- php 基本符号
用这么久了,竟然PHP的基本符号都没有认全,看到@号还查了半天才知道什么意思.把基本符号列表帖一下吧,需要的朋友可以参考~ 注解符号: // 单行注解 /* ...
- Nltest
查看登陆到的DC: