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 字符串专题(一)的更多相关文章

  1. Leetcode字符串专题

    Leetcode38. Count and Say 分析:根据题意,数列的下一项就是统计上一项中每个数字出现的次数,理解清楚题意就很简单了 class Solution { public: strin ...

  2. 【leetcode 字符串处理】Compare Version Numbers

    [leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...

  3. NOIP2018提高组金牌训练营——字符串专题

    NOIP2018提高组金牌训练营——字符串专题 1154 回文串划分 有一个字符串S,求S最少可以被划分为多少个回文串. 例如:abbaabaa,有多种划分方式.   a|bb|aabaa - 3 个 ...

  4. LeetCode树专题

    LeetCode树专题 98. 验证二叉搜索树 二叉搜索树,每个结点的值都有一个范围 /** * Definition for a binary tree node. * struct TreeNod ...

  5. leetcode 字符串类型题

    1,Vaild Palindrome bool isPalindrome(string& s) { transform(s.begin(), s.end(), s.begin(), tolow ...

  6. leetcode 字符串中的第一个唯一字符

    给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = "leetcode" 返回 0. s = "loveleetcod ...

  7. leetcode 字符串动态规划总结

    问题1:leetcode 正则表达式匹配 请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配 ...

  8. 字符串专题:map POJ 1002

    第一次用到是在‘校内赛总结’扫地那道题里面,大同小异 map<string,int>str 可以专用做做字符串的匹配之类的处理 string donser; str [donser]++ ...

  9. PHP 截取字符串专题

    1. 截取GB2312中文字符串 < ?php//截取中文字符串function mysubstr($str, $start, $len) {    $tmpstr = "" ...

随机推荐

  1. 上传Jcenter

    将自己封装的库发布分享到Jcenter

  2. 2018-2019-1-20165221&20165225 《信息安全系统设计》实验五:通讯协议设计

    2018-2019-1-20165221&20165225 <信息安全系统设计>-实验五:通讯协议设计 OpenSSL学习: 简介: OpenSSL是为网络通信提供安全及数据完整性 ...

  3. Mybatis 笔记

    环境:Mybatis 3 +MariaDB 10.1 似乎在调用存储过程时 ,参数只能写在一行上. 否则会返回语法错误.

  4. D - WE POJ - 3273 (二分法)

    Farmer John is an astounding accounting wizard and has realized he might run out of money to run the ...

  5. 软件工程作业-(third)

    1.选题目(1) 最大连续子数组和(最大子段和) 问题:给定n个整数(可能为负数)组成的序列a[1],a[2],a[3],-,a[n],求该序列如a[i]+a[i+1]+-+a[j]的子段和的最大值. ...

  6. css @import 导入文件

    导入另一个css文件 例如 <style type="text/css"> @import url(css/main.css); //导入css目录下的main.css ...

  7. 注册中心(Eureka)

    1. pom.xml依赖 <dependencies> <dependency> <groupId>org.springframework.cloud</gr ...

  8. mysql explain结果含义

    在SQL语句前面加上EXPLAIN即可 各字段含义 id SELECT识别符.这是SELECT的查询序列号 select_type SELECT类型,可以为以下任何一种: SIMPLE:简单SELEC ...

  9. python设计模式---行为型之观察者模式

    比较常用咯~~ from django.test import TestCase from abc import ABCMeta, abstractmethod # 行为型设计模式---观察者模式 c ...

  10. 关键字static

    原文出处:http://cmsblogs.com/ 『chenssy』 一. static代表着什么 在Java中并不存在全局变量的概念,但是我们可以通过static来实现一个“伪全局”的概念,在Ja ...