leetcode

[LeetCode] 918. Maximum Sum Circular Subarray 环形子数组的最大和

廉价感情. 提交于 2019-12-01 20:15:51
Given a circular array C of integers represented by A , find the maximum possible sum of a non-empty subarray of C. Here, a circular array means the end of the array connects to the beginning of the array. (Formally, C[i] = A[i] when 0 <= i < A.length , and C[i+A.length] = C[i] when i >= 0 .) Also, a subarray may only include each element of the fixed buffer A at most once. (Formally, for a subarray C[i], C[i+1], ..., C[j] , there does not exist i <= k1, k2 <= j with k1 % A.length = k2 % A.length .) Example 1: Input: [1,-2,3,-2] Output: 3 Explanation: Subarray [3] has maximum sum 3 Example 2:

[总结]链表与栈

冷暖自知 提交于 2019-12-01 17:14:19
链表与栈也是高频出现的面试题,这里将他们放在一篇讨论。 链表 链表最关键的在于边界条件的处理,这个只有在不断训练中熟悉与掌握。 [leetcode]24.Swap Nodes in Pairs 分别可以用递归和迭代来实现。对于迭代实现,还是需要建立dummy节点。要对头结点进行操作时,考虑创建哑节点dummy,使用dummy->next表示真正的头节点。这样可以避免处理头节点为空的边界问题。 # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def swapPairs(self, head: 'ListNode') -> 'ListNode': if not head or not head.next:return head new = head.next head.next = self.swapPairs(new.next) new.next = head return new [leetcode]25.Reverse Nodes in k-Group 递归。首先找到第k+1个节点,也就是这一段需要翻转的链表的尾部相连的那个节点。如果找不到第k+1个节点

LeetCode初级算法--数组02:旋转数组

你。 提交于 2019-12-01 16:38:37
LeetCode初级算法--数组02:旋转数组 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法、机器学习干货 csdn: https://blog.csdn.net/baidu_31657889/ csdn: https://blog.csdn.net/abcgkj/ github: https://github.com/aimi-cn/AILearners 一、引子 这是由LeetCode官方推出的的经典面试题目清单~ 这个模块对应的是探索的初级算法~旨在帮助入门算法。我们第一遍刷的是leetcoda推荐的题目。 查看完整的剑指Offer算法题解析请点击github链接: github地址 二、题目 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。 示例 1: 输入: [1,2,3,4,5,6,7] 和 k = 3 输出: [5,6,7,1,2,3,4] 解释: 向右旋转 1 步: [7,1,2,3,4,5,6] 向右旋转 2 步: [6,7,1,2,3,4,5] 向右旋转 3 步: [5,6,7,1,2,3,4] 示例 2: 输入: [-1,-100,3,99] 和 k = 2 输出: [3,99,-1,-100] 解释: 向右旋转 1 步: [99,-1,-100,3] 向右旋转 2 步: [3,99,-1,-100]

[总结]字符串

时光毁灭记忆、已成空白 提交于 2019-12-01 10:02:17
字符串类型的题目一般与哈希,栈,双指针,DP紧密相关。理解了这几个知识与结构,字符串类型的题目一般也可以迎刃而解。另外,针对字符串特有的KMP算法以及Manacher算法的变形题也是常见题,需要掌握。 哈希 [leetcode]318. Maximum Product of Word Lengths 难点是怎么判断两个字符串是否不含有相同的字符。可以用一个int32位(小写字母只有26个),后26位用来表示对应的字符是否存在。最后两个int相与,如果结果为0,说明两个对应的字符串没有相同的字符。 class Solution: def maxProduct(self, words: List[str]) -> int: res = 0 d = collections.defaultdict(int) N = len(words) for i in range(N): w = words[i] for c in w: d[w] |= 1 << (ord(c) - ord('a')) for j in range(i): if not d[words[j]] & d[words[i]]: # if not set(list(words[j])) & set(list(words[i])): #Time Limit Exceeded res = max(res, len(words[j]

LeetCode-237 删除链表中的节点

你。 提交于 2019-12-01 09:38:16
问题: 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。 现有一个链表 -- head = [4,5,1,9],它可以表示为: 示例 1: 输入: head = [4,5,1,9], node = 5 输出: [4,1,9] 解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9. 示例 2: 输入: head = [4,5,1,9], node = 1 输出: [4,5,9] 解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9. 说明: 链表至少包含两个节点。 链表中所有节点的值都是唯一的。 给定的节点为非末尾节点并且一定是链表中的一个有效节点。 不要从你的函数中返回任何结果。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/delete-node-in-a-linked-list 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 分析:这道题的难度在于容易陷入思维误区,题干说明是删除节点,所以我们在想的是利用某种算法来删除给定的节点,但实际上这是不可能实现的,所以我们应该透过现象看本质,本质是将链表[4,5,1,9],转换为[4,5,9]

本周学习小结(14/10 - 20/10)

