
算法
文章平均质量分 88
努力前行的小蜗牛
这个作者很懒,什么都没留下…
展开
-
【leetcode】378. Kth Smallest Element in a Sorted Matrix
378. Kth Smallest Element in a Sorted Matrix378. Kth Smallest Element in a Sorted Matrix题目思路代码)378. Kth Smallest Element in a Sorted Matrix题目题目链接: https://leetcode.com/problems/kth-smallest-eleme...原创 2019-10-22 19:23:08 · 223 阅读 · 0 评论 -
【leetcode】180. Consecutive Numbers
答案select Num as ConsecutiveNums from (select T.Num, max(T.Count) as Count from (select Num, (case when @pre <> (@pre := Num) then @i := 1 else @i := @i + 1 end) as Count from Logs, (select @i:=...原创 2019-10-11 19:38:05 · 189 阅读 · 0 评论 -
【leetcode】 178. Rank Scores
参考https://www.cnblogs.com/rever/p/7149995.html练习做完原创 2019-10-11 19:36:49 · 216 阅读 · 0 评论 -
【leetcode】1179. Reformat Department Table
文章目录答案case有input_expression没有input_expression组函数group bymysql总结讲解参考答案select id, max(case when month = 'Jan' then revenue end) as Jan_Revenue,max(case when month = 'Feb' then revenue end) as Feb_Rev...原创 2019-10-10 17:26:27 · 515 阅读 · 0 评论 -
[LeetCode] 1008. Construct Binary Search Tree from Preorder Traversal (java)
文章目录题目翻译思路代码题目Return the root node of a binary search tree that matches the given preorder traversal.(Recall that a binary search tree is a binary tree where for every node, any descendant of node....原创 2019-08-13 19:41:31 · 369 阅读 · 0 评论 -
找出int数字最高位的1对应的2幂次方
文章目录分析例子源码中的应用分析方法一:从左向右依次取该位bit,找到最高位1。复杂度是O(n), n为该数组的bit位方法二:将该数字的末尾全部置1,然后i - (i >>> 1)即可,复杂度O(1)数字1后面的值都设置为1算法: i |= (i >> 1); i |= (i >> 2); i ...原创 2019-10-29 15:58:41 · 994 阅读 · 0 评论 -
java Integer.bitCount源码分析
算法:统计整数的二进制表达式中的bit位为1的位数普通算法public int bitCount(int num) { int count = 0; do { if ((num & 1) == 1) { count++; } num>>=1; } while (num &g...转载 2019-09-06 11:04:29 · 260 阅读 · 0 评论 -
新浪实习生面试题
200个数组,每个数组100个已排好序的数(从大到小),求出最大的20个数;复杂度。方法一:1、200个数组第一个数快排从大到小排好;200log2002、取出排好序的第一元素为最大元;3、将最大元所在数组第二个数取出,折半查找插入相应位置;log2004、重复2、3,直到找到20个为止。 19log200所以时间复杂度为219log200 空间复杂度200方法二:(面试完之后想的)1、原创 2016-04-25 22:47:20 · 1224 阅读 · 0 评论 -
排序算法--面试总结分析
面试时最常问的就是排序算法,所以总结一下这个过程中我遇到的问题:叙述**排序算法思想,然后写代码;最常问的是快排;各个排序的时间复杂度,一般情况下都说的是平均。首先还是会问快排,nlogn,然后问同样复杂度的排序算法,合并排序、堆排序、shell排序。(刚开始的时候一直特别害怕复杂度,因为感觉自己不清楚,面试过程中突然感觉,只要进行了折半,复杂度就是nlogn。所以知道各个排序的思想,还是很好原创 2016-04-25 22:45:45 · 573 阅读 · 0 评论 -
分治/减治/变治
分治:大的问题分别为规模小的相同的子问题。原问题仅存在于分解出来的某一个子问题中;子问题需要综合处理得出原问题解。减治:一个问题给定实例的解和同样问题较小实例的解之间的关系。减去一个常量;将去一个常数因子;减可变规模。分治减治区别:分治法:求解多个子问题(每个子问题均需要单独求解),合并子问题的解。减治法:求解一个子问题(子问题只需要求一次),扩展子问题的解。例如:求a^n。 分治法原创 2016-04-25 22:29:38 · 2222 阅读 · 3 评论 -
leetcode思路二
Next Permutation 按字典数生成排列数的下一个序列。 算法思想:举例如下 输入:1 4 6 5 3 2 step1:从右往左找到第一个破坏升序(非严格)的元素,此例中为4.记下标为 i step2: 依然从右往左,找到第一个大于4的元素,此例中5,交换4和5. step3:从i+1到最右端,逆置。6 4 3 2 to 2 3 4 6 so,1 5 2原创 2016-04-25 22:27:18 · 325 阅读 · 0 评论 -
leetcode思路一
Maximum Product of Word Lengths 针对stings数组,找出两个字符串中没有公共元素长度成绩的最大值。 自己思路: 双重循环,一个用一个分割,但是如果涉及两个都是重复元素,效率还是很低: str2.split(String.valueOf(str1.charAt(i))).length > 1 正确思路: 因为都是小写字符,所以将两个string类型转化原创 2016-04-25 22:26:39 · 341 阅读 · 0 评论 -
Best Time to Buy and Sell Stock
Best Time to Buy and Sell Stock 有一个price数组,标记第i天交易股票的花费。如果只允许一次交易必须先买后卖,最大收益。 找price数组中的最小的,然后其后续与其的最大差值为最大收益。Best Time to Buy and Sell Stock II 交易任意次数,求整个过程中的最大收益。 price[i]>price[i-1], 则其差值加入收益中原创 2016-04-25 22:26:02 · 479 阅读 · 0 评论 -
leetcode easy
Add Digits 每一位相加,直至得到的数字为个位数。 返回值只可能是0-9,并且0的情况只有输入数字是0. 剩下num,均是按照1-9循环出现的。则: num == 0 ? 0 : (num%9 == 0 ? 9 : num%9).Happy Number 一个数字上的每位的平方相加,循环,直到得到的值为1。 遍历每一位,得到的值,如果此值为1,则返回true,反之,原创 2016-04-25 22:24:37 · 440 阅读 · 0 评论