摘要: 删除排序数组重复元素,先来个简单的。Remove Duplicates from Sorted ArrayGiven a sorted array, remove the duplicates in place such that each element appear onlyonceand return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.For example,Given input array A =[1, 阅读全文
posted @ 2014-03-23 20:15 flowerkzj 阅读(141) 评论(0) 推荐(0) 编辑
摘要: 对于有序数组进行查找我们可以用二分查找,可以参考我之前写的Search Insert Position但如果把这个排序的数组当成一个环,然后做一个旋转操作后再截断,再进行查找又会是什么情况呢?且看下文分解。Search in Rotated Sorted ArraySuppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).You are given a target value to search. If foun 阅读全文
posted @ 2014-03-23 18:52 flowerkzj 阅读(166) 评论(0) 推荐(0) 编辑
摘要: 关于循环单链表的两题,先来简单的。Linked List CycleGiven a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?判断是否为循环链表。利用循环链表的周期性,两个步长不一样的指针一定会有相遇的点,而如果无环的话,步长大的一定会先行结束。代码如下: 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ... 阅读全文
posted @ 2014-03-23 16:15 flowerkzj 阅读(224) 评论(0) 推荐(0) 编辑
摘要: Median of Two Sorted ArraysThere are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).求中位数,思想跟归并的一样了,只是不用记录所有的数,只需要记录中间位置的记录,对奇数长度与偶数长度的情况处理下就好。代码如下: 1 class Solution { 2 public: 3 double find... 阅读全文
posted @ 2014-03-23 01:32 flowerkzj 阅读(144) 评论(0) 推荐(0) 编辑