leetcode

LeetCode第283题移动零(Python)

匿名 (未验证) 提交于 2019-12-02 22:51:30
LeetCode第283题移动零(Python) 题目描述 解题方法和思路 设定两个索引,交换元素 题目描述 给定一个数组 nums ,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 示例1: 输入 : [ 0 , 1 , 0 , 3 , 12 ] 输出 : [ 1 , 3 , 12 , 0 , 0 ] 说明: 必须在原数组上操作,不能拷贝额外的数组。 尽量减少操作次数。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/move-zeroes/ 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 解题方法和思路 带*的表示参考其他人的是实现 设定两个索引,交换元素 思路: 初始化索引 p 和 q ,位置为数组中第一个 0 元素出现的地方的索引,例如如下所示: [ 0 , 1 , 0 , 3 , 12 ] ^ p = q 前进 q 索引,碰到非零元素则和 p 索引交换,交换完成后 p 索引自加1 复杂度: 时间复杂度: O ( n ) O(n) O ( n ) 空间复杂度:无需额外数据空间, O ( 1 ) O(1) O ( 1 ) 代码: class Solution : def moveZeroes ( self , nums : List [ int ] ) - > None :

leetcode 5190. 反转每对括号间的子串(C++、python)

匿名 (未验证) 提交于 2019-12-02 22:51:30
给出一个字符串 s (仅含有小写英文字母和括号)。 请你按照从括号内到外的顺序,逐层反转每对匹配括号中的字符串,并返回最终的结果。 注意,您的结果中 不应 包含任何括号。 示例 1: 输入: s = "(abcd)" 输出: "dcba" 示例 2: 输入: s = "(u(love)i)" 输出: "iloveu" 示例 3: 输入: s = "(ed(et(oc))el)" 输出: "leetcode" 示例 4: 输入: s = "a(bcdefghijkl(mno)p)q" 输出: "apmnolkjihgfedcbq" 提示: 0 <= s.length <= 2000 s 中只有小写英文字母和括号 我们确保所有括号都是成对出现的 C++ class Solution { public : string reverseParentheses ( string s ) { string res ; int n = s . length (); vector <string> vec ; for ( int i = 0 ; i < n ; i ++) { if ( '(' == s [ i ]) { vec . push_back ( "" ); } else if ( ')' == s [ i ]) { string str = vec . back (); vec .

Python3解leetcode Lowest Common Ancestor of a Binary Search Tree

匿名 (未验证) 提交于 2019-12-02 22:51:30
问题描述: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia : “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).” Given binary search tree: root = [6,2,8,0,4,7,9,null,null,3,5] Example 1: Input : root = [ 6 , 2 , 8 , 0 , 4 , 7 , 9 , null , null , 3 , 5 ], p = 2 , q = 8 Output : 6 Explanation : The LCA of nodes 2 and 8 is 6. Example 2: Input : root = [ 6 , 2 , 8 , 0 , 4 ,

Leetcode刷题第一天

匿名 (未验证) 提交于 2019-12-02 21:45:52
Leetcode第一题:两数之和   这道题目还是比较简单的,首先想到的就是暴力的破解法,直接使用两个For循环进行遍历,当找到两数相加为target时,我们就可以输出结果了。      在leetcode上还有其他的解法,比如利用hashmap等,今天晚上再来一起探讨! 转载请标明出处: Leetcode刷题第一天 文章来源: Leetcode刷题第一天

[leetcode] Palindrome Number

邮差的信 提交于 2019-12-02 19:12:31
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case? There is a more generic way of solving this problem. 思路1(不推荐):利用Reverse Integer的方法,求的转换后的数字,然后比较是否相等。提示说这样有溢出的问题,想了想感觉问题不大,leetcode也过了。因为首先输入必须是一个合法的int值,负数直接返回false,对于正数

LeetCode 73 矩阵置零

心已入冬 提交于 2019-12-02 18:17:14
问题: 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0。请使用原地算法。 示例 1: 输入: [ [1,1,1], [1,0,1], [1,1,1] ] 输出: [ [1,0,1], [0,0,0], [1,0,1] ] 示例 2: 输入: [ [0,1,2,0], [3,4,5,2], [1,3,1,5] ] 输出: [ [0,0,0,0], [0,4,5,0], [0,3,1,0] ] 进阶: 一个直接的解决方案是使用 O(mn) 的额外空间,但这并不是一个好的解决方案。 一个简单的改进方案是使用 O(m + n) 的额外空间,但这仍然不是最好的解决方案。 你能想出一个常数空间的解决方案吗? 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/set-matrix-zeroes 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 分析: 分析可知,要遍历整个矩阵,判断每个元素是不是零,要是零的话就可以直接更新相应元素为零,或者可以将对应的行号和列号存储,等遍历结束再置零。 1.那按照节约空间的想法肯定是采用第一个想法,但是第一个想法不能将相应的元素直接置零,因为那样的话和被置零元素相关的元素也会被置零,所以先置一个-100000,等完事后再遍历矩阵,要是

数据结构与算法学习大纲

限于喜欢 提交于 2019-12-02 18:17:11
  [TOC]      学习一门语言就练练下面的数据结构和算法.      01链表      1. 链表的必备知识要点(包括基础知识、刷题中使用的STL等知识)      2. 链表逆序(LeetCode 92,206. Reverse Linked List 1,2)      3. 求两个链表的交点(LeetCode 160. Intersection of Two Linked Lists)      4. 链表的节点交换(LeetCode 24. Swap Nodes in Pairs)      5. 链表求环(LeetCode 141,142. Linked List Cycle 1,2)      6. 链表重新构造(LeetCode 86. Partition List)      7. 复杂的链表复制(LeetCode 138. Copy List with Random Pointer)      8. 排序链表合并(2个与多个) (LeetCode 21,23 Merge Two(k) Sorted ListsLeetCode)      02栈、队列、堆      1. 栈、队列知识要点与实现(数组、链表)      2. 使用队列实现栈(LeetCode 232. Implement Queue using Stacks)      3.

【LeetCode】230. Kth Smallest Element in a BST

╄→尐↘猪︶ㄣ 提交于 2019-12-02 18:14:44
Difficulty: Medium More: 【目录】LeetCode Java实现 Description https://leetcode.com/problems/kth-smallest-element-in-a-bst/ Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. Example 1: Input: root = [3,1,4,null,2], k = 1 3 / \ 1 4 \ 2 Output: 1 Example 2: Input: root = [5,3,6,2,4,null,null,1], k = 3 5 / \ 3 6 / \ 2 4 / 1 Output: 3 Follow up: What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the

刷14道leetcode的总结

时光毁灭记忆、已成空白 提交于 2019-12-02 18:08:57
引子 为什么我要刷leetcode?换工作?不是!那是?玩!巴菲特的双目标清单系统,基本方法是列两个清单,一个是职业生涯最重要的目标(不超过5个),另一个是比较重要的目标。对于比较重要的目标,要像躲避瘟疫一样的去躲避它们,不投入任何的时间和精力,把这些资源花在最重要的目标上。这个双目标清单系统以方法的形式说明三件事: 1,目标很重要 2,专注很有力量 3,比起「有所为」、「有所不为」更为关键 所以当我想做一件事情,但是想不到这件事情能给我带来实在的收益时,我就定义为「玩」。就像看电影、喝奶茶一样。我只是去做这件事,在有闲暇的时候占用一些时间,并不占用精力。 我工作十二年,技术一直不好。虽然对一部分人来说还算可以,在美团工作、百余项技术发明专利、kubernetes源码贡献者。但是即使在名声每况愈下的母校同届同专业毕业生中,也算是菜的。原来我之前一直定义为没有天赋。在毕业的第一家公司,被作为技术招进来,但是因为语言学的太好做的是翻译的工作。一年过日语1级。参加任何和日语相关的比赛,只要我参加,第一名肯定是我的,没别人什么事儿。别人说这是天赋,我那时候没想过这个问题,只是不由自主的把精力都花在了这上面。而对于工作,只是完成任务。对于技术,只是用到才去学。很久之后我才说服自己:我的没有天赋只是没投入精力而已。在一件事上有多少天赋取决于在这上面成功的渴望有多强烈。所以现在,我有了一个重心

LeetCode解题汇总目录

a 夏天 提交于 2019-12-02 17:37:52
此篇为学习完 《数据结构与算法之美》 后,在 LeetCode 刷题的汇总目录,方便大家查找(Ctrl+F ind ),一起刷题,一起PK交流!Updated on 2019.12.2 刷题可以按照 不同的专题 进行,便于加强某个知识点的理解。 我的 LeetCode 主页 我的 GitHub 主页 已解决 519/1185 - 简单 281 中等 208 困难 30 2019.7.24 - 2019.11.9,108天做了400道题 2019.11.9 - 2019.11.24, AC+100道,4个月共计500道题 参赛记录 LeetCode 2019 力扣杯全国秋季编程大赛 ​ 已解题目,部分太简单的没有列出 LeetCode 1. 两数之和(哈希) LeetCode 2. 两数相加(单链表反转) LeetCode 3. 无重复字符的最长子串(滑动窗口+哈希) LeetCode 4. 寻找两个有序数组的中位数(二分查找,难) LeetCode 7. 整数反转 LeetCode 8. 字符串转换整数 (atoi) LeetCode 9. 回文数 LeetCode 11. 盛最多水的容器(双指针) LeetCode 14. 最长公共前缀 LeetCode 15. 三数之和 LeetCode 16. 最接近的三数之和(固定左端+滑动窗口) LeetCode 17.