![](https://img-blog.csdnimg.cn/20201014180756930.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
文章平均质量分 53
一的三次方
这个作者很懒,什么都没留下…
展开
-
设计 | 数据流中的中位数
题目求数据流的中位数设计一个类,包含addNum和findMedian两个方法,findMedian时间复杂度优化到O(1)方法最简单的方法 - 排序,每次addNum时,元素追加到list末尾;findMedian时快速排序,然后求得中位数;addNum和findMedian的时间复杂度分别是O(n) 和 O(nlogn);提供这个答案明显还不够;如何优化findMedian的时间...原创 2020-02-22 15:53:06 · 185 阅读 · 0 评论 -
leetcode|152. Maximum Product Subarray[动态规划]
ProblemGiven an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product.Example 1:Input: [2,3,-2,4]Output: 6Explanation: [2...原创 2018-12-09 17:39:29 · 271 阅读 · 0 评论 -
Leetcode | 947. Most Stones Removed with Same Row or Column [Union-Find/DPS]
Problemhttps://leetcode.com/problems/most-stones-removed-with-same-row-or-column/分析https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/197668/Count-the-Number-of-Islan...原创 2018-12-01 19:02:12 · 437 阅读 · 0 评论 -
leetcode|37. Sudoku Solver
Sudoku SolverWrite a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character ‘.’.You may assume that there will be only one unique solution.const sta...原创 2018-11-18 12:06:30 · 100 阅读 · 0 评论 -
Leetcode|79. Word Search (深度优先DFS)
ProblemGiven a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or verti...原创 2018-11-24 12:49:00 · 276 阅读 · 0 评论 -
leetcode|212. Word Search II (字典树Trie && DFS)
ProblemGiven a 2D board and a list of words from the dictionary, find all words in the board.Each word must be constructed from letters of sequentially adjacent cell, where “adjacent” cells are thos...原创 2018-11-24 11:39:42 · 376 阅读 · 0 评论 -
Leetcode|【69】Sqrt(x) |二分查找
Implement int sqrt(int x).Compute and return the square root of x.解法有两种:第一种是二分查找,注意边界上限可以从x/2+1开始查找。第二种方法是牛顿迭代法 解法1:int sqrt(int n){//二分查找,8ms int res=n/2+1; int x=0; while(x<=res){ int mid=(x+原创 2015-07-06 19:04:16 · 496 阅读 · 0 评论 -
Leetcode|【4】Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).这题不简单,通用的方法是写出一个找到两个数组第K大的函数。这原创 2015-09-08 19:25:09 · 365 阅读 · 0 评论 -
Leetcode|Sliding Window Maximum(multiset,优先队列,双端队列和区间树的应用)
Given an arraynums, there is a sliding window of sizekwhich is moving from the very left of the array to the very right. You can only see theknumbers in the window. Each time the sliding window m...原创 2015-09-05 17:09:33 · 526 阅读 · 0 评论 -
Leetcode|Reverse Integer(string转char*总结)
Reverse digits of an integer.Example1: x = 123, return 321 Example2: x = -123, return -321思路是转化为字符串。溢出的判断要进行字符串比较;这里string类型不能直接用strcmp;如果想用的话,需要类型转换。 下面我会说明到底怎么比较字符串。int strcmp(const char str1,原创 2015-07-11 10:05:52 · 322 阅读 · 0 评论 -
Leetcode|Longest Palindromic Substring(最长回文的几种方法)(Manacher算法)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.解法1:枚举法 O(n^2)时间复杂原创 2015-08-30 12:03:12 · 446 阅读 · 0 评论 -
Leetcode|Combination Sum III[回溯]
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.Ensure that numbers within原创 2015-08-02 20:34:50 · 338 阅读 · 0 评论 -
Leetcode|Combination Sum II[递归回溯]
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the combination.No原创 2015-08-02 20:26:47 · 324 阅读 · 0 评论 -
Leetcode|Find Minimum in Rotated Sorted Array[二分查找]
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the minimum element.You may assume no duplicate exists in the array.参考lee原创 2015-08-01 17:12:57 · 257 阅读 · 0 评论 -
Leetcode|Find Minimum in Rotated Sorted Array II(有重复元素的二分查找)
Follow up for “Find Minimum in Rotated Sorted Array”: What if duplicates are allowed?Would this affect the run-time complexity? How and why? Suppose a sorted array is rotated at some pivot unknown to原创 2015-08-01 17:15:34 · 249 阅读 · 0 评论 -
Leetcode|Reorder List
Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes’ values.For example, Given {1,2,3,4}, reorder it to {1,4,2,3}原创 2015-07-30 23:32:18 · 204 阅读 · 0 评论 -
leetcode| 958. Check Completeness of a Binary Tree
今天参加周赛的题目:完全二叉树的定义:Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as ...原创 2018-12-16 21:58:23 · 347 阅读 · 0 评论 -
leetcode|191.Number of 1 Bits(位运算)
这是位运算的题目,这类题目相对简单,先从这里开始1)常规方法:右移,判断最低位是否为1,终止条件是值为0;最多移动32次(由最高位1位置决定);2)优化方法:清除最低位的1,加快收敛速度,只需要计算1的位数的次数。核心:n&=(n-1) 可以清除最低位的1;public class NumberOf1Bits { public int hammingWeight(int n...原创 2018-12-09 23:13:52 · 239 阅读 · 0 评论 -
leetcode | 112. Path Sum(递归&&迭代)
Problemhttps://leetcode.com/problems/path-sum/Example:Given the below binary tree and sum = 22,5/ 4 8/ / 11 13 4/ \ 7 2 1return true, as there exist a root-to-leaf path ...原创 2018-12-24 22:15:30 · 250 阅读 · 0 评论 -
leetcode |4. Median of Two Sorted Arrays(分治/二分查找)
解法1 排序假排序,只排到前一半的数即可。时间复杂度:O(m + n) public double findMedianSortedArrays(int[] nums1, int[] nums2) { int l1 = nums1.length; int l2 = nums2.length; int total = l1 + l2;...原创 2019-03-25 23:22:30 · 112 阅读 · 0 评论 -
leetcode | 305. Number of Islands II (并查集)
题目Input: m = 3, n = 3, positions = [[0,0], [0,1], [1,2], [2,1]]Output: [1,1,2,3]题目描述:m*n的网格区域,刚开始都是水,addLand操作会把某个区域变成陆地;动态求出所有addLand操作后的陆地连通区域数量;分析DFS/BFS不适合处理动态连通问题; 如下UF解法-时间均摊复杂度非常接近O(k) ,时...原创 2019-03-17 22:47:28 · 393 阅读 · 0 评论 -
leetcode | 301. Remove Invalid Parentheses (BFS)
题目Input: "()())()"Output: ["()()()", "(())()"]Input: "(a)())()"Output: ["(a)()()", "(a())()"]Input: ")("Output: [""]题目描述:去除最小数量的无效括号,保证输入的括号有效,输出所有可能。分析此题适合BFS,按照删除原创 2019-03-17 22:38:36 · 155 阅读 · 0 评论 -
leetcode| 323. Number of Connected Components in an Undirected Graph(并查集/DFS/BFS)
题目题目描述:一张含n节点的无向图,以及一组无向边,编写一个函数来查找无向图中连接的组件的数量。Input: n = 5 and edges = [[0, 1], [1, 2], [3, 4]] 0 3 | | 1 --- 2 4 Output: 2分析解法DFS/BFS/并查集,DFS和BFS时间复杂度均为O...原创 2019-03-17 22:36:15 · 610 阅读 · 0 评论 -
链表反转
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */链表尤其是单向链表是一个比较简答的问题,一般容易出错的点是对结尾处NU...原创 2015-06-20 12:51:23 · 527 阅读 · 0 评论 -
leetcode | Binary Tree Maximum Path Sum
题目1:Binary Tree Maximum Path Sum 题目要求: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. 就是求出值最大的路径。这个题很难。最开始我做的时候,没有充分考虑到负数存在的情况。原创 2015-05-29 22:46:58 · 254 阅读 · 0 评论 -
链表的基本操作应用2---remove
链表经常需要删除的一些操作。虽然简单但是容易出错。1,Remove Nth Node From End of ListFor example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked...原创 2015-06-20 20:20:10 · 412 阅读 · 0 评论 -
leetcode | 685. Redundant Connection II (有向图 - 并查集)
题目找出有向图中,多余的连接;确保所有点都相连 && 每个节点只有一个parent(不包含root节点);如果有多个solution,去除靠后的连接;【题目保证去除一个连接,就可以满足要求】Input: [[1,2], [1,3], [2,3]]Output: [2,3]Explanation: The given directed graph will be like t...原创 2019-03-16 14:17:18 · 250 阅读 · 0 评论 -
leetcode | 928. Minimize Malware Spread II (Hard UF/DFS)
题目网络中,某个节点被感染,可以传染给相邻的节点,给定n个节点,以及initial表示的初始感染节点;M(initial):全网中最终被感染的节点的数量;问题:如果从initial中移除一个节点,同时也会移除该节点与相邻节点的连接;返回可以最小化M(initial)的节点;如果不止一个答案,返回最小的那个节点;Input: graph = [[1,1,0],[1,1,0],[0,0,1]...原创 2019-03-20 08:14:23 · 208 阅读 · 0 评论 -
leetcode | 200. Number of Islands (并查集)
题目Given a 2d grid map of '1’s (land) and '0’s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may ass...原创 2019-02-24 16:51:44 · 314 阅读 · 0 评论 -
leetcode | 983. Minimum Cost For Tickets (动态规划)
最近做的leetcode有点多,没时间更新博客,后面多做一些基础算法题;今天上午周赛做了一道DP题目,不过当时没有想到使用DP,囧题目给定一个数组days,表示今年的旅行计划,你可以买单日票,周票和月票求:旅行的最低花费如下特点:1 <= days.length <= 365;1 <= days[i] <= 365,元素特点是单调递增的。Input: ...原创 2019-01-27 20:18:12 · 1075 阅读 · 0 评论 -
leetcode | 974. Subarray Sums Divisible by K (subArray系列1)
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.Example 1:Input: A = [4,5,0,-2,-3,1], K = 5Output: 7Explanation: There are 7 sub...原创 2019-01-13 20:07:42 · 174 阅读 · 0 评论 -
leetcode | 969. Pancake Sorting(sorting)
https://leetcode.com/problems/pancake-sorting/周赛week118题目,是个水题。要求:将给定的数组进行排序,要求是每次只能reverse 数组前缀部分[0, …, i],例子如下:Input: [3,2,4,1]Output: [4,2,4,3]Explanation:We perform 4 pancake flips, with k v...原创 2019-01-06 17:32:42 · 505 阅读 · 1 评论 -
leetcode | 971. Flip Binary Tree To Match Preorder Traversal(DFS/preorder)
https://leetcode.com/problems/flip-binary-tree-to-match-preorder-traversal/分析:题目要求,如何通过swap左右节点的方式,得到固定的前序遍历的数组解法:这种题目,直接dfs就好,当然也可以用一个stack来迭代;唯一需要考虑的是left为空,是preorder是直接遍历right的,这种情况不需要swap;复杂度分析...原创 2019-01-06 17:25:01 · 246 阅读 · 0 评论 -
Leetcode|Palindrome Linked List
Given a singly linked list, determine if it is a palindrome.Follow up: Could you do it in O(n) time and O(1) space? 满足时间和空间复杂度真得动脑想想啦! 解法1:翻转整个链表,存储翻转链表,从头比较。遍历两边了,空间O(n)也不行。解法2:借助栈,先找到中点,然后把前半部压入栈。原创 2015-07-30 23:36:09 · 214 阅读 · 0 评论 -
Leetcode|Pow(x,n)
Implement pow(x, n). x是double类型,n是int类型;边界条件:x==0和n==0 n为负数: 结尾取个倒数即可 效率问题: 解法1:一个一个乘,肯定超时。 解法2:用2的m次方和n比较。因为n可以表示成2的m次方的多项式相加的形式。 问题是:表示2的n次方(用移位表示),1<<30是(INT_MAX/2) ,1<<31 就是INT_MIN。 但是n==INT原创 2015-08-10 16:45:02 · 392 阅读 · 0 评论 -
Leetcode|Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *.Example 1 Input:原创 2015-08-08 13:29:32 · 262 阅读 · 0 评论 -
Leetcode|Largest Number
Given a list of non negative integers, arrange them such that they form the largest number.For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.Note: The result may be ve原创 2015-06-29 11:03:21 · 229 阅读 · 0 评论 -
Leetcode|Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.这是归并排序链表的基础。代码如下(C++):ListNode* mergeTwoLists(Lis原创 2015-06-28 16:21:11 · 215 阅读 · 0 评论 -
Leetcode|Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold add原创 2015-06-28 15:43:10 · 315 阅读 · 0 评论 -
Leetcode|Anagrams
Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.anangrams:举例说明,就是如果几个单词里面的元素完全相同(种类和个数),只是排列方式不同而形成的不同单词。他们就是anangrams;比如;r原创 2015-06-29 15:45:23 · 290 阅读 · 0 评论