LeetCode 字符串专题(一)
LeetCode 字符串专题 <c++>
\([5]\) Longest Palindromic Substring
最长回文子串
\([28]\) Implement strStr()
要求实现c++中strstr()函数。
解法一:暴力 时间复杂度 \(O(nm)\)
解法二:KMP,时间复杂度 \(O(n+m)\),附代码
class Solution {
public:
int strStr(std::string haystack, std::string needle) {
return kmp(haystack.c_str(), needle.c_str());
}
private:
static void compute(const char *parttern, int next[]) {
int i;
int j = -1;
const int m = strlen(parttern);
next[0] = j;
for (i = 1; i < m; i++) {
while (j > -1 && parttern[j + 1] != parttern[i]) j = next[j];
if (parttern[i] == parttern[j + 1]) j++;
next[i] = j;
}
}
static int kmp(const char *text, const char *parttern) {
int i;
int j = -1;
const int n = strlen(text);
const int m = strlen(parttern);
if (m == 0) return 0;
int *next = (int *) malloc(sizeof(int) * m);
compute(parttern, next);
for (i = 0; i < n; i++) {
while (j > -1 && parttern[j + 1] != text[i]) j = next[j];
if (text[i] == parttern[j + 1]) j++;
if (j == m - 1) {
free(next);
return i - j;
}
}
free(next);
return -1;
}
};
[\(49\)] Group Anagrams
将每个字符串的字符排完序后,若两个字符串相同,则构成原串的字母种类及数量都相同,即属于同一组‘Anagrams’。
hash,时间复杂度 \(O(n)\)
class Solution {
public:
std::vector<std::vector<std::string>> groupAnagrams(std::vector<std::string> &strs) {
std::unordered_map<std::string, std::vector<std::string>> ump;
for (auto i = strs.begin(); i != strs.end(); i++) {
auto key = *i;
std::sort(key.begin(), key.end());
ump[key].push_back(*i);
}
std::vector<std::vector<std::string>> res;
for (auto i = ump.begin(); i != ump.end(); i++) {
res.push_back(i->second);
}
return res;
}
};
LeetCode 字符串专题(一)的更多相关文章
- Leetcode字符串专题
Leetcode38. Count and Say 分析:根据题意,数列的下一项就是统计上一项中每个数字出现的次数,理解清楚题意就很简单了 class Solution { public: strin ...
- 【leetcode 字符串处理】Compare Version Numbers
[leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...
- NOIP2018提高组金牌训练营——字符串专题
NOIP2018提高组金牌训练营——字符串专题 1154 回文串划分 有一个字符串S,求S最少可以被划分为多少个回文串. 例如:abbaabaa,有多种划分方式. a|bb|aabaa - 3 个 ...
- LeetCode树专题
LeetCode树专题 98. 验证二叉搜索树 二叉搜索树,每个结点的值都有一个范围 /** * Definition for a binary tree node. * struct TreeNod ...
- leetcode 字符串类型题
1,Vaild Palindrome bool isPalindrome(string& s) { transform(s.begin(), s.end(), s.begin(), tolow ...
- leetcode 字符串中的第一个唯一字符
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = "leetcode" 返回 0. s = "loveleetcod ...
- leetcode 字符串动态规划总结
问题1:leetcode 正则表达式匹配 请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配 ...
- 字符串专题:map POJ 1002
第一次用到是在‘校内赛总结’扫地那道题里面,大同小异 map<string,int>str 可以专用做做字符串的匹配之类的处理 string donser; str [donser]++ ...
- PHP 截取字符串专题
1. 截取GB2312中文字符串 < ?php//截取中文字符串function mysubstr($str, $start, $len) { $tmpstr = "" ...
随机推荐
- git 命令详细
git是代码管理工具 github是基于git实现的代码管理平台 git --version 查看git版本 git remote -v 查看clone地址 git init 初始化git //全局设 ...
- Pytorch--Dropout笔记
dropout常常用于抑制过拟合,pytorch也提供了很方便的函数.但是经常不知道dropout的参数p是什么意思.在TensorFlow中p叫做keep_prob,就一直以为pytorch中的p应 ...
- STM32 的PWM关闭方法
采用直接修改PWM的占空比,可以实现对PWM的关闭,且切换到稳定的高或者低状态.
- git(windows)
windows下比较比较好用的git客户端: 1. msysgit + TortoiseGit(乌龟git) 2. GitHub for Windows 3. Git Extensions
- Maven Install报错:Perhaps you are running on a JRE rather than a JDK?
我用的是idea,解决办法是:安装jdk,配置环境变量
- VUE中/deep/深度作用域
vue中css样式不起作用,用!important也不起作用,此时需要用 /deep/ ,没加之前是 加了之后起作用了,此时这个deep是深度作用域
- Linux定时执行PHP
1.使用crond服务 crontab -e #编辑任务列表 crontab -l #展示任务列表 26 15 * * * /usr/local/php70/bin/php -q /data/www/ ...
- No grammar constraints (DTD or XML Schema) referenced in the document.
问题描述 web.xml 使用 Servlet4.0 版本,No grammar constraints (DTD or XML Schema) referenced in the document. ...
- 微信小程序rich-text 文本首行缩进和图片居中
微信小程序开发使用rich-text组件渲染html格式的代码,常常因为不能自定义css导致文本不能缩进,以及图片不能居中等问题,这里可以考虑使用js的replace方法,替换字符串,然后在渲染的同时 ...
- github配置ssh密钥的方法
配置用户名和邮箱 初次安装git需要配置用户名和邮箱,否则git会提示:please tell me who you are. 你需要运行命令来配置你的用户名和邮箱: $ git config --g ...