摘要: 逆序对(reverse pair) 思想和归并排序的思想一样,时间复杂度是O(nlgn)。 就是在统计逆序对个数的表达式需要注意一下。 具体实现 C++ include include include using namespace std; class Solution { public: //逆 阅读全文
posted @ 2017-03-13 13:29 清水汪汪 阅读(1174) 评论(0) 推荐(1) 编辑
摘要: 二分查找 进行二分查找的前提是序列已经有序,二分查找的时间复杂度为O(lgn)。每次和中间的数字进行比较,如果比较的数比中间的大,就在右边查找,小就在左边查找。 具体实现 C++ include include using namespace std; class Solution { public 阅读全文
posted @ 2017-03-13 10:55 清水汪汪 阅读(182) 评论(0) 推荐(1) 编辑
摘要: Maximum Subarray Question Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given 阅读全文
posted @ 2017-03-12 21:50 清水汪汪 阅读(190) 评论(0) 推荐(1) 编辑
摘要: Merge Sort(归并排序) 思想 利用分治的思想,具体实现也就是递归,不断的将问题话分为更小的子问题,当子问题中规模为1的时候,认为数组已经有序了,然后再将子问题求得的结果不断的合并。也就是将长度为1的两个数组合并到长度为2的数字,依次合并下去。 合并的过程就是依次比较两个数组,将较小的数复制 阅读全文
posted @ 2017-03-12 16:15 清水汪汪 阅读(854) 评论(0) 推荐(1) 编辑
摘要: 插入排序(Insertion Sort) 算法描述 依次比较元素,将元素依次往后移动,挪出一个合适的位置,然后将值放入其中。 具体实现 C++ include include using namespace std; class Solution { public: //插入排序算法 void in 阅读全文
posted @ 2017-03-12 15:05 清水汪汪 阅读(155) 评论(0) 推荐(1) 编辑
摘要: 2 Color Dutch National Flag Problem 问题 a[0..n 1]中包含红元素或蓝元素;重新放置使得 红元素均在蓝元素之前。 循环不变式 每一次循环,a[0...k 1]是红色 实例代码 C++ include include using namespace std; 阅读全文
posted @ 2017-03-12 14:41 清水汪汪 阅读(301) 评论(0) 推荐(1) 编辑
摘要: LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse the first k characters for every 2k characters co 阅读全文
posted @ 2017-03-12 11:54 清水汪汪 阅读(217) 评论(0) 推荐(1) 编辑
摘要: Boyer Moore majority vote algorithm(摩尔投票算法) 简介 Boyer Moore majority vote algorithm(摩尔投票算法)是一种在线性时间O(n)和空间复杂度的情况下,在一个元素序列中查找包含最多的元素。它是以Robert S.Boyer和J 阅读全文
posted @ 2017-03-11 23:32 清水汪汪 阅读(7868) 评论(1) 推荐(2) 编辑
摘要: Question Implement pow(x, n). 解题思路 问题如此简单,第一反应求指数也就是求多个数的乘积,那么直接for循环走起,结果一提交发现超时了。 那么我们就来分析一下,如果用以上方法,那么时间复杂度为O(n), O(n)都超时了,说明时间复杂度还应该更低,多少呢?这个时候可以考 阅读全文
posted @ 2017-03-04 22:21 清水汪汪 阅读(231) 评论(0) 推荐(1) 编辑
摘要: Question Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 解题思路 当拿到这道题的时候,就在想为啥Accept率只有 阅读全文
posted @ 2017-03-04 00:44 清水汪汪 阅读(185) 评论(0) 推荐(1) 编辑