// 面试题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. php 采集爬取单个淘宝商品描述,商品属性

    下载链接:https://download.csdn.net/download/a724008158/10723448 效果图:

  2. 一键PHP/JAVA安装工具

    OneinStack是一键PHP/JAVA安装脚本工具,包含lnmp,lamp,lnmpa,ltmp,lnmh,MySQL,PostgreSQL,MongoDB等 建议使用 PHP7.1+MYSQL5 ...

  3. Golang指针基本介绍及使用案例

    一.指针的相关概念说明 变量:是基本类型,变量存的就是值,也叫值类型 地址:用于引用计算机的内存地址,可理解为内存地址的标签,通俗一点讲就是一间房在小区里的门牌号.如下图① 指针:指针变量存的是一个地 ...

  4. Python3 格式化字符串

    Python3 格式化字符串 在Python 3.6之前,有两种将Python表达式嵌入到字符串文本中进行格式化的主要方法:%-formatting和str.format() 一.%-formatti ...

  5. windows 7/10 安装u盘制作

    今天,在拷贝数据时,发现那台丢在那两三年的pc密码忘了,故计划重装,因为从来都是公司信息中心管这事,至少都七八年没有自己装机了,故整理过程如下: 1.从itellyou.cn下载原版镜像: 2.准备一 ...

  6. 跟阿铭学Linux习题答案

    第一章:走进Linux 1.简述它的发展历史,列举几种代表性的发行版 Linux之前是Unix,由于Unix收费昂贵,so,Richard Stallman 发起了开发自由软件的运动,并成立了自由软件 ...

  7. Python3基础 dict 推导式 生成10以内+奇数的值为True 偶数为False的字典

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  8. P1337 [JSOI2004]平衡点 / 吊打XXX 模拟退火

    链接 https://www.luogu.org/problemnew/show/P1337 思路 交了好多发,都是wrong 初始值取平均数就1A了 真的是玄学的算法 代码 // luogu-jud ...

  9. Smartmontools硬盘检测工具

    安装: 下载 命令使用: 查看驱动器信息: $ smartctl -i D: smartctl 6.6 2017-11-05 r4594 [x86_64-w64-mingw32-w10-b17134] ...

  10. 逆波兰表达式|2013年蓝桥杯A组题解析第六题-fishers

    逆波兰表达式 正常的表达式称为中缀表达式,运算符在中间,主要是给人阅读的,机器求解并不方便. 例如:3 + 5 * (2 + 6) - 1 而且,常常需要用括号来改变运算次序. 相反,如果使用逆波兰表 ...