当前位置:网站首页>Leetcode skimming -- bit by bit record 022
Leetcode skimming -- bit by bit record 022
2022-07-23 10:44:00 【Robust least squares support vector machine】
22. The finger of the sword Offer 48. The longest substring without repeating characters
requirement
Please find the longest substring in the string that does not contain duplicate characters , Calculate the length of the longest substring .
Ideas
Dynamic programming :
- State definition : Set up a dynamic programming list dp ,dp[j] Represents with the character s[j] For the end of “ The longest non repeating substring ” The length of
- Transfer equation : Fixed right border j, Set character s[j] The same character nearest to the left is s[i], namely s[i]=s[j]
When dp[j - 1] < j - i , Description character s[i] In substring dp[j-1] Out of range , be dp[j] = dp[j - 1] + 1
When dp[j−1] ≥ j−i , Description character s[i] In substring dp[j-1] In the interval , be dp[j] The left boundary of is bounded by s[i] decision , namely dp[j] = j - i - Return value : max(dp) , That is, the overall situation “ The longest non repeating substring ” The length of
Problem solving
#include <iostream>
using namespace std;
#include <vector>
#include<algorithm>
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int len = s.size();
if (s.empty())
return 0;
vector<int> dp(len + 1, 0);
for (int j = 1; j < len; j++)
{
int i = j - 1;
while (i >= 0 && s[i] != s[j])
i--; // Linear search i
dp[j] = dp[j - 1] < j - i ? dp[j - 1] + 1 : j - i; // dp[j - 1] -> dp[j]
}
return *max_element(dp.begin(), dp.end()); // Maximum , Dereference value max(dp[j - 1], dp[j])
}
};
void test01()
{
Solution solution;
string s = "abcabcbb";
cout << solution.lengthOfLongestSubstring(s) << endl;
}
int main()
{
test01();
system("pause");
return 0;
}

I hope this article can help you , If there is anything wrong with the above , Welcome to correct
Sharing determines the height , Learn to widen the gap
边栏推荐
- MGRE环境下实现私网互通综合实验
- 数据湖:从数据仓库看数据湖
- 美团8年经验之谈,测试工程师如何进阶(自动化、性能、测开)
- PyQt5_QListWidget分页多选控件
- The safe distance between you and personal information leakage may be decided by a laptop!
- Cloudcompare & PCL point cloud point matching (based on point to face distance)
- Chapter2 Standard Output
- linux:数据库连接
- Flask学习笔记
- MySQL master-slave replication
猜你喜欢
C语言详解系列——函数的认识(1)库函数,自定义函数

32.< tag-数组和位运算>补充: lt.剑指 Offer 56 - I. 数组中数字出现的次数

【Delphi】制作控件面板安装图标的简单方法(译)

04_ue4进阶_物理碰撞入门和发射火球

Clion + mingw64 configure C language development environment visual studio installation

数据湖:Apache Iceberg介绍

What is the core essence of smart parks?

8.< tag-动态规划和LCS问题>lt.300. 最长递增子序列 + lt.674. 最长连续递增序列

李宏毅机器学习2022-HW1

No routines, no traps, no advertisements | are you sure you don't need this free instant messaging software?
随机推荐
0 basic career change software test, the necessary skills with a monthly salary of 6000 and 11000 are quite different
Summary of topics related to strings
linux:数据库连接
注册树模式
The safe distance between you and personal information leakage may be decided by a laptop!
What is instant messaging? Development of instant messaging
CloudCompare&PCL 点云点匹配(基于点到面的距离)
Accessory mode
China Economic Net: "Yuan universe" is hot
PowerBI入门指南
Chapter 3 Standard Input
Kingbasees SQL language reference manual of Jincang database (8. Function (8))
编译构建工具-bazel
交换机Exchanges
SAP batch import template (WBS batch import as an example)
MySQL master-slave replication
Richview textbox items textbox
Unityc realizes the conversion of Chinese characters to Pinyin - using Microsoft chspinyinconv Library
ROS2的topic pub 指令出现:Failed to populate field: ‘Vector3‘ object has no attribute ‘x:1‘错误
openvino_datawhale