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 = "" ...
随机推荐
- python 机器学习三剑客 之 Matplotlib
Matplotlib介绍: Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形 . 通过 Matplotlib,开发者可以仅需要几 ...
- 最新传智播客web前端开发39期视频教程【完整版】
本套视频为传智2018web前端开发全套视频教程基础班+就业班,视频+源码+案例笔记,全套高清不加密~2018最新传智播客视频! 本教程是实战派课程!为传智最新web前端39期,挑战全网最全视频,没有 ...
- Java基础12-工具类;变长参数;IO
作业解析 取出整数的16进制表示形式 \u00ff /** * int2hex * */ public static String int2hex(int i) { String str = &quo ...
- SSH整合 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- cadcam
Email:kefu007@vip.qq.com 13D TIMON 2007 英語版2007 23DVIA Composer V6R2013 中文版2013 3ABQUS V6.11 6.11 4A ...
- 微服务杂谈--EureKa及自我保护
时值初夏,各位老官的心也有所悸动,这不,众看官搬好小板凳,沏一壶清茶,待听Jerry话谈Eureka,以此静心.话不多少,且听: 一.微服务的产生 单体应用:一个jar或者一个war包交给运维,运行在 ...
- Scyther
1.Security Protocol :a domain analysis 一个安全协议描述了很多的行为,每一个行为称为角色,例如触发角色和 接受角色,一个系统有多个通信代理组成,每一个代理扮演 ...
- golang 实现广度优先算法(走迷宫)
maze.go package main import ( "fmt" "os" ) /** * 广度优先算法 */ /** * 从文件中读取数据 */ fun ...
- css设置多列等高布局
初始时,多个列内容大小不同,高度不同.现在需要设置不同的背景来显示,而且各个列的高度需要保持一致.那么这就需要利用到多列等高布局. 最终需要的效果: 1. 真实等高布局 flex 技术点:弹性盒子布局 ...
- FbinstTools制作多系统启动U盘(Windows+Linux)
U盘启动盘制作工具在国内有倆工具,老毛桃.大白菜.也不知道是谁模仿谁的,反正PE肯定是Microsoft的. PE其实就是精简版的Windows维护系统,那如何制作Linux启动盘呢,百度搜“linu ...