上一页 1 ··· 18 19 20 21 22 23 24 25 26 ··· 30 下一页
摘要: Sort a linked list inO(nlogn) time using constant space complexity.归并排序!用慢指针、快指针来寻找二分位置,这里很容易出现错,要特别注意。 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution {10 ... 阅读全文
posted @ 2014-04-08 14:00 Eason Liu 阅读(137) 评论(0) 推荐(0) 编辑
摘要: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:getandset.get(key)- Get the valu... 阅读全文
posted @ 2014-04-08 13:34 Eason Liu 阅读(287) 评论(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?转自:http://www.cnblogs.com/x1957/p/3406448.html比I麻烦点的就是找到循环开始点TATI只是判断是否循环。要求不使用额外空间(不然hash就可以了按I的思路,我们又慢指针S和快指针F。。。F走两步,S走一步。。。若有环,必定相遇。画个图(很丑勿喷假设在红色凸起的地 阅读全文
posted @ 2014-04-07 20:56 Eason Liu 阅读(1114) 评论(0) 推荐(0) 编辑
摘要: Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, givenn= 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"递归与深搜。 1 class Solution { 2 public: 3 void getRes(vector &a 阅读全文
posted @ 2014-04-07 00:12 Eason Liu 阅读(148) 评论(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?用了最笨的办法。就地转的话应该是一圈一... 阅读全文
posted @ 2014-04-06 22:24 Eason Liu 阅读(1850) 评论(0) 推荐(0) 编辑
摘要: A message containing letters fromA-Zis being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total number of ways to decode it.For example,Given encoded message"12", it cou 阅读全文
posted @ 2014-04-06 14:21 Eason Liu 阅读(270) 评论(0) 推荐(0) 编辑
摘要: 题目描述:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。输入:每个测试案例包括2行:第一行输入一个整数n(1 2 #include 3 using namespace std; 4 5 int main() 6 { 7 int n; 8 long a[100005]; 9 10 while (cin >> n) {11 for (int i=0; i > a[i];13 }14 ... 阅读全文
posted @ 2014-04-06 13:01 Eason Liu 阅读(407) 评论(0) 推荐(0) 编辑
摘要: Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjace... 阅读全文
posted @ 2014-04-06 01:54 Eason Liu 阅读(2417) 评论(0) 推荐(0) 编辑
摘要: The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1is read off as"one 1"or11.11is read off as"two 1s"or21.21is read off as"one 2, thenone 1"or1211.Given an integern, generate thenthsequence.Note: The sequence of inte 阅读全文
posted @ 2014-04-06 00:29 Eason Liu 阅读(198) 评论(0) 推荐(0) 编辑
摘要: Given amxnmatrix, if an element is 0, set its entire row and column to 0. Do it in place.如果不用额外空间的话,可以把第一行与第一列作为标记行与标记列,但是得先确定第一行与第一列本身要不要设为0。 1 class Solution { 2 public: 3 void setZeroes(vector > &matrix) { 4 if (matrix.size() < 1) return; 5 int row = matrix.size(), col = matri... 阅读全文
posted @ 2014-04-05 13:54 Eason Liu 阅读(212) 评论(0) 推荐(0) 编辑
上一页 1 ··· 18 19 20 21 22 23 24 25 26 ··· 30 下一页