亡梦爱人 提交于 2019-12-01 07:41:06
LeetCode 本周有进展。需要及时复习。 学习笔记之LeetCode - 浩然119 - 博客园 https://www.cnblogs.com/pegasus923/p/5528520.html Explore - LeetCode - Design https://leetcode.com/explore/interview/card/top-interview-questions-medium/112/design/ Explore - LeetCode - Math https://leetcode.com/explore/interview/card/top-interview-questions-medium/113/math/ 学习笔记之Problem Solving with Algorithms and Data Structures using Python - 浩然119 - 博客园 https://www.cnblogs.com/pegasus923/p/10454395.html C++ / Database / Git / Linux / Python / MISC 本周有进展。 学习笔记之C / C++ - 浩然119 - 博客园 https://www.cnblogs.com/pegasus923/p/10437163.html

二叉树算法练习递归类型汇总

谁说我不能喝 提交于 2019-12-01 07:24:25
递归类型 按照编程技巧分啪 一、将复杂问题分解成两个子问题 1、平衡二叉树( LeetCode题库110题 ) 自上而下 :算每个节点的平衡因子(即左右子树的高度差),判断是否满足条件。 可以分成两个子问题:求树的高度,和遍历树判断每个节点的是否满足条件 class Solution { public boolean isBalanced(TreeNode root) { if(root == null) { return true; }else { //判断根结点是否满足平衡因子 int ldepth = depth(root.left); int rdepth = depth(root.right); System.out.println(ldepth + " " + rdepth); if(Math.abs(ldepth - rdepth) > 1) { return false; } //判断左子树是否满足平衡因子 if(!isBalanced(root.left)) { return false; } //判断右子树是否满足平衡因子 if(!isBalanced(root.right)) { return false; } return true; } } private int depth(TreeNode root) { int l = 0; int r = 0;

[总结]数组

℡╲_俬逩灬. 提交于 2019-12-01 07:01:14
数组问题向来是笔试与面试中最长出现的题目。其题型多变,涉及知识面广,从基础到高级数据结构均可涉及,这里总结下刷题常见的以及易错的题型。 常见基础题型 二分 对于数组最常见和基础的算法就是二分了,参考 Rotated Sorted Array问题 ,以旋转数组为例,通过对这类题型的了解,能够很好掌握二分算法的套路与思想。特别注意边界条件以及退出条件的判断,常规二分算法的解法与套路可参考 你真的会写二分查找吗? 另外,对于一些变形题也要特别注意甄别。 [leetcode]275.H-Index II 这里实际要比较的是citations[mid]与n - mid的差别,并进一步二分。具体如下: class Solution: def hIndex(self, citations: List[int]) -> int: if not citations or len(citations) == 0: return 0 n = len(citations) lo ,hi = 0,n-1 while lo <= hi: mid = (lo + hi) // 2 if citations[mid] >= n - mid: hi = mid - 1 else: lo = mid + 1 return n - lo [leetcode]378.Kth Smallest Element in a

LeetCode-BFS&DFS

老子叫甜甜 提交于 2019-12-01 05:36:28
127. Word Ladder 单词梯子 https://leetcode.com/problems/word-ladder/ 题目:给定两个单词(首词和尾词)和字典的单词列表,找出从开头到结尾的最短转换序列的长度,每次转换只能更改一个字母。每个转换后的单词必须存在于单词列表中。请注意,首词不是一个转换的词。 思路:BFS, class Solution { public int ladderLength(String beginWord, String endWord, List<String> wordAsList) { if(beginWord.equals(endWord)) return 1; if (!wordAsList.contains(endWord)) { return 0; } Queue<String> queue = new LinkedList<>(); queue.add(beginWord); wordAsList.remove(beginWord); int level = 2; while(!queue.isEmpty()) { int size = queue.size(); for(int i = 0; i < size; i++) { String temp = queue.poll(); for(int j = 0; j < temp

LeetCode-贪心

十年热恋 提交于 2019-12-01 01:45:51
45. Jump Game II 跳跃游戏 II https://leetcode.com/problems/jump-game-ii/ 题目:给定一个非负整数数组,您最初定位在数组的第一个索引处。数组中的每个元素表示该位置的最大跳转长度。您的目标是达到最小跳数中的最后一个索引。 思路: class Solution { public int jump(int[] nums) { int curFarthest = 0, curEnd = 0; int count = 0; for(int i = 0; i < nums.length-1; i++) { curFarthest = Math.max(nums[i]+i, curFarthest); if(i == curEnd) { count++; curEnd = curFarthest; } } return count; } } 55. Jump Game 跳跃游戏 https://leetcode.com/problems/jump-game/ 题目:给定一个非负整数数组,您最初定位在数组的第一个索引处。数组中的每个元素表示该位置的最大跳转长度。确定是否能够达到最后一个索引。 思路: class Solution { public boolean canJump(int[] nums) { if(nums.length