随笔分类 - Array/ String
摘要:bst: 加count记住遍历过得点有几个点比当前的点小(作为当前节点的左子树的节点数), 返回右父节点的所有count 和, 向左遍历(想好建好树后最后的点向哪遍历, 向左遍历, 第一个点就遍历了右边的所有的节点) Time: O(NlogN), space: O(N) better than n
阅读全文
摘要:Boyer-Moore Majority Vote algorithm The essential concepts is you keep a counter for the majority number X. If you find a number Y that is not X, the
阅读全文
摘要:数组和字符串的分治法: 对返回值的操作(叶结点的+ 非叶结点的), 出口, 分治起始位置, 题目要求--返回值 难点在于从哪分(题目要求的理解? 中间?符号?)和叶结点情况的处理->返回值 见Different Ways to Add Parentheses 投票法: 根据majority 的特性
阅读全文
摘要:数组的题常用方法 :排序、最大值指针边走边找、最小值指针边走边找,数组的其他指针位置—顺序遍历, 逆序遍历,其他指针与最大值指针比较的后果或最小值指针 先想排序能否做: Stack 画图: 利用相对位置和大小pop找到最左边的位置, 和最右边的位置, 利用栈内的元素都是最小的特点, 找到要排序的su
阅读全文
摘要:二有序数组常用二分法, 要判断数组是否越界, 而数组的题常用递归, 擦半的方法, 比快排的方法, 少一个partition 遍历, 但是还是分成logn 层, Lintcode: Nuts & Bolts Problem 关键在于找到合适的比较位置, 准备擦出哪个数组的一半, 防止数组越界 The
阅读全文
摘要:sorted 数组, 常用二指针遍历, 会倒叙, 会前序 逆序遍历的好处, 1只遍历后面的 2此题在于空间上可以不干扰.
阅读全文
摘要:有序数组的题, 二分法, 根据mid的位置再确定start 和end的位置, 画图分情况 三种情况 The idea is that when rotating the array, there must be one half of the array that is still in sorte
阅读全文
摘要:两个数组两个指针 两个数组的题常sort, 再通过两指针遍历 常常与0 的差的正负来判断, 来判断指针的走位, 此处直接判断大小更方便
阅读全文
摘要:数组常sort
阅读全文
摘要:这道题是一道典型的位操作Bit Manipulation的题目,我开始以为异或值最大的两个数一定包括数组的最大值,但是OJ给了另一个例子{10,23,20,18,28},这个数组的异或最大值是10和20异或,得到30。那么只能另辟蹊径,正确的做法是按位遍历,题目中给定了数字的返回不会超过231,那么
阅读全文
摘要:Let's say nums is [10,11,...,19]. Then after nth_element and ordinary partitioning, we might have this (15 is my median): I rewire it so that the firs
阅读全文
摘要:道题给定我们一个有序数组,让我们总结区间,具体来说就是让我们找出连续的序列,然后首尾两个数字之间用个“->"来连接,那么我只需遍历一遍数组即可,每次检查下一个数是不是递增的,如果是,则继续往下遍历,如果不是了,我们还要判断此时是一个数还是一个序列,一个数直接存入结果,序列的话要存入首尾数字和箭头“-
阅读全文
摘要:Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The
阅读全文
摘要:Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that i < j < k and ai < ak < aj. Design an algorithm th
阅读全文
摘要:Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here ak-diff pair is defined as an integ
阅读全文
摘要:https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/#/solutions Given a string and a string dictionary, find the longest string
阅读全文
摘要:https://leetcode.com/problems/reverse-vowels-of-a-string/#/description http://www.cnblogs.com/EdwardLiu/p/6096319.html
阅读全文
摘要:https://leetcode.com/problems/trapping-rain-water/#/solutions Fb: O(1)space 这种方法是基于动态规划的,基本思路就是维护一个长度为n的数组,进行两次扫描,一次从左往右,一次从右往左。第一次扫描的时候维护对于每一个bar左边最大
阅读全文
摘要:https://leetcode.com/problems/remove-element/#/description Given an array and a value, remove all instances of that value in place and return the new
阅读全文

浙公网安备 33010602011771号