![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
字符串
Vaccy Zhu
自律是解决人生问题最主要的工具,也是解决人生痛苦最重要的方法!
展开
-
定长子串中元音的最大数目-字符串1456-c++
字符串滑动窗口,利用队列记录窗口内的元音索引,每次移动窗口,都pop窗口外索引和更新结果。原创 2024-03-30 22:44:38 · 100 阅读 · 0 评论 -
反转字符串中的元音字母-字符串345-c++
【代码】反转字符串中的元音字母-字符串345-c++原创 2024-02-28 16:46:02 · 356 阅读 · 0 评论 -
字符串的最大公因子-数组1071-c++
暴力枚举解法,先找出所有的前缀,再按长度从大到小遍历,判断该前缀能否同时整除str1和str2,能则直接返回,否则继续遍历。若所有前缀都不符合,返回空字符串。优化思路:先求两个字符串长度的最大公约数x,然后直接判断长度为x的前缀是否满足要求。原理:欧几里得算法,两个整数的最大公约数等于其中较小数和两数相除余数的最大公约数。原创 2024-02-28 13:49:43 · 382 阅读 · 0 评论 -
交替合并字符串-字符串1768-c++
【代码】交替合并字符串-字符串1768-c++原创 2024-02-27 18:38:44 · 330 阅读 · 0 评论 -
长按键入-模拟925-python
class Solution: def isLongPressedName(self, name: str, typed: str) -> bool: m, n = len(name), len(typed) if n < m: return False i = j = 0 while j < n: if i < m and name[i转载 2022-05-05 16:07:36 · 113 阅读 · 0 评论 -
同构字符串-字符串205-python
没看答案,需要构造两个映射哈希表(s->t和t->s)。class Solution: def isIsomorphic(self, s: str, t: str) -> bool: n = len(s) mapping_s = {} mapping_t = {} for i in range(n): if s[i] not in mapping_s.keys():原创 2022-05-05 14:38:27 · 102 阅读 · 0 评论 -
重复的子字符串-字符串459-python
没看答案。class Solution: def repeatedSubstringPattern(self, s: str) -> bool: n = len(s) for i in range(1, n//2+1): tmp = s[:i] if tmp*(n//i) == s: return True return False...原创 2022-02-20 16:58:49 · 108 阅读 · 0 评论 -
左旋转字符串-字符串(剑指offer58-II)-python
没看答案,非常简单的切片拼接,运行效率也很高。class Solution: def reverseLeftWords(self, s: str, k: int) -> str: tmp1 = s[:k] tmp2 = s[k:] return tmp2+tmp1原创 2022-02-19 10:40:42 · 128 阅读 · 0 评论 -
翻转字符串里的单词-字符串151-python
没看答案,先遍历s把单词提取出来到列表里,然后再组合成符合要求的答案。class Solution: def reverseWords(self, s: str) -> str: i, ls = 0, [] while i < len(s): if s[i] != ' ': tmp = '' while i < len(s) and s[i] != ' ':原创 2022-02-19 10:33:27 · 67 阅读 · 0 评论 -
替换空格-字符串(剑指offer05)-python
没看答案。class Solution: def replaceSpace(self, s: str) -> str: ls = s.split(' ') res = '%20'.join(ls) return res原创 2022-02-18 19:25:08 · 153 阅读 · 0 评论 -
反转字符串II-字符串541-python
没看答案,双指针局部翻转再拼接一起。class Solution: def reverseStr(self, s: str, k: int) -> str: left, right = 0, 0 res = '' while right < len(s): right += k if right >= len(s): res += s[left:][原创 2022-02-18 19:17:16 · 83 阅读 · 0 评论 -
字母异位词分组-哈希表49-python
由于互为字母异位词的两个字符串包含的字母相同,因此两个字符串中的相同字母出现的次数一定是相同的,故可以将每个字母出现的次数使用数组存储,作为哈希表的键。由于字符串只包含小写字母,因此对于每个字符串,可以使用长度为 26 的数组记录每个字母出现的次数。需要注意的是,在使用数组作为哈希表的键时,不同语言的支持程度不同,因此不同语言的实现方式也不同。from collections import defaultdictclass Solution: def groupAnagrams(self,原创 2022-02-16 14:24:23 · 238 阅读 · 0 评论 -
比较含退格的字符串-字符串844-python
没看答案,用栈来实现class Solution: def substract(self, s: str, length: int): if length < 1: return s stack = [] i = 0 res = '' while i < length: if s[i] != '#': sta原创 2022-02-10 21:50:59 · 407 阅读 · 0 评论 -
比较版本号-字符串165-python
pythonclass Solution: def compareVersion(self, version1: str, version2: str): nums1 = version1.split('.') nums2 = version2.split('.') m, n = len(nums1), len(nums2) if m < n: for _ in range(n-m):原创 2021-09-09 14:36:35 · 186 阅读 · 0 评论 -
第一个只出现一次的字符-字符串(剑指offer 50)-python
python暴力遍历字符串+哈希表排序class Solution: def firstUniqChar(self, s: str): if not s: return ' ' hashtable = dict() for index, c in enumerate(s): if c not in hashtable.keys(): hashtabl原创 2021-09-07 16:08:59 · 67 阅读 · 0 评论 -
最长重复子串-字符串1044-python
二分查找+Robin-Karp算法pythonclass Solution: def longestDupSubstring(self, s: str): nums = [ord(c)-ord('a')+1 for c in s] n = len(nums) def check(L): P, Q = 131, 2**64 M = P**(L-1)%Q tmp = 0原创 2021-09-06 16:55:34 · 181 阅读 · 0 评论 -
实现strStr()-字符串28-python
python一、暴力解法class Solution: def strStr(self, haystack: str, needle: str): if needle == '': return 0 i, j = 0, len(needle)-1 while j < len(haystack): if haystack[i:j+1] == needle: ret原创 2021-09-05 18:43:28 · 60 阅读 · 0 评论 -
字符串变形-字符串NC89-python
python# -*- coding:utf-8 -*-class Solution: def trans(self, s, n): # write code here res = [' '] * n i, j = n-1, 0 stack = [] while i >= 0: while i >= 0 and s[i].isalpha():原创 2021-08-25 18:15:33 · 152 阅读 · 0 评论 -
最大数-字符串179-python
pythonclass Solution: def largestNumber(self, nums: List[int]): if not nums: return '' res = '0' def compare(x, y): return -(int(x+y) - int(y+x)) nums = sorted(map(str, nums), key=cmp_to_key(com原创 2021-08-20 18:54:42 · 92 阅读 · 0 评论 -
大数相乘-模拟43-python
pythonclass Solution: def multiply(self, num1: str, num2: str): if num1 == '0' or num2 == '0': return '0' m = len(num1) n = len(num2) res = '' tmp = [0] * (m+n) for j in range(n原创 2021-08-20 18:00:26 · 103 阅读 · 0 评论 -
字符串相加(大数相加)-模拟415-C++
class Solution {public: string addStrings(string num1, string num2) { int i = num1.length() - 1; int j = num2.length() - 1; int carry = 0; string res; while(i >= 0 || j >= 0 || carry != 0){ int原创 2021-03-28 16:37:22 · 98 阅读 · 0 评论 -
验证IP地址-字符串468-python
pythonclass Solution: def ipv4(self, ip): ls = ip.split('.') if len(ls) != 4: return 'Neither' for data in ls: if data == '' or (len(data) > 1 and data[0] == '0'): return 'Neither'原创 2021-08-19 10:33:14 · 73 阅读 · 0 评论 -
最长公共前缀-字符串14-python
pythonclass Solution: def longestCommonPrefix(self, strs: List[str]): n = len(strs) if n == 0: return '' res = strs[0] for i in range(1, n): j = 0 while j < len(strs[i]) a原创 2021-08-19 09:56:52 · 31 阅读 · 0 评论 -
前k个高频单词-数组692-python
python哈希表+排序import collectionsclass Solution: def topKFrequent(self, words: List[str], k: int): counter = collections.Counter(words) res = [] counter = sorted(counter.items(), key=lambda kv:(-kv[1], kv[0])) for i in原创 2021-08-16 14:15:44 · 117 阅读 · 0 评论 -
复原IP地址-回溯93-python
pythonclass Solution: def restoreIpAddresses(self, s: str): n = len(s) track = [] res = [] index, count = 0, 0 def backtrack(s, index, track, count): if count == 4 and index == n: st原创 2021-08-15 16:49:04 · 90 阅读 · 0 评论 -
字符串转换整数-字符串8-python
python原创 2021-08-12 21:11:59 · 36 阅读 · 0 评论 -
无重复字符的最长字串-字符串3-python&c++
python双指针解法:使用两个指针表示字符串中的某个子串的左右边界。其中左指针代表着某个子串的起始位置,而右指针代表着某个字串的终止位置;在每一步的操作中,将左指针向右移动一格,表示开始枚举下一个字符作为起始位置,然后不断地向右移动右指针,但需要保证这两个指针对应的子串中没有重复的字符。在移动结束后,这个子串就对应着以左指针开始的,不包含重复字符的最长子串。最后记录下这个子串的长度即为答案;class Solution: def lengthOfLongestSubstring(se原创 2021-02-05 21:23:33 · 115 阅读 · 0 评论 -
找到字符串中所有字母异位词-字符串438-python&c++
滑动窗口pythonimport collectionsclass Solution: def findAnagrams(self, s: str, p: str): left, right = 0, 0 m = len(s) n = len(p) valid = 0 needs = collections.Counter(p) window = {} res = []原创 2021-08-10 13:31:46 · 80 阅读 · 0 评论 -
字符串的排列-字符串567-python
滑动窗口pythonimport collectionsclass Solution: def checkInclusion(self, s1: str, s2: str): left, right = 0, 0 n = len(s2) m = len(s1) valid = 0 needs = collections.Counter(s1) window = {} while原创 2021-08-10 12:17:08 · 78 阅读 · 0 评论 -
最小覆盖字串-字符串76-python&c++
滑动窗口pythonimport collectionsimport sysclass Solution: def minWindow(self, s: str, t: str): left, right = 0, 0 n = len(s) needs = collections.Counter(t) window = {} valid = 0 start = 0 length原创 2021-08-09 18:35:02 · 85 阅读 · 0 评论 -
反转字符串-字符串344-C++
class Solution {public: void reverseString(vector<char>& s) { char ch; int size = s.size(); int j = size - 1; for(int i = 0; i < size; i++){ if(i < j){ swap(s[i], s[j]);原创 2021-03-28 18:13:20 · 66 阅读 · 0 评论 -
回文数-字符串9-C++&python
算法思想:一、将int转化成string,然后遍历字符串即可。没看答案。class Solution {public: bool isPalindrome(int x) { if(x < 0) return false; string num = to_string(x); for(int i = 0; i < num.length(); i++){ if(i == num.length() - 1 - i)原创 2021-03-10 19:29:32 · 130 阅读 · 0 评论 -
整数反转-字符串7-C++
class Solution {public: int reverse(int x) { int res = 0; while(x != 0){ int pop = x % 10; x /= 10; if(res > INT_MAX / 10 || (res == INTMAX_MAX / 10 && pop > 7)) return 0; .原创 2021-03-10 20:07:39 · 100 阅读 · 0 评论