摘要: 对于有序数组进行查找我们可以用二分查找,可以参考我之前写的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 阅读(164) 评论(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 阅读(142) 评论(0) 推荐(0) 编辑
摘要: Two SumGiven an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index 阅读全文
posted @ 2014-03-22 23:00 flowerkzj 阅读(139) 评论(0) 推荐(0) 编辑
摘要: 这一篇是两个全集,从2个排序列表到k个排序列表。先从2个入手。Merge Two Sorted ListsMerge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.思路跟数组时类似,主要都是比较、链表插入操作。代码如下: 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * ... 阅读全文
posted @ 2014-03-22 15:48 flowerkzj 阅读(282) 评论(0) 推荐(0) 编辑
摘要: Merge Sorted ArrayGiven two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space (size that is greater or equal tom+n) to hold additional elements from B. The number of elements initialized in A and B aremandnrespectively.最简单粗暴的思路:1. 开辟数组空间C用以 阅读全文
posted @ 2014-03-22 02:19 flowerkzj 阅读(176) 评论(0) 推荐(0) 编辑
摘要: Path Sum IIGiven a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1return[ ... 阅读全文
posted @ 2014-03-21 20:00 flowerkzj 阅读(122) 评论(0) 推荐(0) 编辑
摘要: Path SumGiven a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 ... 阅读全文
posted @ 2014-03-21 19:51 flowerkzj 阅读(110) 评论(0) 推荐(0) 编辑
摘要: Single NumberGiven an array of integers, every element appearstwiceexcept for one. Find that single one.一个数组中只有一个整数是只出现了一次,其余均为两次,找出那个数。直接用异或即可,因为如果两个数相同的话异或结果为0. 1 class Solution { 2 public: 3 int singleNumber(int A[], int n) { 4 int ret = A[0]; 5 for (int i = 1; i < n; ++i) { 6... 阅读全文
posted @ 2014-03-21 19:42 flowerkzj 阅读(97) 评论(0) 推荐(0) 编辑
摘要: Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.Input:(2 -> 4 -> 3) + (5 -> 6 -> 4)Output:7 -> 0 -> 8大非负整数相 阅读全文
posted @ 2014-03-21 19:38 flowerkzj 阅读(136) 评论(0) 推荐(0) 编辑