2015年4月16日
摘要: 题目链接https://leetcode.com/problems/majority-element/这道题最一开始的思路是建立一个hashmap,这样就是 O(n) time和 O(n) space。但是后来看了一下leetcode给出的官方解答,发现moore voting algorithm很... 阅读全文
posted @ 2015-04-16 03:10 小虎是枪王 阅读(284) 评论(0) 推荐(0) 编辑
摘要: 题目链接https://leetcode.com/problems/merge-sorted-array/这道题是merge sort中merge的过程,有一个trick是从后往前merge,这样可以避免多余的数组操作class Solution {public: void merge(int... 阅读全文
posted @ 2015-04-16 02:28 小虎是枪王 阅读(111) 评论(0) 推荐(0) 编辑
摘要: 题目链接https://leetcode.com/problems/pascals-triangle/https://leetcode.com/problems/pascals-triangle-ii/这两道题都是数组操作,需要注意的是II在I的基础上使用滚动数组存储过往的中间结果,这个思想可以注意... 阅读全文
posted @ 2015-04-16 02:20 小虎是枪王 阅读(155) 评论(0) 推荐(0) 编辑
摘要: 题目链接https://leetcode.com/problems/plus-one/这是digit这类题里最简单的一道了,这类题基本都不难,但是需要把几个boundary case考虑到,这道题里需要考虑的是进位之后首位的情况。其他case以后遇到再提。class Solution {public... 阅读全文
posted @ 2015-04-16 01:55 小虎是枪王 阅读(113) 评论(0) 推荐(0) 编辑
摘要: 题目链接https://leetcode.com/problems/remove-duplicates-from-sorted-array/思路就是维护两个pointer,一个,i, 用于遍历整个数组,另一个,len, 用于保存当前的无重复元素的数组的最后一个positionif A[i] == A... 阅读全文
posted @ 2015-04-16 01:46 小虎是枪王 阅读(131) 评论(0) 推荐(0) 编辑
摘要: 题目链接https://leetcode.com/problems/remove-element/这道题比较简单,为了维护这个leetcode系列的完整性,我依然把它加在这里,code如下class Solution {public: int removeElement(int A[], in... 阅读全文
posted @ 2015-04-16 01:17 小虎是枪王 阅读(112) 评论(0) 推荐(0) 编辑
摘要: 题目链接https://leetcode.com/problems/rotate-array/这道题主要是想明白rotate是怎样完成的,举个例子{1,2,3,4,5,6,7}, k=3我们发现完成rotate 3需要进行一下三步1)reverse array {1,2,3,4,5,6,7} => ... 阅读全文
posted @ 2015-04-16 01:08 小虎是枪王 阅读(174) 评论(0) 推荐(0) 编辑