摘要: Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array. Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 → 0class Solution {p 阅读全文
posted @ 2013-09-18 17:03 懒猫欣 阅读(140) 评论(0) 推荐(0)
摘要: Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the target is not found in the array, return [-1, -1]. For example,Given [5, 7, 7, 8, 8, 10] and target value 8,return [3, 4].cl 阅读全文
posted @ 2013-09-18 16:54 懒猫欣 阅读(125) 评论(0) 推荐(0)
摘要: Write a function to find the longest common prefix string amongst an array of strings.class Solution {public: string longestCommonPrefix(vector &strs) { // Start typing your C/C++ solution below // DO NOT write int main() function if(strs.size()==0)return ""; int minSi... 阅读全文
posted @ 2013-09-17 23:55 懒猫欣 阅读(138) 评论(0) 推荐(0)
摘要: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.You may not alter the values in the nodes, only nodes itself may be changed.Only constant memory is all 阅读全文
posted @ 2013-09-17 23:37 懒猫欣 阅读(120) 评论(0) 推荐(0)
摘要: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed./** * Definition 阅读全文
posted @ 2013-09-17 21:50 懒猫欣 阅读(146) 评论(0) 推荐(0)
摘要: Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?Note:You may only use constant extra space. For example,Given the following binary tree, 1 / \ 2 3 / \ \ 4 5... 阅读全文
posted @ 2013-09-17 18:30 懒猫欣 阅读(143) 评论(0) 推荐(0)
摘要: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.Initially, all next pointers are set to NULL... 阅读全文
posted @ 2013-09-17 16:46 懒猫欣 阅读(96) 评论(0) 推荐(0)
摘要: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the array.class Solution {public: bool search(int A[], int n, int target) { if(n==0)return false; ... 阅读全文
posted @ 2013-09-17 15:54 懒猫欣 阅读(98) 评论(0) 推荐(0)
摘要: Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the array return its index, otherwise return -1.You may assume no duplicate exists in the array.class Solution {public: in... 阅读全文
posted @ 2013-09-17 15:42 懒猫欣 阅读(129) 评论(0) 推荐(0)
摘要: Given 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 and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 return[ [5,4,11,... 阅读全文
posted @ 2013-09-17 11:50 懒猫欣 阅读(168) 评论(0) 推荐(0)