上一页 1 ··· 7 8 9 10 11 12 13 14 15 16 下一页
摘要: Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space to hold additional elements from B.The number of elements initialized in A and B are m and n respectively.Solution: From back to forth. 1 class Solution { 2 public: 3 void merg... 阅读全文
posted @ 2014-04-05 00:38 beehard 阅读(151) 评论(0) 推荐(0) 编辑
摘要: Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new length.Solution: Refactor: Update solution. Use two pointers. 1 class Solution { 2 public: 3 int removeElement(i 阅读全文
posted @ 2014-04-05 00:37 beehard 阅读(104) 评论(0) 推荐(0) 编辑
摘要: Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses constant space.Solution: Although we can only use constant space, we can still exchange elements within input A!Swap ele 阅读全文
posted @ 2014-04-05 00:36 beehard 阅读(129) 评论(0) 推荐(0) 编辑
摘要: Given a sorted array, remove the duplicates in place such that each element appear only once and 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,1,2],Your function should return length = 2, and A 阅读全文
posted @ 2014-04-05 00:28 beehard 阅读(141) 评论(0) 推荐(0) 编辑
摘要: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0).Find two lines, which together with x-axis forms a container, such that the container contains the most water.N 阅读全文
posted @ 2014-04-05 00:27 beehard 阅读(149) 评论(0) 推荐(0) 编辑
摘要: Reverse a linked list from position m to n. Do it in-place and in one-pass.For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4,return 1->4->3->2->5->NULL.Note:Given m, n satisfy the following condition:1 next; 9 }10 ListNode* cur = pre->next;11 for(int i = 0; i ne 阅读全文
posted @ 2014-04-05 00:09 beehard 阅读(131) 评论(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 @ 2014-04-05 00:08 beehard 阅读(118) 评论(0) 推荐(0) 编辑
摘要: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2->3. 1 class Solution { 2 public: 3 ListNode *delet 阅读全文
posted @ 2014-04-05 00:07 beehard 阅读(137) 评论(0) 推荐(0) 编辑
摘要: Given a singly linked listL:L0→L1→…→Ln-1→Ln,reorder it to:L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given{1,2,3,4}, reorder it to{1,4,2,3}. 1 class Solution { 2 public: 3 void reverseList(ListNode* &head) 4 { 5 ListNode* cur = hea... 阅读全文
posted @ 2014-04-05 00:00 beehard 阅读(103) 评论(0) 推荐(0) 编辑
摘要: Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up:Can you solve it without using extra space? 1 class Solution { 2 public: 3 bool isCycle(ListNode* head) { 4 if (!head) { 5 return false; 6 } 7 ListNode* slo... 阅读全文
posted @ 2014-04-04 23:59 beehard 阅读(121) 评论(0) 推荐(0) 编辑
上一页 1 ··· 7 8 9 10 11 12 13 14 15 16 下一页