《剑指offer》第四十八题(最长不含重复字符的子字符串)
// 面试题48:最长不含重复字符的子字符串
// 题目:请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子
// 字符串的长度。假设字符串中只包含从'a'到'z'的字符。 #include <string>
#include <iostream> // 方法一:蛮力法
//不想说话 // 方法一:动态规划
int longestSubstringWithoutDuplication_2(const std::string& str)
{
int curLength = ;//记录当前长度
int maxLength = ;//记录最大长度 int* position = new int[];//检测26个字母上次出现在字符串下标,没有用过为-1
for (int i = ; i < ; ++i)
position[i] = -; for (int i = ; i < str.length(); ++i)
{
int prevIndex = position[str[i] - 'a'];
if (prevIndex < || i - prevIndex > curLength)//如果这个值之前没有被用过,或者用过但是二者相同字符的距离比当前记录的长度要大(这说明这个字符不再当前统计的范围内)
++curLength;
else
{
if (curLength > maxLength)
maxLength = curLength; curLength = i - prevIndex;//否则将二者相同字符的距离替换当前记录的长度
}
position[str[i] - 'a'] = i;//记录这个字符的位置为i
} if (curLength > maxLength)//记录到结束了,要比较一下最后一段的长度是否是最长的
maxLength = curLength; delete[] position;
return maxLength;
} // ====================测试代码====================
void testSolution2(const std::string& input, int expected)
{
int output = longestSubstringWithoutDuplication_2(input);
if (output == expected)
std::cout << "Solution 2 passed, with input: " << input << std::endl;
else
std::cout << "Solution 2 FAILED, with input: " << input << std::endl;
} void test(const std::string& input, int expected)
{
testSolution2(input, expected);
} void test1()
{
const std::string input = "abcacfrar";
int expected = ;
test(input, expected);
} void test2()
{
const std::string input = "acfrarabc";
int expected = ;
test(input, expected);
} void test3()
{
const std::string input = "arabcacfr";
int expected = ;
test(input, expected);
} void test4()
{
const std::string input = "aaaa";
int expected = ;
test(input, expected);
} void test5()
{
const std::string input = "abcdefg";
int expected = ;
test(input, expected);
} void test6()
{
const std::string input = "aaabbbccc";
int expected = ;
test(input, expected);
} void test7()
{
const std::string input = "abcdcba";
int expected = ;
test(input, expected);
} void test8()
{
const std::string input = "abcdaef";
int expected = ;
test(input, expected);
} void test9()
{
const std::string input = "a";
int expected = ;
test(input, expected);
} void test10()
{
const std::string input = "";
int expected = ;
test(input, expected);
} int main(int argc, char* argv[])
{
test1();
test2();
test3();
test4();
test5();
test6();
test7();
test8();
test9();
test10();
system("pause");
return ;
}
《剑指offer》第四十八题(最长不含重复字符的子字符串)的更多相关文章
- 《剑指offer》第十八题(删除链表中重复的结点)
// 面试题18(二):删除链表中重复的结点 // 题目:在一个排序的链表中,如何删除重复的结点?例如,在图3.4(a)中重复 // 结点被删除之后,链表如图3.4(b)所示. #include &l ...
- 剑指 Offer 48. 最长不含重复字符的子字符串 + 动态规划 + 哈希表 + 双指针 + 滑动窗口
剑指 Offer 48. 最长不含重复字符的子字符串 Offer_48 题目详情 解法分析 解法一:动态规划+哈希表 package com.walegarrett.offer; /** * @Aut ...
- 【Java】 剑指offer(48) 最长不含重复字符的子字符串
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字 ...
- 【Offer】[48] 【最长不含重复字符的子字符串】
题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度.假设字符串中只包含'a'~'z'的字符.例如,在字符串&q ...
- 剑指offer面试题48: 最长不含重复字符的子字符串
Given a string, find the length of the longest substring without repeating characters.(请从子字符串中找出一个最长 ...
- [剑指Offer]48-最长不含重复字符的子字符串(递归思想,循环实现)
题意 如题,字符串只含a-z,输出该子串长度.例:"arabcacfr",输出4. 解题思路 递归思想 计f(i)为以第i个字符结尾的最长不含重复字符的子串长度. 状态转移:计d为 ...
- 《剑指offer》第二十八题(对称的二叉树)
// 面试题28:对称的二叉树 // 题目:请实现一个函数,用来判断一棵二叉树是不是对称的.如果一棵二叉树和 // 它的镜像一样,那么它是对称的. #include <iostream> ...
- 《剑指offer》第十八题(在O(1)时间删除链表结点)
// 面试题18(一):在O(1)时间删除链表结点 // 题目:给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该 // 结点. #include <iostream> ...
- 每日一题 - 剑指 Offer 48. 最长不含重复字符的子字符串
题目信息 时间: 2019-07-02 题目链接:Leetcode tag: 动态规划 哈希表 难易程度:中等 题目描述: 请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度 ...
随机推荐
- Eloquent JavaScript #04# Objects and Arrays
要点索引: JSON More ... 练习 1.补:js字符串的表达方式有三种: "" 和 '' 没什么区别,唯一区别在于 "" 中写 "要转义字符 ...
- Dubbox分布式框架之入门
Dubbox简介 Dubbox是一个分布式服务框架,其前身是阿里巴巴开源项目Dubbox,被国内电商及换联网项目中使用,后期阿里巴巴停止了该项目的维护,当当网便在Dubbo基础上进行优化,并继续维护, ...
- django 函数装饰器 变为 类装饰器
aaa
- js 实现几分钟前、几小时前、几天前,以及几分钟后、几小时后、几天前后
js 实现几分钟前.几小时前.几天前,以及几分钟后.几小时后.几天前后 /* * * 把传入的时间戳与当前时间比较,计算几分钟前.几小时前.几天前,以及几分钟后.几小时后.几天前后 * unixtim ...
- css overflow和float
float:使元素向左或向右移动(不能上下移动),直到它的外边缘碰到包含框或另一个浮动框的边框为止,浮动元素之前的元素将不会受到影响,之后的元素将围绕它. float之后的元素脱离文档流. 默认为no ...
- Hashtable与HashMap的区别
HashMap不是线程安全的,HashTable是线程安全. HashMap允许空(null)的键和值(key),HashTable则不允许. HashMap性能优于Hashtable.
- 错误 1 error LNK2019: 无法解析的外部符号 __imp__pthread_create,该符号在函数 _main 中被引用 解决方法
晚上花几分钟在windows下测了下pthread的用法,出现错误 1 error LNK2019: 无法解析的外部符号 __imp__pthread_create,该符号在函数 _main 中被引用 ...
- you must restart adb and eclipse的相关解决办法
问题是5037端口被占用: C:\>netstat -aon|findstr "5037" 看到了吗,端口被进程号为5037的进程占用,继续执行下面命令(也可以去任务管理器中 ...
- cf水题
题意:输入多组数据,有的数据代表硬币的长宽,有的数据代表钱包的长宽,问你当这组数据代表钱包的长宽时,能不能把它前面出现的所有硬币全部装下. 思路:只要钱包的长宽大于前面出现的所有硬币的长宽就可以装下, ...
- 网络存储结构简明分析—DAS、NAS和SAN 三者区别
存储的总体分类 主流存储结构 网络存储结构大致分为三种:直连式存储(DAS:Direct Attached Storage).存储区域网络(SAN:Storage Area Network ...