leetcode

LeetCode 332. Reconstruct Itinerary

我们两清 提交于 2019-11-28 15:20:59
本题是有向图找 Euler Path 的问题。可以用 Hierholzer’s Algorithm for the directed graph https://www.geeksforgeeks.org/hierholzers-algorithm-directed-graph/ Hierholzer’s Algorithm 的本质和树里的后序遍历很像。对于 Euler Circuit 来说,dfs如果到头了,说明当前这条路径已经构成了一条回路,backtrack回去把别的道路的 circuit 加到当前 circuit 即可。 由于是dfs,所以可以有 Recursive 和 Iterative 两种写法。 Recursive: https://leetcode.com/problems/reconstruct-itinerary/discuss/78835/28ms-C++-beats-100-Short-and-Elegant. Iterative: https://leetcode.com/problems/reconstruct-itinerary/discuss/78842/C++-non-recursive-O(N)-time-O(N)-space-solution-with-detail-explanations 以下代码是 Recursive class

本周学习小结(26/08 - 01/09)

亡梦爱人 提交于 2019-11-28 15:18:10
LeetCode 本周有进展。 学习笔记之LeetCode - 浩然119 - 博客园 https://www.cnblogs.com/pegasus923/p/5528520.html 本周计划完成Sorting and Searching。 Explore - LeetCode - Sorting and Searching https://leetcode.com/explore/interview/card/top-interview-questions-medium/110/sorting-and-searching/ 学习笔记之Problem Solving with Algorithms and Data Structures using Python - 浩然119 - 博客园 https://www.cnblogs.com/pegasus923/p/10454395.html Database / Linux / Machine Learning / Python / MISC 本周有进展。 学习笔记之Database / SQL - 浩然119 - 博客园 https://www.cnblogs.com/pegasus923/p/1954146.html 学习笔记之Linux / Shell / QSHELL - 浩然119 - 博客园 https://www

[LeetCode] 214. 最短回文串

送分小仙女□ 提交于 2019-11-28 13:50:27
题目链接: https://leetcode-cn.com/problems/shortest-palindrome/ 题目链接: 给定一个字符串 s ,你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。 示例: 示例 1: 输入: "aacecaaa" 输出: "aaacecaaa" 示例 2: 输入: "abcd" 输出: "dcbabcd" 思路: 思路一:暴力 1 时间复杂度为: \(O(n^2)\) 思路二:递归 2 时间复杂度为: \(O(n^2)\) 思路三:KMP 3 强烈推荐 如何更好的理解和掌握 KMP 算法? - 海纳的回答 - 知乎 ,多看几遍!用kmp找从位置 0 最长回文子串。 时间复杂度: \(O(n)\) 代码: 思路一: class Solution: def shortestPalindrome(self, s: str) -> str: r = s[::-1] for i in range(len(s) + 1): if s.startswith(r[i:]): return r[:i] + s 思路二: class Solution: def shortestPalindrome(self, s: str) -> str: j = 0 # 找到从头开始,最长的回文子串 for i in range

Leetcode 53,69,151,100

大憨熊 提交于 2019-11-28 13:15:37
53. 最大子序和 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/maximum-subarray 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 class Solution: def maxSubArray(self, nums): if max(nums) < 0: return nums local_max, global_max = 0, 0 for num in nums: local_max = max(0, local_max + num) global_max = max(global_max, local_max) return global_max 69. x 的平方根 实现 int sqrt(int x) 函数。 计算并返回 x 的平方根,其中 x 是非负整数。 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。 示例 1: 输入: 4 输出: 2 示例 2: 输入: 8 输出: 2 说明: 8 的平方根是 2.82842...,

Leetcode 26,27,28, 22, 873

做~自己de王妃 提交于 2019-11-28 12:35:01
26. 删除排序数组中的重复项 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。 示例 1: 给定数组 nums = [1,1,2], 函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。 你不需要考虑数组中超出新长度后面的元素。 示例 2: 给定 nums = [0,0,1,1,1,2,2,3,3,4], 函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 class Solution: def remove_duplicates(self, nums): if not nums: return 0 count = 0 for i in range(len(nums)): if nums[count] != nums[i]: count += 1 nums[count] = nums[i] return count+1

[Swift]LeetCode1165. 单行键盘 | Single-Row Keyboard

