Leetcode
Walden1998
这个作者很懒,什么都没留下…
展开
-
LeetCode 40. 组合总和 II c++
class Solution {public: vector<vector<int>> combinationSum2(vector<int>& candidates, int target) { vector<vector<int>> res; if (candidates.size() == 0 || ta...原创 2019-04-10 10:07:41 · 159 阅读 · 0 评论 -
LeetCode 2. 两数相加 c++ python
提交代码:class Solution: def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: temp=ListNode(0) #创建一个新链表 l3=temp #将temp头部赋予l3 a=0 while l1!=No...原创 2019-05-15 15:53:02 · 145 阅读 · 0 评论 -
LeetCode 19. 删除链表的倒数第N个节点 c++
快慢指针可以一遍遍历解决/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:...原创 2019-05-15 16:15:22 · 182 阅读 · 0 评论 -
LeetCode 21. 合并两个有序链表 c++
设置一个临时头结点,方便使用/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {pu...原创 2019-05-15 16:25:36 · 194 阅读 · 0 评论 -
LeetCode 23. 合并K个排序链表 c++
分治大法/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: //合...原创 2019-05-15 16:40:37 · 232 阅读 · 0 评论 -
LeetCode 24. 两两交换链表中的节点 c++
思路还是比较清晰的/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ...原创 2019-05-15 16:58:52 · 140 阅读 · 0 评论 -
LeetCode 25. k个一组翻转链表 c++
每次处理k个结点中的三个结点中的中间结点,提到k个结点的第一个位置,一直到第k-1的位置/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} *...原创 2019-05-15 20:21:43 · 155 阅读 · 0 评论 -
LeetCode 61. 旋转链表 c++
这题做了很久,有个地方卡了很久大致的想法是先把链表连成环,同时记录链表的长度count,再进行平移,这里设置了两个指针,一个在前,一个在后,也就是快慢指针,一开始慢指针在原链表的末尾,快指针在头,对快指针进行平移,慢指针紧跟其后,平移count-k个单位(关于这个平移还有一些疑问,想通后进行补充),再令慢指针的next为NULL/** * Definition for sin...原创 2019-05-15 21:23:45 · 189 阅读 · 0 评论 -
Leetcode 6. Z 字形变换 python
python测试代码:class Solution: def convert(self, s, numRows): result = ['']*min(numRows,len(s)) #用字符串的个数模拟行数,假如是三行,则设定三个字符串,一个表示一行,如['', '', ''] #print(result) current_row...原创 2019-09-17 22:38:43 · 119 阅读 · 0 评论 -
LeetCode 7. 整数反转 python
测试代码如下class Solution(object): def reverse(self, x): """ :type x: int :rtype: int """ if x==0: return 0 str_x=str(x) #转化为字符串 ...原创 2019-09-19 22:06:30 · 140 阅读 · 1 评论 -
LeetCode 8. 字符串转换整数 (atoi) python
class Solution(object): def myAtoi(self, str): """ :type str: str :rtype: int """ str = str.lstrip() # 去除左空格 if len(str) == 0: return...原创 2019-09-19 22:52:34 · 101 阅读 · 0 评论 -
LeetCode 9. 回文数 python
class Solution(object): def isPalindrome(self, x): """ :type x: int :rtype: bool """ if x<0: return False x=str(x) #转化为字符串处理 ...原创 2019-09-24 22:12:00 · 159 阅读 · 0 评论 -
LeetCode 11. 盛最多水的容器 python
class Solution(object): def maxArea(self, height): """ :type height: List[int] :rtype: int """ left=0 #设立左右指针 right=len(height)-1 maxs=min...原创 2019-09-24 22:48:12 · 149 阅读 · 0 评论 -
LeetCode 12. 整数转罗马数字 python
class Solution(object): def intToRoman(self, num): A=["I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"] #设立两个列表 B=[1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500,...原创 2019-09-25 21:50:17 · 145 阅读 · 0 评论 -
LeetCode 13. 罗马数字转整数 python
class Solution(object): def romanToInt(self, s): """ :type s: str :rtype: int """ #首先建立一个HashMap来映射符号和值,然后对字符串从左到右来, # 如果当前字符代表的值不小于其右边,就加上该值;否则就减去...原创 2019-09-26 21:26:18 · 144 阅读 · 0 评论 -
LeetCode 14. 最长公共前缀 python
class Solution(object): def longestCommonPrefix(self, strs): """ :type strs: List[str] :rtype: str """ # 暴力解法,先找出字符串中长度最短的那个,找到位置s,以及长度 res = "" ...原创 2019-09-26 21:58:37 · 128 阅读 · 0 评论 -
LeetCode 17. 电话号码的字母组合 C++ python
特别注意输入为空的时候class Solution {public: vector<string> letterCombinations(string digits) { vector<string> res; if(digits.empty()) return res; map&l...原创 2019-05-13 20:46:31 · 155 阅读 · 0 评论 -
LeetCode 41. 缺失的第一个正数 c++
总结版题目:给定一个未排序的整数数组,找出其中没有出现的最小的正整数示例1:输入: [1,2,0]输出: 3示例2:输入: [3,4,-1,1]输出: 2示例3:输入: [7,8,9,11,12]输出: 1你的算法的时间复杂度应为O(n),并且只能使用常数级别的空间。当使用额外空间时,使用set可以很快解出class Solu...原创 2019-04-21 11:22:26 · 245 阅读 · 0 评论 -
LeetCode 3. 无重复字符的最长子串 python
运行代码如下:class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ n=0 if len(s) == 0: # 字符串长度为0,返回0 ...原创 2019-04-10 10:24:05 · 133 阅读 · 0 评论 -
Leetcode4 c++ 寻找两个有序数组的中位数
一、此方法暴力,并不能AC,时间超过限制#include <iostream>#include <vector>using namespace std;class Solution {public: double findMedianSortedArrays(vector<int>& nums1, vector<int>...原创 2019-03-31 22:52:02 · 203 阅读 · 0 评论 -
LeetCode 15 c++ 三数之和
转换为两数之和#include <iostream>#include <algorithm>#include <vector>using namespace std;class Solution{public: vector<vector<int>> threeSum(vector<int>& nu...原创 2019-04-01 09:31:01 · 206 阅读 · 0 评论 -
LeetCode88. 合并两个有序数组 python
不借助额外的数组class Solution: def merge(self, nums1, m, nums2, n): while m>0 and n >0: if nums1[m-1] >= nums2[n-1]: nums1[m+n-1] = nums1[m-1] ...原创 2019-04-10 19:08:53 · 474 阅读 · 0 评论 -
LeetCode 121. 买卖股票的最佳时机 python
1.始终保存最小的买入价格2.始终保存最大的利润比如数据2,7,1,3首先找到最小买入是2,然后做差7-2=5,保存利润,然后到最小买入变成1,此时利润还是5,然后到3,注意,这里就是核心了。如果1后面出现的数字足够大,大到和1做差的值大于5,那么最大利润值就改变,否则,最大利润还是5.这里暗含的逻辑是,后面的数如果减1的差肯定比减2的差来的大。class Solution(o...原创 2019-04-10 19:21:25 · 446 阅读 · 0 评论 -
LeetCode 122. 买卖股票的最佳时机 II python
比如[1,3,5,2,6,1,3]profit=(3-1)+(5-3)+(6-2)+(3-1)=3-1+5-3+(6-2)+(3-1)=(5-1)+(6-2)+(3-1)class Solution(object): def maxProfit(self, prices): """ :type prices: List[int] :...原创 2019-04-10 19:33:05 · 160 阅读 · 0 评论 -
LeetCode 16 C++ 最接近的三数之和
类似第15题#include <iostream>#include <algorithm>#include <cmath>#include <vector>using namespace std;class Solution{public: int threeSumClosest(vector<int>& n...原创 2019-04-01 17:18:36 · 281 阅读 · 0 评论 -
LeetCode 18 c++ 四数之和
#include <iostream>#include <algorithm>#include <cmath>#include <set>#include <vector>using namespace std;class Solution {public: vector<vector<int>>...原创 2019-04-01 20:04:25 · 211 阅读 · 0 评论 -
LeetCode 26 c++ 删除排序数组中的重复项
由于数组本身就是有序的,直接调用unique函数即可class Solution {public: int removeDuplicates(vector<int>& nums) { nums.erase(unique(nums.begin(), nums.end()), nums.end()); r...原创 2019-04-01 20:37:00 · 157 阅读 · 0 评论 -
LeetCode c++ 27. 移除元素
使用vector特别注意erase函数,使用迭代器时,删除之后会自动指向下一个元素当使用erase删除时,这才是删除的正确方法class Solution {public: int removeElement(vector<int>& nums, int val) { for(vector<int>::iterator iter=...原创 2019-04-01 20:39:39 · 134 阅读 · 0 评论 -
LeetCode c++ 33. 搜索旋转排序数组
0 1 2 4 5 6 77 0 1 2 4 5 66 7 0 1 2 4 55 6 7 0 1 2 44 5 6 7 0 1 22 4 5 6 7 0 11 2 4 5 6 7 0二分搜索法的关键在于获得了中间数后,判断下面要搜索左半段还是右半段,我们观察上面加粗的数字都是升序...原创 2019-04-07 16:29:33 · 134 阅读 · 0 评论 -
LeetCode 53. 最大子序和 c++ python
class Solution(object): def maxSubArray(self, nums): """ :type nums: List[int] :rtype: int """ sum = nums[0] maxsum = nums[0] for i in rang...原创 2019-04-11 11:31:13 · 103 阅读 · 0 评论 -
leetCode 35.搜索插入位置 c++
#include <iostream>#include <vector>using namespace std;class Solution {public: int searchInsert(vector<int>& nums, int target) { int low = 0; int hight = nums.size() -...原创 2019-04-08 11:03:33 · 172 阅读 · 0 评论 -
LeetCode 34.在排序数组中查找元素的第一个和最后一个位置 c++
#include <iostream>#include <vector>using namespace std;class Solution {public: vector<int> searchRange(vector<int>& nums, int target) { vector<int> res(2, -1...原创 2019-04-08 11:05:10 · 331 阅读 · 0 评论 -
LeetCode 31 C++ 下一个排列
【1 3 5 4 2】 --->【14 5 32】 --->【142 3 5】综合以上,找某序列的下一个排列算法为:1、找到右边“”最长“”的降序子序列,如【5,4,2】,此时pos定位到“3”的位置2、从右到左遍历降序子序列【5,4,2】,找到第一个大于3的元素,并交换(如上图红色值)3、将第二步得到的子序列翻转即可得到下一...原创 2019-04-03 19:59:13 · 240 阅读 · 0 评论 -
LeetCode 39. 组合总和 c++
采用回溯法class Solution {public: vector<vector<int>> combinationSum(vector<int>& candidates, int target) { vector<vector<int>> res; if (candidates.size() == 0 ||...原创 2019-04-10 09:50:38 · 225 阅读 · 0 评论 -
LeetCode 15. 三数之和 python
class Solution(object): def threeSum(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ #转化为两数之和,采用取出一个数,再用0减除这个数,记为target,再查找列表是否存在target...原创 2019-09-30 22:38:45 · 185 阅读 · 0 评论