leetcode

LeetCode All in One 题目讲解汇总(持续更新中...)

为君一笑 提交于 2020-01-01 03:37:03
Given strings S and T , find the minimum (contiguous) substring W of S , so that T is a subsequence of W . If there is no such window in S that covers all characters in T , return the empty string "" . If there are multiple such minimum-length windows, return the one with the left-most starting index. Example 1: Input: S = "abcdebdde", T = "bde" Output: "bcde" Explanation: "bcde" is the answer because it occurs before "bdde" which has the same length. "deb" is not a smaller window because the elements of T in the window must occur in order. Note: All the strings in the input will only contain

LeetCode 15. 3Sum

北城余情 提交于 2019-12-30 22:56:59
问题链接 LeetCode 15. 3Sum 题目解析 给定n个元素的数组,寻找符合 \(a+b+c=0\) 的所有 {a, b, c}。 解题思路 如果对LeetCode的第一题 Two Sum 还有印象的话,会发现这道题很类似,不过题目要求的结果有明显区别,本题需要求出所有符合条件的结果。 如果想复用Two Sum中的方法,可以加以转化: \(a+b = -c\) ,与 \(x+y = target\) 就很类似了。存在一个严重的问题——重复的结果!为了避免重复,可以想到需用使用 \(set\) 容器。由于 \(set\) 中的元素是三元素向量,想要避免重复必须使向量中三个元素有序,所以需要先将数组排序。 参考代码 class Solution { public: vector<vector<int>> threeSum(vector<int>& nums) { set<vector<int>> res; sort(nums.begin(), nums.end()); for (int k = 0; k < nums.size(); k++) { if (nums[k] > 0) break; int target = 0 - nums[k];//固定最小数 int i = k + 1, j = nums.size() - 1; while (i < j) { if (nums

LeetCode All in One 题目讲解汇总(持续更新中...)

a 夏天 提交于 2019-12-30 22:56:04
Given an array of n integers nums and a target , find the number of index triplets i, j, k with 0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target . For example, given nums = [-2, 0, 1, 3] , and target = 2. Return 2. Because there are two triplets which sums are less than 2: [-2, 0, 1] [-2, 0, 3] Follow up: Could you solve it in O ( n 2) runtime? 这道题是3Sum问题的一个变形,让我们求三数之和小于一个目标值,那么最简单的方法就是穷举法,将所有的可能的三个数字的组合都遍历一遍,比较三数之和跟目标值之间的大小,小于的话则结果自增1,参见代码如下: 解法一: // O(n^3) class Solution { public: int threeSumSmaller(vector<int>& nums, int target) { int res = 0; sort(nums

LeetCode解题报告—— 1-bit and 2-bit Characters & 132 Pattern & 3Sum

江枫思渺然 提交于 2019-12-30 22:54:37
1. 1-bit and 2-bit Characters We have two special characters. The first character can be represented by one bit 0 . The second character can be represented by two bits ( 10 or 11 ). Now given a string represented by several bits. Return whether the last character must be a one-bit character or not. The given string will always end with a zero. Example 1: Input: bits = [1, 0, 0] Output: True Explanation: The only way to decode it is two-bit character and one-bit character. So the last character is one-bit character. Example 2: Input: bits = [1, 1, 1, 0] Output: False Explanation: The only way

LeetCode 16. 3Sum Closest

五迷三道 提交于 2019-12-30 22:53:51
问题链接 LeetCode 16. 3Sum Closest 题目解析 给定n个元素的数组和目标值,取三个元素使三数之和最接近目标值,求最接近目标值的三数之和。 解题思路 本题与 LeetCode 15. 3Sum ,思路一模一样,难度相差不大。 同样的,先将数组排序。固定最小数字,移动左右指针,利用变量 \(diff\) 记录最小的误差值,需要注意一点:与 \(3Sum\) 不同,本题只需输出最接近的和即可,一些限制条件可以去除,比如 \(nums[k]>target\) 这种情况,在 \(3sum\) 中直接跳出循环,但在本题中也是有可能是答案的。 时间复杂度: \(O(n^2)\) 。 参考代码 class Solution { public: int threeSumClosest(vector<int>& nums, int target) { int diff = INT_MAX, res; sort(nums.begin(), nums.end()); for(int k = 0; k < nums.size(); k++) { int i = k+1, j = nums.size()-1; while(i < j) { int sum = nums[k] + nums[i] + nums[j]; int newDiff = abs(sum - target); if

leetcode@ [54/59] Spiral Matrix & Spiral Matrix II

你。 提交于 2019-12-28 01:59:08
https://leetcode.com/problems/spiral-matrix/ Given a matrix of m x n elements ( m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5] . class Solution { public: bool check(vector<vector<int> >& matrix, vector<vector<bool> >& vis, int x, int y) { if(x<0 || x>=matrix.size() || y<0 || y>=matrix[0].size() || vis[x][y]) return false; return true; } vector<int> spiralOrder(vector<vector<int>>& matrix) { vector<int> res; res.clear(); if(matrix.size() == 0) return

LeetCode All in One 题目讲解汇总(持续更新中...)

核能气质少年 提交于 2019-12-27 21:34:32
Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph. Each node in the graph contains a val ( int ) and a list ( List[Node] ) of its neighbors. Example: Input: {"$id":"1","neighbors":[{"$id":"2","neighbors":[{"$ref":"1"},{"$id":"3","neighbors":[{"$ref":"2"},{"$id":"4","neighbors":[{"$ref":"3"},{"$ref":"1"}],"val":4}],"val":3}],"val":2},{"$ref":"4"}],"val":1} Explanation: Node 1's value is 1, and it has two neighbors: Node 2 and 4. Node 2's value is 2, and it has two neighbors: Node 1 and 3. Node 3's value is 3, and it has two neighbors: Node 2

LeetCode--387--字符串中的第一个唯一字符

偶尔善良 提交于 2019-12-27 17:46:07
问题描述: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。 案例: s = "leetcode" 返回 0. s = "loveleetcode", 返回 2. 方法: 1 class Solution(object): 2 def firstUniqChar(self, s): 3 """ 4 :type s: str 5 :rtype: int 6 """ 7 x = "abcdefghijklmnopqrstuvwxyz" 8 res = [] #res=[s.index[i] for i in x if s.count(i) == 1] 9 for i in x: 10 if i in s and s.count(i) == 1: 11 res.append(s.index(i)) 12 if len(res): 13 return min(res) 14 return -1 官方: 1 class Solution(object): 2 def firstUniqChar(self, s): 3 """ 4 :type s: str 5 :rtype: int 6 """ 7 8 if s == '': 9 return -1 10 11 l = len(s) 12 tmp = l 13 for i in

LeetCode 268. Missing Number--Python解法--数学题

江枫思渺然 提交于 2019-12-27 17:23:51
LeetCode 268. Missing Number–Python解法–数学题 LeetCode题解专栏: LeetCode题解 LeetCode 所有题目总结: LeetCode 所有题目总结 大部分题目C++,Python,Java的解法都有。 题目地址: Missing Number - LeetCode Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array. Example 1: Input: [3,0,1] Output: 2 Example 2: Input: [9,6,4,2,3,5,7,0,1] Output: 8 Note: Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity? 之所以会想到做这道题,是因为之前在看书《算法新解》的前言部分就讲了这道题目。 最容易想到的是使用排序算法,但肯定不是最优的。 另外一种略好的做法是使用set,但额外空间占用比较多。 我想到的Python解法如下: class

一线互联网公司算法方向社招面试过程(五)-今日头条

穿精又带淫゛_ 提交于 2019-12-27 06:03:14
头条面了两个部门,第二次的面试题如下: 找出数组子数组和为给定target的所有子数组 求二叉树的路径和 排好序的数组,找target的索引的上下界 数组子数组和为给定target的所有子数组在leetcode上有几道类似的题目: [LeetCode] 560. Subarray Sum Equals K 子数组和为K ​[LeetCode] Continuous Subarray Sum 连续的子数组之和 ​[LeetCode] 209. Minimum Size Subarray Sum 最短子数组之和 ​[LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k 这几道题很类似,可以结合在一起看下。 二叉树的路径和,用DFS递归来解,对应leetcode题目 [LeetCode] Sum Root to Leaf Numbers 求根到叶节点数字之和 [LeetCode] 112. Path Sum 二叉树的路径和 排好序的数组,找target的索引的上下界:排好序的数据一般用二分查找解决,这道题递归的用二分查找,对应leetcode [LeetCode] 34. Find First and Last Position of Element in Sorted Array 在有序数组中查找元素的第一个和最后一个位置