
leetcode
binling
这个作者很懒,什么都没留下…
展开
-
LeetCode 827 最大人工岛 题解
给你一个大小为 n x n 二进制矩阵 grid。最多 只能将一格 0 变成 1。返回执行此操作后,grid 中最大的岛屿面积是多少?岛屿 由一组上、下、左、右四个方向相连的 1 形成。经典的图论中连同分量的题目。原创 2022-09-18 16:39:49 · 385 阅读 · 1 评论 -
预处理在快速判断两个单词是否有相同字符的应用
这里面利用到单词的字符集是有限的这一特性,比如,英文单词就是26个字母,可以把一个单词包含的字母的情况,转化为一个26位的位图,两个单词如果有相同的字母,那么相应的bit位置上就同为1。把位图当成一个数,两个位图做一个“与”操作,结果为0就得出对应的单词无重复字符。,一般的做法是,采用辅助的集合类容器,先遍历一个字符串,把它的字符放到集合中;如果是一个单词的集合,需要判断两两判断两个单词是否有重复字符,按照上面的算法,总的复杂度变为 O(n * len), len是字符串长度的上限。...原创 2022-08-10 17:50:51 · 371 阅读 · 0 评论 -
基于代数思维的coding
大概有两种思维,一种是面向过程的,就是想象一个数据结构上的操作步骤,推演一下,看看能不能接近一个solution。这种思维有诸多弊端:1)靠灵光一现的乱试,不可控,不可重复2)面向过程,比较复杂、麻烦,易出错3)逻辑性不强,为什么这么操作一番就能得出解? 这个问题很难回答,缺乏一种确信感。离散,不易证明代数的特点是什么?都是变量和公式推导,从不需要带入离散的具体值去验证,是自证明的,或者说你可以代入任何具体值,都是成立的,逻辑性非常强。比如求斐波那契数列的代数式算法:```def原创 2021-08-10 13:15:24 · 155 阅读 · 0 评论 -
LeetCode 32 Longest Valid Parentheses 2种优雅解法
思路一 递归DP定义子问题 : f(i) 为 以s[i] 结尾的子串的原问题,有:如果s[i] == ‘(’:f(i) = 0如果s[i] == ‘)’:如果s[i - 1] == ‘(’:f(i) = f(i - 2) + 2如果s[i - 1] == ‘)’:令 l = f(i -1), 如果s[ i - l - 1] == ‘(’,那么f(i) = f(i - 1) +...原创 2020-03-17 15:28:44 · 174 阅读 · 0 评论 -
Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal is to rea原创 2014-11-25 10:33:10 · 420 阅读 · 0 评论 -
Next Permutation
算法摘要1)从右往左扫描,原创 2014-07-30 12:08:23 · 481 阅读 · 0 评论 -
快速幂
1. 减半递归法计算x^n 先算 half = x^(n/2),如果n是偶数,结果就是 half * half,如果n是奇数,结果就是half * half * x。递归出口 n==1 时候 return 12 非递归(平方法)对于n的每个二进制位,不断平方底数,得到 x^(2^0), x^(2^1), x^(2^2), x^(2^3) 的序列,如果对应二进制位为1,则将这个底原创 2014-12-11 11:41:36 · 441 阅读 · 0 评论 -
Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position. Determine if you a原创 2014-11-25 09:39:23 · 385 阅读 · 0 评论 -
building outline 问题及set, mutilset以及堆的一些理解
1扫描线法,需要考虑的就是当来一条竖线的时候,需要更新什么2本例中,只需要考虑 是否更新当前最高高度的起点curX,是否产生一个输出。还有扫描线法固定的步骤:当起点来时候加进一个高度,当终点来时移除对应的高度。3循环不变式:curX是当前outline的起点,堆里的最大高度就是当前outline的高度。当一个面来时:1)堆是空的(没有建筑)进入一个outline, 记录curX2)原创 2015-05-26 14:37:16 · 936 阅读 · 0 评论 -
Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3原创 2014-12-09 22:41:58 · 386 阅读 · 0 评论 -
Remove Nth Node From End of List
删除倒数第k个节点,和返回倒数第k个原创 2014-08-18 12:17:19 · 450 阅读 · 0 评论 -
Linked List Cycle II
如果一个链表有环,求环入口结点,否则返回NULL原创 2014-08-20 22:41:46 · 451 阅读 · 0 评论 -
Find Minimum in Rotated Sorted Array II
算法正确性证明的三要素1)invariant property 是什么,每步循环保证了invariant property2)证明循环可以退出:(比如,在循环体的每个分支,循环的界都是在收敛)3)根据循环退出的condition和 invariant property 得出answer/* invariant property: the min is within原创 2014-12-30 12:36:43 · 466 阅读 · 0 评论 -
leetcode 287 Find the Duplicate Number
数的取值范围是[1, n],取n + 1个组成一个大小为n+1的数组1)根据鸽巢原理,必然有一个重复的2)求这个重复的数思路一:桶大小为1的桶排序,值和位置一一对应,尝试把每个值放到它对应的位置上,如果某个值不在其位置上,且其位置上已经是这个值,说明这个值重复int findDuplicate(vector& nums) { for (int i = 0; i < num原创 2015-09-29 14:51:54 · 484 阅读 · 0 评论 -
单调栈的应用 Largest Rectangle in Histogram,Max Tree,expression tree, 表达式求值,
单调栈的应用场景:求一侧或两侧比当前元素小(大)的最近的元素。例题:一个数组,对于每个元素,输出左边第一个比它小的元素暴力解法:对于每个元素,往前查看,直到遇到比自己小的,O(n^2)单调栈:维护一个递增栈,满足递增的时候(比栈顶元素大),上一个元素(栈顶元素)就是所求,当新元素不满足递增(比栈顶大),退栈,直到栈顶比当前元素小,输出,然后再把当前元素入栈。每个元素最多入栈一次出栈一次原创 2014-12-12 18:34:56 · 750 阅读 · 0 评论 -
Permutation Sequence
string getPermutation(int n, int k)类似康拓展开的思路。原创 2014-07-30 15:44:03 · 435 阅读 · 0 评论 -
LRU Cache
怎么去想:一般的cache就是一个原创 2014-08-22 14:39:59 · 512 阅读 · 0 评论 -
Plus One
数组代表一个数,每个元素代表一位,就原创 2014-07-31 15:18:45 · 408 阅读 · 0 评论 -
Container With Most Water
一个竖着的线段列表,求任意两个线段原创 2014-08-23 17:25:40 · 399 阅读 · 0 评论 -
Trapping Rain Water
题目:一个数组,每个元素代表原创 2014-07-31 09:08:07 · 497 阅读 · 0 评论 -
Gas Station
一个环路上有n个加油站,给出一个jia'y原创 2014-10-01 09:42:09 · 426 阅读 · 0 评论 -
Reverse Linked List II
题目:Reverse a linked list from position m to n. Do it in-place and in one-pass.原创 2014-08-11 12:24:38 · 507 阅读 · 0 评论 -
Swap Nodes in Pairs
主框架就是遍历,条件是p && p->next,保证原创 2014-08-18 14:28:38 · 390 阅读 · 0 评论 -
Reverse Nodes in k-Group
主循环就是一节一节的处理,循环变量为原创 2014-08-19 11:28:25 · 535 阅读 · 0 评论 -
Best Time to Buy and Sell Stock III
思路一 正向反向扫描两次的DP关键是两个子问题的定义:1)这个日期为止,交易一次的最大收益2)这个日期开始,交易一次的最大收益问题的解就是遍历一遍,二者和最大值。而两个子问题都是通过基本的dp求解。int maxProfit(vector &prices) { // write your code here if(prices.size()<原创 2014-12-12 19:20:31 · 447 阅读 · 0 评论 -
Copy List with Random Pointer
两种方法,都要扫描两遍map法:1)副原创 2014-08-19 12:57:52 · 476 阅读 · 0 评论 -
Rotate List
rotate a linked list by k. 分析:把链表的后k个原创 2014-08-18 12:34:35 · 183 阅读 · 0 评论 -
Interleaving String
题目:判断一个字符串c是否k原创 2014-08-13 23:12:32 · 455 阅读 · 0 评论 -
Partition List
题目:Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in原创 2014-08-11 20:18:16 · 418 阅读 · 0 评论 -
Rotate Image
顺时针把一个图像翻转90度。原创 2014-07-31 11:40:03 · 427 阅读 · 0 评论 -
Remove Duplicates from Sorted Array II
还是原地重写法。条件是A[原创 2014-07-30 01:04:14 · 350 阅读 · 0 评论 -
Remove Element
从数组中删除满足某种条件的元素,原创 2014-07-30 09:14:08 · 371 阅读 · 0 评论 -
Remove Duplicates from Sorted Array
排序数组的duplicates都是相邻的,原地重写fa原创 2014-07-30 00:29:56 · 383 阅读 · 0 评论 -
4sum
1)排序2)穷举前两个数的组合,保证原创 2014-07-29 11:49:12 · 633 阅读 · 0 评论 -
Search in rotated sorted Array
1)首先确定mid落在前半部分还是后半部分,用A[mi原创 2014-07-25 09:07:48 · 488 阅读 · 0 评论 -
Insertion Sort List
插入排序的算法是,假定前面部分是排好序的,原创 2014-09-23 18:07:06 · 461 阅读 · 0 评论 -
Sort List
Merge Sort链表的merge Sort 就是 查找中间原创 2014-09-24 13:06:12 · 452 阅读 · 0 评论 -
Median of Two Sorted Arrays
O(logn)算法:原创 2014-07-29 19:41:00 · 517 阅读 · 0 评论 -
Spiral Matrix
Given a matrix ofmn elements (mrows, n columns), return all elements of the matrix in spiral order.For example, Given the following matrix:[[ 1, 2, 3 ],[ 4, 5, 6 ],[ 7, 8, 9 ]]You shoul原创 2014-12-29 15:11:54 · 410 阅读 · 0 评论 -
Rotate List
Rotate ListGiven a list, rotate the list to the right by k places, where k is non-negative.原创 2014-09-23 12:00:54 · 488 阅读 · 0 评论