Longest Substring Without Repeating Characters(C语言实现)
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/
题目意思:给出一个字符串,输出最长的子串的长度,子串不能有重复的字符。(子串是连续的。)
解题思路: 依次读取字符串的每一个字符,如果第一次出现则当前子串长度+1,否则:首先判断当前长度是否大于最大长度,是则替换最大长度。然后
查找重复的字符是在原字符串哪里出现的。
代码如下:
int lengthOfLongestSubstring(char* s) {
,currlen = ;
], i, j, start = ;
memset(table, , sizeof(table));
; s[i] != '\0'; ++i){
){
if (currlen > maxlen){
maxlen = currlen;
}
for(j = start; j < i; ++j){ //start记录重复的字符后一个位置
if (s[j] == s[i]){
table[s[j]] = ;
start = j+;
break;
}else{
--currlen;
table[s[j]] = ;
}
}
}else{
++currlen;
}
}
if (currlen > maxlen){
maxlen = currlen;
}
return maxlen;
}

Longest Substring Without Repeating Characters(C语言实现)的更多相关文章
- leetcode: longest substring without repeating characters
July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...
- leetcode 3 Longest Substring Without Repeating Characters最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- LeetCode——Problem3:Longest Substring Without Repeating Characters
哎哟我天啊.这道题快折磨死我了.刚开始连题都没看明白,就是不知道substring是什么意思.研究了好长时间才看出来的. 光辉历史呀...菜死了 1.题目 Given a string, find t ...
- Leetcode 3. Longest Substring Without Repeating Characters(string 用法 水题)
3. Longest Substring Without Repeating Characters Medium Given a string, find the length of the long ...
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 3. Longest Substring Without Repeating Characters(c++) 15ms
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
随机推荐
- 2016苹果春季发布会 iPhone SE发布
配置如下 主屏尺寸:4英寸 主屏分辨率:1336x640像素 后置摄像头:1200万像素 前置摄像头:120万像素 电池容量:1624mAh 核心数:双核 操作系统:iOS 9 核心数:双核 CPU: ...
- Java 浅析三大特性之一多态
Java 浅析三大特性之一多态 之前我们的文章讲了Java的封装和继承,封装讲的时候,并没有体现出来封装的强大之处,反而还要慎用封装.因为这时的封装还没有和多态联系到一起,还无法看出向上转型的厉害之处 ...
- css垂直居中那点事
这是我技术博客生涯的第一篇文章,想想还是有点小鸡冻...菜鸟的征程现在要开始了 学习css的时候经常被各种问题纠结到不要不要的,没办法,只能写写博客帮助整理一下自己的思绪和帮助一下和我遇到同样问题的小 ...
- Hadoop学习笔记——搭建
一搭建环境列表 操作系统:centos6.5 64位 JDK环境:jdk1.7.0_71 hadoop版本:社区版本2.7.2,hadoop-2.7.2-src.tar.gz 主机名 ip 角色 用户 ...
- Juint整合Log4j
一般Log4j配置在web.xml中,在单元测试时,不需要启动Tomcat,所有Log4j找不到配置文件 在测试类中手动加载 配置文件 PropertyConfigurator.configure(& ...
- Java 抽象类的理解
1. 基本概念 用abstract修饰的类是抽象类.如果类中有方法是abstract类型的,那么此类肯定是abstract类型的,也就是说此类的修饰符肯定有abstract(也就是说,有抽象方法的类是 ...
- 利用免费cdn加速webpack单页应用
回顾现状 在之前的学习过程中,react单页应用经过webpack打包之后会输出大概如下的目录结构,它就是站点的所有前端组成了: 1 2 3 4 5 6 MacBook-Pro:output ba ...
- Razor视图出现重复的解决方法
- Sql Server系列:Transact-SQL变量
变量是Transact-SQL中由用户定义.可对其赋值并参与运算的一个实体,分为全局变量和局部变量.其中全局变量由系统自定义并维护,全局变量名称前面有@@字符,任何程序均可随时调用.局部变量名称前面有 ...
- 让hammer完美支持Pixi.js - 2D webG库
由于项目改造,采用2D webG的pixi库,那么基于canvas的结构上,事件就是最大的一个问题了 改造的原理很简单,把hammer里面的addEventListeners事件绑定给第三方库代替,事 ...