《剑指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: 动态规划 哈希表 难易程度:中等 题目描述: 请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度 ...
随机推荐
- kivy Properties
Introduction to Properties¶ Properties are an awesome way to define events and bind to them. Essenti ...
- Windbg解决系统蓝屏
win10企业版连续两天遭遇系统蓝屏, 今天就各种检查,准备好好地研究一下这个问题,以下是整个过程: 首先,找到系统蓝屏时的错误日志: [计算机] --> [管理] --> [系统工具] ...
- 标准库 string
1.func Fields(s string) []string,这个函数的作用是按照1:n个空格来分割字符串最后返回的是[]string的切片 package main import ( " ...
- oracle 18c的版本号规则
18C之后的版本标识 从2017年7月开始,Oracle改变了以往的数据库软件发布流程,采用年度Release和季度更新的策略. Yearly Release 将之前的N年一发布更改为每年一发布.每年 ...
- vue 起步
vue 官网 目前最火的前端框架当属Vue.js了,很多使用过vue的程序员这样评价它,“vue.js兼具angular.js和react.js的优点,并剔除了它们的缺点”.授予了这么高的评价的vue ...
- docker 实践
https://doc.yonyoucloud.com/doc/docker_practice/etcd/etcdctl.html 启动http restful API docker批量映射端口 怎么 ...
- springboot 事务回滚
在springboot中,使用事务回滚时,添加@Transactional注解,然后在try-catch块中,发生异常时,在catch中 添加 TransactionAspectSupport.cur ...
- Office 2016 永久激活
启示:office突然过期,QWQ,卖电脑的真坑爹,找了好多办法,总结2个不花钱的办法啦. 1>只有30天试用期 Office 2016预览版序列号:NKGG6-WBPCC-HXWMY-6DQG ...
- 记录结果再利用的"动态规划"之背包问题
参考<挑战程序设计竞赛>p51 https://www.cnblogs.com/Ymir-TaoMee/p/9419377.html 01背包问题 问题描述:有n个重量和价值分别为wi.v ...
- NSIS+Duilib 制作Windows安装包
转载:https://www.cnblogs.com/zzllily/articles/5443850.html 转载:https://blog.csdn.net/bruce135lee/articl ...