上一页 1 ··· 8 9 10 11 12 13 14 15 16 下一页
摘要: Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space? 1 class Solution { 2 public: 3 bool hasCycle(ListNode *head) { 4 if (!head) { 5 return false; 6 } 7 ListNode* slow = head; 8 ListNode* fast = h... 阅读全文
posted @ 2014-04-04 23:53 beehard 阅读(105) 评论(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.Solution: 1. Iter 阅读全文
posted @ 2014-04-04 23:49 beehard 阅读(148) 评论(0) 推荐(0) 编辑
摘要: Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3. 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * Lis 阅读全文
posted @ 2014-04-04 23:47 beehard 阅读(133) 评论(0) 推荐(0) 编辑
摘要: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.Solution: Find the smallest list-head first using minimum-heap(lgk).complexity: O(NlgK) 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 ... 阅读全文
posted @ 2014-04-04 23:46 beehard 阅读(114) 评论(0) 推荐(0) 编辑
摘要: Given a linked list, remove the nth node from the end of list and return its head.For example,Given linked list: 1->2->3->4->5, and n = 2.After removing the second node from the end, the linked list becomes 1->2->3->5.Note:Given n will always be valid.Try to do this in one pass. 阅读全文
posted @ 2014-04-04 23:45 beehard 阅读(121) 评论(0) 推荐(0) 编辑
摘要: Merge 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 class Solution { 2 public: 3 ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { 4 ListNode dummy(0), *cur = &dummy; 5 while(l1 && l2) ... 阅读全文
posted @ 2014-04-04 23:43 beehard 阅读(94) 评论(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].Sol 阅读全文
posted @ 2014-04-04 23:36 beehard 阅读(134) 评论(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).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. 1 class Solution { 2 publi 阅读全文
posted @ 2014-04-04 23:34 beehard 阅读(250) 评论(0) 推荐(0) 编辑
摘要: Implement int sqrt(int x).Compute and return the square root of x. 1 class Solution { 2 public: 3 // binary search, overflow values 4 int sqrt(int x) { 5 int i = 0; 6 int j = x / 2 + 1; 7 while (i <= j) 8 { 9 long long mid = i + (j-i) / 2; // not ... 阅读全文
posted @ 2014-04-04 23:31 beehard 阅读(110) 评论(0) 推荐(0) 编辑
摘要: Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most two transactions.Note:You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).Soluti 阅读全文
posted @ 2014-04-04 23:26 beehard 阅读(140) 评论(0) 推荐(0) 编辑
上一页 1 ··· 8 9 10 11 12 13 14 15 16 下一页