上一页 1 2 3 4 5 6 7 8 9 10 ··· 18 下一页
摘要: Given a linked list, swap every two adjacent nodes and return its head.For example,Given1->2->3->4, you should return the list as2->1->4->3.Your algorithm should use only constant space. You maynotmodify the values in the list, only nodes itself can be changed.利用三个指针p, pPre, pPrePr 阅读全文
posted @ 2012-11-19 17:07 chkkch 阅读(3258) 评论(0) 推荐(1) 编辑
摘要: You are given a string,S, and a list of words,L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.For example, given:S:"barfoothefoobarman"L:["foo", " 阅读全文
posted @ 2012-11-19 16:57 chkkch 阅读(1467) 评论(1) 推荐(0) 编辑
摘要: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving onlydistinctnumbers from the original list.For example,Given1->2->3->3->4->4->5, return1->2->5.Given1->1->1->2->3, return2->3.关键是记录三个指针,当前指针p, p的父亲pPre, pPre的父亲pPrePre。 1 /** 2 * 阅读全文
posted @ 2012-11-19 15:23 chkkch 阅读(1378) 评论(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.首先我们把key设为非头节点的值,然后遍历链表,当节点值等于key时删除,不能于时更新key. 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode 阅读全文
posted @ 2012-11-19 14:58 chkkch 阅读(1208) 评论(0) 推荐(0) 编辑
摘要: Given a linked list and a valuex, partition it such that all nodes less thanxcome before nodes greater than or equal tox.You should preserve the original relative order of the nodes in each of the two partitions.For example,Given1->4->3->2->5->2andx= 3,return1->2->2->4->3- 阅读全文
posted @ 2012-11-19 14:45 chkkch 阅读(1499) 评论(0) 推荐(0) 编辑
摘要: Given an array of words and a lengthL, format the text such that each line has exactlyLcharacters and is fully (left and right) justified.You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces' 'when necessary so that each line 阅读全文
posted @ 2012-11-18 21:08 chkkch 阅读(2211) 评论(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.画图会比较容易理解 1 /** 2 * Definition for singly-linked list. 阅读全文
posted @ 2012-11-18 20:23 chkkch 阅读(4697) 评论(0) 推荐(0) 编辑
摘要: You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?需要O(N)的辅助空间,应该有个O(1)空间的。但似乎比较复杂。 1 class Solution { 2 public: 3 void rotate(vector<vector<int> > &matrix) { 4 // Start typing your C/C++ solution below 5 阅读全文
posted @ 2012-11-18 18:16 chkkch 阅读(746) 评论(0) 推荐(0) 编辑
摘要: The string"PAYPALISHIRING"is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I I GY I RAnd then read line by line:"PAHNAPLSIIGYIR"Write the code that will take a string and 阅读全文
posted @ 2012-11-18 17:40 chkkch 阅读(2891) 评论(0) 推荐(0) 编辑
摘要: Given a linked list, reverse the nodes of a linked listkat a time and return its modified list.If the number of nodes is not a multiple ofkthen 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 allowed 阅读全文
posted @ 2012-11-18 17:02 chkkch 阅读(1633) 评论(0) 推荐(0) 编辑
上一页 1 2 3 4 5 6 7 8 9 10 ··· 18 下一页