摘要: Given a list, rotate the list to the right bykplaces, wherekis non-negative.For example:Given1->2->3->4->5->NULLandk=2,return4->5->1->2->3->NULL.解法:游标指针从头节点向后移动,当指向尾节点时,得到链表长度len,同时将链表头尾相连,接着游标再向后移动 k % len 步得到结果链表的尾节点。 1 /** 2 * Definition for singly-linked list. 3 * s 阅读全文
posted @ 2013-05-14 10:38 infinityu 阅读(766) 评论(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. 1 class Solution { 2 public: 3 int removeElement(int A[], int n, int elem) { 4 // Start typing y... 阅读全文
posted @ 2013-05-13 20:39 infinityu 阅读(137) 评论(0) 推荐(0) 编辑
摘要: Given 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,1,2],Your function should return length =2, and A is 阅读全文
posted @ 2013-05-13 20:24 infinityu 阅读(150) 评论(0) 推荐(0) 编辑
摘要: Write a function to find the longest common prefix string amongst an array of strings. 1 class Solution { 2 public: 3 string longestCommonPrefix(vector<string> &strs) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 stringstream rtn; 7... 阅读全文
posted @ 2013-05-13 19:14 infinityu 阅读(259) 评论(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 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) ... 阅读全文
posted @ 2013-05-12 21:36 infinityu 阅读(194) 评论(0) 推荐(0) 编辑
摘要: Given a sorted linked list, delete all duplicates such that each element appear onlyonce.For example,Given1->1->2, return1->2.Given1->1->2->3->3, return1->2->3. 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode 阅读全文
posted @ 2013-05-12 17:16 infinityu 阅读(182) 评论(0) 推荐(0) 编辑
摘要: Reverse a linked list from positionmton. Do it in-place and in one-pass.For example:Given1->2->3->4->5->NULL,m= 2 andn= 4,return1->4->3->2->5->NULL.Note:Givenm,nsatisfy the following condition:1 ?m?n? length of list.解法:需要定位待翻转部分的头与尾,以及翻转部分相邻的两个节点,同时还要考虑m和n之间的关系。 1 /** 2 阅读全文
posted @ 2013-05-12 17:05 infinityu 阅读(561) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6Hints:If you notice carefully ... 阅读全文
posted @ 2013-05-12 16:09 infinityu 阅读(173) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).Note:Bonus points if you could solve it both recursively and iteratively.解法 1:非递归解法使用双端队列dequeue记录对称的节点,依次从dequeue的头尾取出两个节点进行判断,两个节点的值以及节点左右孩子结构分别相同的情况下,将他们的孩子按顺序加入dequeue。再依次从dequeue的头尾取出两个节点进行判断,直到dequeue 阅读全文
posted @ 2013-05-12 14:50 infinityu 阅读(207) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keysless thanthe node's key.The right subtree of a node contains only nodes with keysgreater thanthe node's key.Both the left and ri 阅读全文
posted @ 2013-05-12 14:20 infinityu 阅读(255) 评论(0) 推荐(0) 编辑