随笔分类 -  Algorithm

上一页 1 ··· 7 8 9 10 11 12 13 14 下一页

[Leetcode] Trapping Rain Water
摘要:Givennnon-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.Fo... 阅读全文

posted @ 2014-04-10 13:38 Eason Liu 阅读(407) 评论(0) 推荐(0)

[Jobdu] 题目1390:矩形覆盖
摘要:题目描述:我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?输入:输入可能包含多个测试样例,对于每个测试案例,输入包括一个整数n(1 2 using namespace std; 3 4 long long res[71]; 5 6 void init() 7 { 8 res[0] = 1; 9 res[1] = 1;10 for (int i = 2; i > n) {21 cout << res[n] << endl;22 }23 return 0;24... 阅读全文

posted @ 2014-04-09 17:22 Eason Liu 阅读(178) 评论(0) 推荐(0)

[Leetcode] Integer to Roman
摘要:Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.纯模拟:基本字符IVXLCDM相应的阿拉伯数字表示为1510501005001000相同的数字连写,所表示的数等于这些数字相加得到的数,如:Ⅲ = 3;小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数, 如:Ⅷ = 8;Ⅻ = 12;小的数字,(限于Ⅰ、X 和C)在大的数字的左边,所表示的数等于大数减小数得到的数,如:Ⅳ= 4;Ⅸ= 9;正常使用时,连写的数字重复不得超过三 阅读全文

posted @ 2014-04-09 14:38 Eason Liu 阅读(2247) 评论(0) 推荐(0)

[Leetcode] Word Break
摘要:Given a stringsand a dictionary of wordsdict, determine ifscan be segmented into a space-separated sequence of one or more dictionary words.For exampl... 阅读全文

posted @ 2014-04-09 14:09 Eason Liu 阅读(1904) 评论(0) 推荐(0)

[Leetcode] Sort List
摘要: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 阅读(145) 评论(0) 推荐(0)

[Leetcode] LRU Cache
摘要: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 阅读(299) 评论(0) 推荐(0)

[Leetcode] Linked List Cycle II
摘要: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 阅读(1125) 评论(0) 推荐(0)

[Leetcode] Generate Parentheses
摘要: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 阅读(154) 评论(0) 推荐(0)

[Leetcode] Rotate Image
摘要: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 阅读(1863) 评论(0) 推荐(0)

[Leetcode] Decode Ways
摘要: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 阅读(281) 评论(0) 推荐(0)

[Jobdu] 题目1370:数组中出现次数超过一半的数字
摘要:题目描述:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为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 阅读(417) 评论(0) 推荐(0)

[Leetcode] Word Search
摘要: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 阅读(2426) 评论(0) 推荐(0)

[Leetcode] Count and Say
摘要: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 阅读(209) 评论(0) 推荐(0)

[Leetcode] Set Matrix Zeroes
摘要: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 阅读(224) 评论(0) 推荐(0)

[Leetcode] Copy List with Random Pointer
摘要:A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.最开始的想法就是暴力复制,时间复杂度为O(n^2),写的时候就感觉要出现事,果不其然,超时了,后来网上看到一个O(n)的算法,非常巧妙的利用了原来链表的信息:该算法更为巧妙,不用保存原始链表的映射关系,构建新节点时,指针做如下变化,即把新节点插入到相应的旧节点后面:同理分两步 阅读全文

posted @ 2014-04-05 13:34 Eason Liu 阅读(4309) 评论(3) 推荐(0)

[Leetcode] Flatten Binary Tree to Linked List
摘要:Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened t... 阅读全文

posted @ 2014-04-05 01:11 Eason Liu 阅读(1372) 评论(0) 推荐(0)

[Leetcode] Letter Combinations of a Phone Number
摘要:Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit string "23"Output: ["ad", "ae", "af", "bd", "be", &q 阅读全文

posted @ 2014-04-05 00:33 Eason Liu 阅读(223) 评论(0) 推荐(0)

[Leetcode] Distinct Subsequences
摘要:Given a stringSand a stringT, count the number of distinct subsequences ofTinS.A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie,"ACE" 阅读全文

posted @ 2014-04-04 19:57 Eason Liu 阅读(231) 评论(0) 推荐(0)

[Leetcode] Partition List
摘要: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 @ 2014-04-04 18:37 Eason Liu 阅读(152) 评论(0) 推荐(0)

[Leetcode] Reverse Linked List II
摘要: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.no words!!!! 1 /** 2 * Definition for singly-linked li 阅读全文

posted @ 2014-04-04 18:09 Eason Liu 阅读(212) 评论(0) 推荐(0)

上一页 1 ··· 7 8 9 10 11 12 13 14 下一页