leetcode

LeetCode 22. Generate Parentheses--Python 解法--广度优先、深度优先解法

跟風遠走 提交于 2020-01-29 08:50:12
此文首发于我的个人博客: LeetCode 22. Generate Parentheses–Python 解法–广度优先、深度优先解法 — zhang0peter的个人博客 LeetCode题解文章分类: LeetCode题解文章集合 LeetCode 所有题目总结: LeetCode 所有题目总结 题目地址: Generate Parentheses - LeetCode Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: [ "((()))", "(()())", "(())()", "()(())", "()()()" ] 这道题目看起来不难,只要把所有的可能性都输出即可。 可以深度优先,或者广度优先解决。 广度优先的Python解法如下,先输出 ()()()... class Solution : def generateParenthesis ( self , n : int ) - > List [ str ] : def helper ( s = '' , left = 0 , n = 0 ) : if n == 0 :

LeetCode 347:前k个高频元素

我的未来我决定 提交于 2020-01-28 13:31:40
LeetCode 347:前k个高频元素 题目描述 解题思路 代码实现 总结 题目描述 给定一个非空的整数数组,返回其中出现频率前 k 高的元素。 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输入: nums = [1], k = 1 输出: [1] 说明: 你可以假设给定的 k 总是合理的,且 1 ≤ k ≤ 数组中不相同的元素的个数。 你的算法的时间复杂度必须优于 O(n log n) , n 是数组的大小。 解题思路 想到可以用hashmap以每个元素为key统计,各个元素出现的频次,再构建一个小顶堆用来存储前k个高频的元素,遍历数组元素,在小顶堆存储个数小于k时,按序添加,大于k时,比较堆顶元素出现的频次和遍历元素出现的频次,如果堆顶元素频次小于遍历元素的,那么将堆顶元素弹出后,再将新元素加入堆中!!! 代码实现 去 博客设置 页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片 . import java . util . * ; public class topKthEle { //前k个高频元素 public List < Integer > topKFrequent ( int [ ] nums , int k ) { //acc: 24ms 53% HashMap < Integer ,

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

穿精又带淫゛_ 提交于 2020-01-28 03:36:38
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses. Now, you are given positions of houses and heaters on a horizontal line, find out minimum radius of heaters so that all houses could be covered by those heaters. So, your input will be the positions of houses and heaters seperately, and your expected output will be the minimum radius standard of heaters. Note: Numbers of houses and heaters you are given are non-negative and will not exceed 25000. Positions of houses and heaters you are given are non-negative and

LeetCode刷题笔记 39. 组合总和

半腔热情 提交于 2020-01-27 20:38:11
39. 组合总和 题目要求 题解 其他 size_t pop_back() 题目要求 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 candidates 中的数字可以无限制重复被选取。 说明: 所有数字(包括 target)都是正整数。 解集不能包含重复的组合。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/combination-sum 示例 1: 输入: candidates = [ 2,3,6,7 ] , target = 7, 所求解集为: [ [ 7 ] , [ 2,2,3 ] ] 示例 2: 输入: candidates = [ 2,3,5 ] , target = 8, 所求解集为: [ [ 2,2,2,2 ] , [ 2,3,3 ] , [ 3,5 ] ] 题解 https://github.com/soulmachine/leetcode class Solution { public : vector < vector < int >> combinationSum ( vector < int > & candidates , int target ) { sort ( candidates .

LeetCode 74.Search a 2D Matrix & 240.Search a 2D Matrix II

旧城冷巷雨未停 提交于 2020-01-27 14:00:19
文章目录 版权声明 1. LeetCode 74 1.1 复杂度分析 1.2 二分法 2. LeetCode 240 2.1 复杂度分析 2.2 每次排除一行或一列 References 版权声明 LeetCode 系列笔记来源于 LeetCode 题库 1 ,在个人思考的基础之上博采众长,受益匪浅;故今记此文,感怀于心,更多题解及程序,参见 Github 2 ; 该系列笔记不以盈利为目的,仅用于个人学习、课后复习及交流讨论; 如有侵权,请与本人联系(hqpan@foxmail.com),经核实后即刻删除; 本文采用 署名-非商业性使用-禁止演绎 4.0 国际 (CC BY-NC-ND 4.0) 协议发布; 1. LeetCode 74 1.1 复杂度分析 二分法: 时间复杂度: O ( l o g ( m n ) ) O(log(mn)) O ( l o g ( m n ) ) ; 空间复杂度: O ( 1 ) O(1) O ( 1 ) ; 解题思路: 每行中的数据从左向右递增; 每行中的最后一个数大于下一行中的第一个数; 该二维数组中的数据关系类似于一个一维数组中的数据关系; 使用二分法求解; 1.2 二分法 难点: Q1:两种方法的区别? Approach 1:给定两个数在矩阵中的索引,求中点的索引; Approach 2:给定1和 m × n m\times n m × n

[leetcode]Sort List @ Python

江枫思渺然 提交于 2020-01-27 03:19:11
原题地址:http://oj.leetcode.com/problems/sort-list/ 题意:链表的排序。要求:时间复杂度O(nlogn),空间复杂度O(1)。 解题思路:由于题目对时间复杂度和空间复杂度要求比较高,所以查看了各种解法,最好的解法就是归并排序,由于链表在归并操作时并不需要像数组的归并操作那样分配一个临时数组空间,所以这样就是常数空间复杂度了,当然这里不考虑递归所产生的系统调用的栈。      这里涉及到一个链表常用的操作,即快慢指针的技巧。设置slow和fast指针,开始它们都指向表头,fast每次走两步,slow每次走一步,fast到链表尾部时,slow正好到中间,这样就将链表截为两段。      运行时需要将中文注释删掉,leetcode oj平台里面不支持中文字符。 代码: # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: # @param head, a ListNode # @return a ListNode def merge(self, head1, head2): if head1 == None: return head2 if

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

百般思念 提交于 2020-01-27 01:45:00
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous row. Example 1: Input: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 3 Output: true Example 2: Input: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 13 Output: false 这道题要求搜索一个二维矩阵,由于给的矩阵是有序的,所以很自然的想到要用 二分查找法 ,可以在第一列上先用一次二分查找法找到目标值所在的行的位置,然后在该行上再用一次二分查找法来找是否存在目标值。对于第一个二分查找,由于第一列的数中可能没有 target 值,该如何查找呢

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

我们两清 提交于 2020-01-26 20:31:05
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero. To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1. Example: Input: A = [ 1, 2] B = [-2,-1] C = [-1, 2] D = [ 0, 2] Output: 2 Explanation: The two tuples are: 1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0 这道题是之前那道 4Sum 的延伸

[Leetcode Weekly Contest]173

白昼怎懂夜的黑 提交于 2020-01-26 20:12:54
链接: LeetCode [Leetcode]5319.删除回文子序列 给你一个字符串 s,它仅由字母 'a' 和 'b' 组成。每一次删除操作都可以从 s 中删除一个回文 子序列。返回删除给定字符串中所有字符(字符串为空)的最小删除次数。 注意这里,「子序列」定义:如果一个字符串可以通过删除原字符串某些字符而不改变原字符顺序得到,那么这个字符串就是原字符串的一个子序列。 「回文」定义:如果一个字符串向后和向前读是一致的,那么这个字符串就是一个回文。 这道题关键在于“审题”,当发现字符串仅有字母 'a' 和 'b' 组成,那么我们联想到,删除给定字符串中所有字符(字符串为空)的最小删除次数只有三种情况: 0种,即字符串为空 1种,即字符串本身为回文 2种,即其他情况。因为当字符串仅有字母 'a' 和 'b' 组成,则我们可以将所有包含a的子序列提取出来删除,再将所有b的子序列删除,此时只需要两次就实现了删除所有字符。 class Solution: def removePalindromeSub(self, s: str) -> int: if not s: return 0 if s == s[::-1]: return 1 return 2 [Leetcode]5320.餐厅过滤器 给一个餐馆信息数组 restaurants,其中 \(restaurants[i] = [idi

leetcode刷题记第14题解法(python解析)

感情迁移 提交于 2020-01-26 08:48:43
leetcode刷题记--> 14题解法(python解析) 题目定义 解题 1. 暴力解法 2. 垂直扫描 3. 利用zip函数 4. ascII码排序比较 5. os库 实现 题目定义 编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串 “”。 示例 1: 输入: ["flower","flow","flight"] 输出: "fl" 示例 2: 输入: ["dog","racecar","car"] 输出: "" 解释: 输入不存在公共前缀。 说明: 所有输入只包含小写字母 a-z 。 来源:力扣(LeetCode) 链接: leetcode_14题 . 解题 本次使用4种方法,在leetcode上还有更多的方法,只能说真牛逼,真聪明。 1. 暴力解法 2. 垂直扫描 我们从前往后枚举字符串的每一列,先比较每个字符串相同列上的字符(即不同字符串相同下标的字符)然后再进行对下一列的比较。 3. 利用zip函数 利用python的zip函数,把str看成list然后把输入看成二维数组,左对齐纵向压缩,然后把每项利用集合去重,之后遍历list中找到元素长度大于1之前的就是公共前缀 4. ascII码排序比较 利用python的max()和min(),在Python里字符串是可以比较的,按照ascII值排,举例abb, aba,abac,最大为abb