// 面试题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》第四十八题(最长不含重复字符的子字符串)的更多相关文章

  1. 《剑指offer》第十八题(删除链表中重复的结点)

    // 面试题18(二):删除链表中重复的结点 // 题目:在一个排序的链表中,如何删除重复的结点?例如,在图3.4(a)中重复 // 结点被删除之后,链表如图3.4(b)所示. #include &l ...

  2. 剑指 Offer 48. 最长不含重复字符的子字符串 + 动态规划 + 哈希表 + 双指针 + 滑动窗口

    剑指 Offer 48. 最长不含重复字符的子字符串 Offer_48 题目详情 解法分析 解法一:动态规划+哈希表 package com.walegarrett.offer; /** * @Aut ...

  3. 【Java】 剑指offer(48) 最长不含重复字符的子字符串

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字 ...

  4. 【Offer】[48] 【最长不含重复字符的子字符串】

    题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度.假设字符串中只包含'a'~'z'的字符.例如,在字符串&q ...

  5. 剑指offer面试题48: 最长不含重复字符的子字符串

    Given a string, find the length of the longest substring without repeating characters.(请从子字符串中找出一个最长 ...

  6. [剑指Offer]48-最长不含重复字符的子字符串(递归思想,循环实现)

    题意 如题,字符串只含a-z,输出该子串长度.例:"arabcacfr",输出4. 解题思路 递归思想 计f(i)为以第i个字符结尾的最长不含重复字符的子串长度. 状态转移:计d为 ...

  7. 《剑指offer》第二十八题(对称的二叉树)

    // 面试题28:对称的二叉树 // 题目:请实现一个函数,用来判断一棵二叉树是不是对称的.如果一棵二叉树和 // 它的镜像一样,那么它是对称的. #include <iostream> ...

  8. 《剑指offer》第十八题(在O(1)时间删除链表结点)

    // 面试题18(一):在O(1)时间删除链表结点 // 题目:给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该 // 结点. #include <iostream> ...

  9. 每日一题 - 剑指 Offer 48. 最长不含重复字符的子字符串

    题目信息 时间: 2019-07-02 题目链接:Leetcode tag: 动态规划 哈希表 难易程度:中等 题目描述: 请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度 ...

随机推荐

  1. Python For Android (P4a):添加权限(Permissions)

    from flutter study: <uses-permission android:name="android.permission.INTERNET"/>< ...

  2. Django模板系统 运算

    记下来,省得每次google 过滤器,变量的显示形式的改变 一.形式:小写 {{ name | lower }} 二.串联:先转义文本到HTML,再转换每行到 <p> 标签 {{ my_t ...

  3. linux 安装 ImageMagick 和 imagick 扩展

    使用命令安装 1.依次运行以下命令 yum install ImageMagick yum install ImageMagick-devel yum install php-pear 安装php-p ...

  4. Netty1

    基于Netty4的HttpServer和HttpClient的简单实现 Netty的主页:http://netty.io/index.html 使用的Netty的版本:netty-4.0.23.Fin ...

  5. fjwc2019 D1T1 全连(dp+树状数组)

    #178. 「2019冬令营提高组」全连 显然我们可以得出一个$O(n^2)$的dp方程 记$f(i)$为取到第$i$个音符时的最大分数,枚举下一个音符的位置$j$进行转移. 蓝后我们就可以用树状数组 ...

  6. golang json用法讲解

    简介 json格式可以算我们日常最常用的序列化格式之一了,Go语言作为一个由Google开发,号称互联网的C语言的语言,自然也对JSON格式支持很好.但是Go语言是个强类型语言,对格式要求极其严格而J ...

  7. 20145308 《网络对抗》 MAL_后门应用与实践 学习总结

    20145308 <网络对抗> MAL_后门应用与实践 学习总结 实践目的 使用nc实现win和Linux间的后门连接 meterpreter的应用 MSF POST的应用 知识点学习总结 ...

  8. hdu 1151 Air Raid - 二分匹配

    Consider a town where all the streets are one-way and each street leads from one intersection to ano ...

  9. Windows Shell编程实现重叠图标IconOverlay

    转载:https://www.codeproject.com/Articles/7484/How-to-overlay-an-icon-over-existing-shell-objects 转载:h ...

  10. [内核驱动] DOS路径转化为NT路径

    转载:http://blog.csdn.net/qq_33504040/article/details/78468278 最近在做一个文件过滤驱动程序,禁止访问指定目录或文件.想要从R3给R0发命令和 ...