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 = "" ...
随机推荐
- 管理外部表(External Tables)
Oracle数据库允许对外部表中的数据进行只读访问.外部表定义为不驻留在数据库中的表,并且可以是为其提供访问驱动程序的任何格式.通过为数据库提供描述外部表的元数据,数据库能够公开外部表中的数据,就好像 ...
- 彻底搞懂 C# 的 async/await
前言 Talk is cheap, Show you the code first! private void button1_Click(object sender, EventArgs e) { ...
- C# 常用类型校验Validate
using System.Text; using System.Text.RegularExpressions; namespace 落地页测试代码 { public class Validate { ...
- 基于tkinter的GUI编程
tkinter:tkinter是绑定了Python的TKGUI工具集,就是Python包装的Tcl代码,通过内嵌在Python解释器内部的Tcl解释器实现的,它是Python标准库的一部分,所以使用它 ...
- 电脑移动后WIFI连接失败解决方法
1.现象原因 经常会发现将自己的电脑带到不同的地方后连接附近WIFI失败的现象,这是什么原因造成的了,觉得明明之前还有连过这个无线,密码都是正确的,无线连接的图标显示一个大大大的感叹号! 像下面一样 ...
- Make a plan
1. 思考 2. 学习英语 3. 练习书法 执行周期:2015年1月15日到2016年1月16日. 要像每一次用餐一样对待每一天的计划. 早晨起来,第一件事情是洗漱,然后是思考: 中午时间,第一件事情 ...
- golang http.client 遇到了 Connection reset by peer 问题
最近一个 golang 写的 http.client 的,获取远程服务器数据,有时候会报错,尤其在数量很大的时候,老是收到 Connection reset by peer 这样的 提醒,都有点想用重 ...
- 将已经存在的异步请求callback转换为同步promise
由于js是单线程执行,为防止阻塞,会有很多异步回调函数callback,嵌套层次多了,可读性就差了很多.随着社区的发展,出现了promise.我们来将一些常见的回调函数做修改,变成promise的链式 ...
- Angular结构型指令,模块和样式
结构型指令 *是一个语法糖,<a *ngIf="user.login">退出</a>相当于 <ng-template [ngIf]="use ...
- 获取clock ticks per second
#include <sys/syscall.h> #include <stdio.h> #include <unistd.h> int main() { print ...