// 面试题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. Java 安全套接字编程以及keytool 使用最佳实践

    概述 利用 Java 的 JSSE(Java Secure Socket Extension)技术,我们可以方便的编写安全套接字程序,关于 JSSE 的介绍,可以参阅 Oracle 网站提供的 JSS ...

  2. K8S学习笔记之Flannel解读

    0x00 概述 我们知道docker官方并没有提供多主机的容器通信方案,单机网络的模式主要有host,container,brige,none.none这种模式,顾名思义就是docker本身不去管理网 ...

  3. SpringMVC学习笔记1

    1:IntelliJ新建Maven工程 2:pom.xml文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...

  4. Angular 请求数据

    Angular 请求数据 get post 以及 jsonp 请求数据 引入 HttpModule .JsonpModule 普通的 HTTP 调用并不需要用到 JsonpModule,不过稍后我们就 ...

  5. Android之xml解析

    利用类下载器解析Xml文件要解析的xml文件<?xml version="1.0" encoding="utf-8"?><info> & ...

  6. 复选框批量删除操作-jquery方式

    1.首先在页面添加一个批量删除的按钮:<li class="btns"><input id="deleteSubmit" class=&quo ...

  7. HTTP协议和XMPP协议、MQTT协议

    应用层协议:每个应用层的都是为了解决某一类应用问题.而问题的解决又必须通过位于不同主机中的多个应用进程之间的通信和协同工作来完成.应用进程之间必须遵守严格的规则.应用层协议应当定义如下几个: 应用进程 ...

  8. maven 新建项目时报错“Could not calculate build plan: Failure to transfer org.apache.maven.plugins:。。。。。。。。。。。。。。”

    首先,我们看到观察这个错误:Failure to transfer org.apache.maven.plugins,这种错误是项目部署时,maven所关联的仓库中插件的设置出错了. 所以我们需要找到 ...

  9. [转] Java 基础

    1. 面向对象和面向过程的区别 面向过程 面向对象 2. Java 语言有哪些特点 3. 关于 JVM JDK 和 JRE 最详细通俗的解答 JVM JDK 和 JRE 4. Oracle JDK 和 ...

  10. Images之Dockerfile中的命令2

    COPY COPY has two forms: COPY [--chown=<user>:<group>] <src>... <dest> COPY ...