摘要: 1 class Solution(object): 2 def intersection(self, nums1, nums2): 3 """ 4 :type nums1: List[int] 5 :type nums2: List[int] 6 :rtype: List[int] 7 """ 8 阅读全文
posted @ 2020-04-14 14:44 人间烟火地三鲜 阅读(225) 评论(0) 推荐(0)
摘要: 思路:1、s和t长度不同,返回false;2、转list,sort(),s==t则返回true,否则返回false。 1 class Solution(object): 2 def isAnagram(self, s, t): 3 """ 4 :type s: str 5 :type t: str 阅读全文
posted @ 2020-04-14 14:43 人间烟火地三鲜 阅读(180) 评论(0) 推荐(0)
摘要: 思路:i、j指针分别遍历name和typed:若typed[j] == name[i]:i、j同步后移;若typed[j] != name[i]:i不动、j后移;j比i先遍历完,则返回false;否则返回true。 1 class Solution(object): 2 def isLongPres 阅读全文
posted @ 2020-04-14 14:40 人间烟火地三鲜 阅读(199) 评论(0) 推荐(0)
摘要: 思路:双指针,就是反转字符串的思路,不过加了层非字母的判断。详见注释。注:遇到非字母字符移动指针后不能忽略continue,因为不确定下一个字符就是字母。 1 class Solution(object): 2 def reverseOnlyLetters(self, S): 3 """ 4 :ty 阅读全文
posted @ 2020-04-14 14:37 人间烟火地三鲜 阅读(213) 评论(0) 推荐(0)
摘要: 思路:1、若A和B都为空,返回False;2、若两串长度不同,返回False;3、若A和B相等, 只要有重复元素,返回True;4、若两串长度相同但不相等,统计不同字符的个数num,并记录其位置,若num>2,返回False;5、将A中不同的两个字符交换位置,与B相等,返回True。 1 class 阅读全文
posted @ 2020-04-14 14:35 人间烟火地三鲜 阅读(195) 评论(0) 推荐(0)
摘要: 1 class Solution(object): 2 def toGoatLatin(self, S): 3 """ 4 :type S: str 5 :rtype: str 6 """ 7 # 定义元音字母集合 8 vowel = ['a', 'e', 'i', 'o', 'u', 'A', ' 阅读全文
posted @ 2020-04-14 14:31 人间烟火地三鲜 阅读(230) 评论(0) 推荐(0)
摘要: 思路:1、将整个段落中的字母转成小写;2、用各标点符号分割段落;3、遍历2返回的list,统计不在禁用表中且出现次数最多的单词并返回。 1 import re 2 3 class Solution(object): 4 def mostCommonWord(self, paragraph, bann 阅读全文
posted @ 2020-04-14 14:29 人间烟火地三鲜 阅读(205) 评论(0) 推荐(1)
摘要: 思路:逐个翻译每个单词的摩斯码,若result[]中不存在相投的摩斯码,则存入;否则翻一下一个单词;返回result[]的有效长度; 1 class Solution(object): 2 def uniqueMorseRepresentations(self, words): 3 """ 4 :t 阅读全文
posted @ 2020-04-14 14:25 人间烟火地三鲜 阅读(294) 评论(0) 推荐(0)
摘要: 思路: 1、遍历从1 N+1的所有整数2、将当前数字转化成字符串ch,只有所有字符都是list1中的字符,才满足旋转条件;3、遍历字符串化后的数,将存在list1中的字符旋转,拼接给ans;4、旋转后比较ch和ans,相等则res加1;5、返回res。 1 class Solution(object 阅读全文
posted @ 2020-04-14 14:22 人间烟火地三鲜 阅读(277) 评论(0) 推荐(0)
摘要: 1 class Solution(object): 2 def toLowerCase(self, str): 3 """ 4 :type str: str 5 :rtype: str 6 """ 7 res = "" 8 for i, ch in enumerate(str): 9 if ord( 阅读全文
posted @ 2020-04-14 14:19 人间烟火地三鲜 阅读(185) 评论(0) 推荐(0)
摘要: 思路: 首先分析题目要求的子串特点:0和1数量必须相等且不能交叉排列,即子串一半是0、另一半是1。 1、遍历原串s,统计连续相同字符的个数,存到list中;2、遍历list,每相邻两个元素,取其中较小者(相等则任取其一)添加到sum中;3、返回sum。 1 class Solution(object 阅读全文
posted @ 2020-04-14 14:15 人间烟火地三鲜 阅读(232) 评论(0) 推荐(0)
摘要: 1 class Solution(object): 2 def repeatedStringMatch(self, A, B): 3 """ 4 :type A: str 5 :type B: str 6 :rtype: int 7 """ 8 lenb = len(B) 9 # 用来暂存原A串 1 阅读全文
posted @ 2020-04-14 14:10 人间烟火地三鲜 阅读(164) 评论(0) 推荐(0)
摘要: 给定一个非空字符串 s,最多删除一个字符。判断是否能成为回文字符串。注意:字符串只包含从 a-z 的小写字母。字符串的最大长度是50000。示例 1:输入: "aba"输出: True示例 2:输入: "abca"输出: True解释: 你可以删除c字符。注意: 1. 字符串只包含从 a-z 的小写 阅读全文
posted @ 2020-04-14 14:07 人间烟火地三鲜 阅读(148) 评论(0) 推荐(0)