廉价感情. 提交于 2019-11-28 12:22:40
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:为敢(WeiGanTechnologies) ➤博客园地址:山青咏芝( https://www.cnblogs.com/strengthen/ ) ➤GitHub地址: https://github.com/strengthen/LeetCode ➤原文地址: https://www.cnblogs.com/strengthen/p/11407048.html ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。 ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创! ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ There is a special keyboard with all keys in a single row. Given a string keyboard of length 26 indicating the layout of the keyboard (indexed from 0 to 25), initially your finger is at index 0. To type a character, you have to move your finger to the index of the

[Swift]LeetCode1166.设计文件系统 | Design File System

混江龙づ霸主 提交于 2019-11-28 12:22:40
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:为敢(WeiGanTechnologies) ➤博客园地址:山青咏芝( https://www.cnblogs.com/strengthen/ ) ➤GitHub地址: https://github.com/strengthen/LeetCode ➤原文地址: https://www.cnblogs.com/strengthen/p/11407049.html ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。 ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创! ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ You are asked to design a file system which provides two functions: create(path, value): Creates a new path and associates a value to it if possible and returns True . Returns False if the path already exists or its parent path doesn't exist. get(path): Returns the

LeetCode338——比特位计数

断了今生、忘了曾经 提交于 2019-11-28 11:52:50
我的LeetCode代码仓: https://github.com/617076674/LeetCode 原题链接: https://leetcode-cn.com/problems/counting-bits/ 题目描述: 知识点:位运算、动态规划 思路一:动态规划 状态定义 :f(x) -------- 数组中索引为x处的值 状态转移 : (1)如果x是偶数,f(x) = f(x / 2)。因为对于偶数来说,其乘以2只是在所有位左移1位并在右边新添一个0,因此其1的个数不会发生变化。 (2)如果x是奇数,f(x) = f(x - 1)。奇数的1的个数一定和比它小的最大偶数的1的个数相同。 时间复杂度和空间复杂度均是O(n)。 JAVA代码: public class Solution { public int[] countBits(int num) { if (num == 0) { return new int[] {0}; } else if (num == 1) { return new int[] {0, 1}; } int[] result = new int[num + 1]; result[0] = 0; result[1] = 1; for (int i = 2; i < num + 1; i++) { if (i % 2 == 0) { result[i]

LeetCode 338. 比特位计数(Counting Bits)

☆樱花仙子☆ 提交于 2019-11-28 11:52:30
LeetCode.jpg 338. 比特位计数 338. 比特位计数 给定一个非负整数 num。对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回。 示例 1: 输入: 2 输出: [0,1,1] 示例 2: 输入: 5 输出: [0,1,1,2,1,2] 进阶: 给出时间复杂度为O(n*sizeof(integer))的解答非常容易。但你可以在线性时间O(n)内用一趟扫描做到吗? 要求算法的空间复杂度为O(n)。 你能进一步完善解法吗?要求在C++或任何其他语言中不使用任何内置函数(如 C++ 中的 __builtin_popcount)来执行此操作。 Python3 实现 # @author:leacoder # @des: 比特位计数 class Solution: def countBits(self, num: int) -> List[int]: res = [0] * (num + 1) for i in range(1,num+1): res[i] = res[i&(i-1)] + 1 return res GitHub链接: https://github.com/lichangke/LeetCode 知乎个人首页: https://www.zhihu.com/people/lichangke/ 简书个人首页:

简单动态规划的实现(Leetcode 338. 比特位计数)

无人久伴 提交于 2019-11-28 11:51:00
背景 想到五月就是蓝桥杯决赛了,而自己还是什么都不会的渣渣,感觉还是得花点时间找找代码感觉。听人说蓝桥杯基本上动态规划加贪心算法基本就够省二的水平了,因此还是想从动规入手。 题目描述 题目来源于Leetcode 338.比特位计数。 官方描述 给定一个非负整数 num。对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回。 示例 1: 输入: 2 输出: [0,1,1] 示例 2: 输入: 5 输出: [0,1,1,2,1,2] 进阶: 给出时间复杂度为O(n*sizeof(integer))的解答非常容易。但你可以在线性时间O(n)内用一趟扫描做到吗? 要求算法的空间复杂度为O(n)。 你能进一步完善解法吗?要求在C++或任何其他语言中不使用任何内置函数(如 C++ 中的 __builtin_popcount)来执行此操作。 题目分析 第一眼看到这个题以为是要我从计算二进制的角度去找计算的规律,但是稍微列了两三个数就发现没有必要,规律十分明显。 简单计算出了16个数就能够直观的看出来,前2 3 个数中,后2 2 个数的1的个数恰好都是前2 2 个数的1的个数加1得到的。当然对于8、4、2个数也成立。 其实从数学的角度也完全可以分析出规律,任何一个数n,都能写成2 i +k的形式,这里的k是n对2 i 取余的值,而2 i