Python
fxy流年无悔
不要留下太多遗憾
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
680. 回文字符串-力扣
最多删除一个字符,判断是否能构成回文字符串。 b = a[i:j] 表示复制a[i]到a[j-1] a[::-1]相当于倒序,也就是从最后一个元素到第一个元素复制一遍 class Solution: def validPalindrome(self, s: str) -> bool: if s == s[::-1]: return True i,j=0...原创 2020-04-03 10:07:27 · 290 阅读 · 0 评论 -
633. 平方数之和-力扣
给定一个非负整数c,你要判断是否存在两个整数a和b,使得a2+ b2= c。 class Solution { public: bool judgeSquareSum(int c) { long i=0,j=sqrt(c); while(i<=j){ long sum=i*i+j*j; ...原创 2020-04-03 09:56:39 · 217 阅读 · 0 评论 -
167. 两数之和 II - 输入有序数组
给定一个已按照升序排列的有序数组,找到两个数使得它们相加之和等于目标数。 函数应该返回这两个下标值 index1 和 index2,其中 index1必须小于index2。 双指针 class Solution: def twoSum(self, numbers: List[int], target: int) -> List[int]: i = 0 ...原创 2020-04-03 09:55:02 · 175 阅读 · 0 评论 -
58. 最后一个单词的长度-力扣
给定一个仅包含大小写字母和空格' '的字符串 s,返回其最后一个单词的长度。如果字符串从左向右滚动显示,那么最后一个单词就是最后出现的单词。 如果不存在最后一个单词,请返回 0。 split() 通过指定分隔符对字符串进行切片, 默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等 class Solution: def lengthOfLastWord(self, s:...原创 2020-04-03 09:52:44 · 154 阅读 · 0 评论 -
53. 最大子序和-力扣
DP:nums[i-1]意为到i-1位置的最大子序和 给定一个整数数组nums,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 class Solution: def maxSubArray(self, nums: List[int]) -> int: for i in range(1,len(nums)): ...原创 2020-04-03 09:50:41 · 141 阅读 · 0 评论 -
35. 搜索插入位置-力扣
class Solution { public: int searchInsert(vector<int>& nums, int target) { int n=nums.size(); for(int i=0;i<=n-1;i++){ if(nums[i]>=target) return i; ...原创 2020-04-02 09:23:21 · 229 阅读 · 0 评论 -
28. 实现 strStr()-力扣
class Solution: def strStr(self, haystack: str, needle: str) -> int: m = len(needle) for i in range(len(haystack)-m+1): if haystack[i:i+m] == needle: ...原创 2020-04-02 09:22:23 · 187 阅读 · 0 评论 -
27. 移除元素 - 移除元素-力扣
class Solution { public: int removeElement(vector<int>& nums, int val) { int len = nums.size(); int i=0,j=0; while(j<len){ if(nums[j]!=val){ ...原创 2020-04-02 09:20:53 · 233 阅读 · 0 评论 -
26. 删除排序数组中的重复项,返回长度
删除重复元素 class Solution: def removeDuplicates(self, nums: List[int]) -> int: i=1 while i<=len(nums)-1: if nums[i]==nums[i-1]: del nums[i] ...原创 2020-04-02 09:19:12 · 188 阅读 · 0 评论 -
21. 合并两个有序链表
class Solution { public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode* head = new ListNode(1); ListNode* ret = head; while (l1 != NULL && l2 != NU...原创 2020-04-02 09:17:39 · 159 阅读 · 0 评论 -
20.有效的括号-力扣
遍历字符串: 左括号入栈 右括号分三种情况: 1.若此时栈为空,直接返回False 2.若此时右括号和栈顶左括号不匹配,直接返回False 3.若此时右括号和栈顶左括号匹配,栈顶左括号出栈 遍历结束后 若栈为空栈,说明所有括号都匹配完成,返回True 否则,返回False class Solution: def isValid(self, s: str) -> bool: ...原创 2020-04-02 09:16:16 · 227 阅读 · 0 评论 -
14.最长公共前缀
c++: class Solution { public: string longestCommonPrefix(vector<string>& strs) { if(strs.size()==0) return "";//空字符串 string result = ""; for (int i=0;i<strs[0].s...原创 2020-04-02 09:14:43 · 214 阅读 · 0 评论 -
1.两数之和-力扣
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 def twoSum(nums, target): lens = len(nums) j=-1 for i in range(1,lens): temp = nums[:i] if (target - nums[i])...原创 2020-04-02 09:13:01 · 221 阅读 · 0 评论 -
9. 回文数 - 力扣
Python: class Solution(object): def isPalindrome(self, x): lst=list(str(x)); l,r = 0,len(lst)-1 while l<=r: if lst[l]!=lst[r]: return False ...原创 2020-04-02 09:10:33 · 211 阅读 · 0 评论 -
13. 罗马数字转整数-力扣
C++: class Solution { public: int romanToInt(string s) { int result=0; map<char, int> mp = { {'I',1} ,{'V', 5} ,{'X', 10},{'L', 50} ,{'C', 100} ,{'D', 500} ,{'M', 1000} }; ...原创 2020-04-02 09:08:49 · 341 阅读 · 0 评论
分享