04 2014 档案

[Leetcode] Longest Consecutive Sequence
摘要:Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given[100, 4, 200, 1, 3, 2],The longest ... 阅读全文

posted @ 2014-04-29 17:13 Eason Liu 阅读(187) 评论(0) 推荐(0)

[Leetcode] Valid Number
摘要:Validate if a given string is numeric.Some examples:"0"=>true" 0.1 "=>true"abc"=>false"1 a"=>false"2e10"=>trueNote:It is intended for the problem stat... 阅读全文

posted @ 2014-04-29 16:27 Eason Liu 阅读(1013) 评论(0) 推荐(0)

[Leetcode] Scramble String
摘要:Given a strings1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation... 阅读全文

posted @ 2014-04-28 13:20 Eason Liu 阅读(4186) 评论(1) 推荐(1)

[Leetcode] ZigZag Conversion
摘要: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 fo... 阅读全文

posted @ 2014-04-27 19:02 Eason Liu 阅读(175) 评论(0) 推荐(0)

[Leetcode] Wildcard Matching
摘要:Implement wildcard pattern matching with support for'?'and'*'.'?' Matches any single character.'*' Matches any sequence of characters (including the e... 阅读全文

posted @ 2014-04-27 16:19 Eason Liu 阅读(203) 评论(1) 推荐(0)

[Leetcode] 4Sum
摘要:Given an arraySofnintegers, are there elementsa,b,c, anddinSsuch thata+b+c+d= target? Find all unique quadruplets in the array which gives the sum of ... 阅读全文

posted @ 2014-04-27 15:13 Eason Liu 阅读(174) 评论(0) 推荐(0)

[Leetcode] Word Break II
摘要:Given a stringsand a dictionary of wordsdict, add spaces insto construct a sentence where each word is a valid dictionary word.Return all such possibl... 阅读全文

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

[Leetcode] Best Time to Buy and Sell Stock III
摘要:Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete a... 阅读全文

posted @ 2014-04-22 16:34 Eason Liu 阅读(209) 评论(0) 推荐(0)

[Leetcode] Permutation Sequence
摘要:The set[1,2,3,…,n]contains a total ofn! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie... 阅读全文

posted @ 2014-04-22 15:41 Eason Liu 阅读(158) 评论(0) 推荐(0)

[Leetcode] Surrounded Regions
摘要:Given a 2D board containing'X'and'O', capture all regions surrounded by'X'.A region is captured by flipping all'O's into'X's in that surrounded region... 阅读全文

posted @ 2014-04-22 15:28 Eason Liu 阅读(293) 评论(0) 推荐(0)

[Jobdu] 题目1522:包含min函数的栈
摘要:题目描述:定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。输入:输入可能包含多个测试样例,输入以EOF结束。对于每个测试案例,输入的第一行为一个整数n(1 2 #include 3 #include 4 using namespace std; 5 6 int n, a... 阅读全文

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

[Jobdu] 题目1348:数组中的逆序对
摘要:题目描述:在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数。输入:每个测试案例包括两行:第一行包含一个整数n,表示数组中的元素个数。其中1 v[right]时,cnt += 所有左边的left以后的元素的个数,而不是cnt ... 阅读全文

posted @ 2014-04-18 15:19 Eason Liu 阅读(358) 评论(0) 推荐(0)

[Leetcode] N-Queens II
摘要:Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.跟上一题一样,改下结果的显示方式。 1 class S... 阅读全文

posted @ 2014-04-18 00:32 Eason Liu 阅读(242) 评论(0) 推荐(0)

[Leetcode] N-Queens
摘要:Then-queens puzzle is the problem of placingnqueens on ann×nchessboard such that no two queens attack each other.Given an integern, return all distinc... 阅读全文

posted @ 2014-04-17 18:56 Eason Liu 阅读(423) 评论(0) 推荐(0)

[Leetcode] 3Sum
摘要:Given an arraySofnintegers, are there elementsa,b,cinSsuch thata+b+c= 0? Find all unique triplets in the array which gives the sum of zero.Note:Elemen... 阅读全文

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

[Leetcode] 3Sum Closest
摘要:Given an arraySofnintegers, find three integers inSsuch that the sum is closest to a given number, target. Return the sum of the three integers. You m... 阅读全文

posted @ 2014-04-14 00:13 Eason Liu 阅读(240) 评论(0) 推荐(0)

[Leetcode] Reverse Nodes in k-Group
摘要: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-o... 阅读全文

posted @ 2014-04-13 23:52 Eason Liu 阅读(234) 评论(0) 推荐(0)

[Leetcode] Sudoku Solver
摘要:Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character'.'.You may assume that there will be on... 阅读全文

posted @ 2014-04-13 22:25 Eason Liu 阅读(1430) 评论(0) 推荐(0)

[Leetcode] Valid Sudoku
摘要:Determine if a Sudoku is valid, according to:Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with ... 阅读全文

posted @ 2014-04-13 20:22 Eason Liu 阅读(901) 评论(0) 推荐(0)

[Leetcode] Regular Expression Matching
摘要:Implement regular expression matching with support for'.'and'*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input string (not partial).The function prototype should be:bool isMatch(const char 阅读全文

posted @ 2014-04-13 14:44 Eason Liu 阅读(2408) 评论(0) 推荐(0)

[Leetcode] Edit Distance
摘要:Given two wordsword1andword2, find the minimum number of steps required to convertword1toword2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:a) Insert a characterb) Delete a characterc) Replace a character自然语言处理(NLP)中,有一个基本问题就是求两个字符串的minimal Edit Dist 阅读全文

posted @ 2014-04-12 22:56 Eason Liu 阅读(3035) 评论(3) 推荐(1)

[Leetcode] Implement strStr()
摘要:Implement strStr().Returns a pointer to the first occurrence of needle in haystack, ornullif needle is not part of haystack.KMP算法! 1 class Solution { ... 阅读全文

posted @ 2014-04-12 18:19 Eason Liu 阅读(7049) 评论(7) 推荐(0)

[Leetcode] Distinct Subsequences
摘要: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.K可能会大于链表的长度,坑爹! 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListN 阅读全文

posted @ 2014-04-11 17:36 Eason Liu 阅读(166) 评论(0) 推荐(0)

[Leetcode] Restore IP Addresses
摘要:Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given"25525511135",return["255.255.11.135", "255.255.111.35"]. (Order does not matter)DFS! 1 class Solution { 2 public: 3 bool isVail(string s) { 4 if ( 阅读全文

posted @ 2014-04-11 17:23 Eason Liu 阅读(238) 评论(0) 推荐(0)

[Leetcode] Substring with Concatenation of All Words
摘要: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 @ 2014-04-11 17:02 Eason Liu 阅读(539) 评论(0) 推荐(0)

[Leetcode] Palindrome Partitioning II
摘要:Given a strings, partitionssuch that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning ofs... 阅读全文

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

[Leetcode] Palindrome Partitioning
摘要:Given a strings, partitionssuch that every substring of the partition is a palindrome.Return all possible palindrome partitioning ofs.For example, givens="aab",Return [ ["aa","b"], ["a","a","b"] ]像这样列举所有结果的只能DFS了。不过为什么我老是忘记写++low跟--high呢,都死 阅读全文

posted @ 2014-04-10 23:52 Eason Liu 阅读(186) 评论(0) 推荐(0)

[Leetcode] Maximal Rectangle
摘要:Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.给定一个矩阵中,只有0和1,求出这个矩阵的一个最大的子矩阵,其中只包含1.例如011011101001110111101111100000其实这个问题可以转化为Largest Rectangle in Histogram,先将上面的矩阵转化为:011011201003110142102532100000然后对每一行求直方图的最大面积。class S 阅读全文

posted @ 2014-04-10 21:47 Eason Liu 阅读(2638) 评论(0) 推荐(0)

[Jobdu] 题目1493:公约数
摘要:题目描述:给定两个正整数a,b(1 2 using namespace std; 3 4 int gcd(int a, int b) { 5 int m = a > b ? a : b; 6 int n = a > b ? b : a; 7 return n == 0 ? a : gcd(n, m % n); 8 } 9 10 int main() {11 int a, b;12 int max;13 int count;14 int i;15 while (cin >> a >> b) {16 count ... 阅读全文

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

[Leetcode] Merge k Sorted Lists
摘要:Mergeksorted linked lists and return it as one sorted list. Analyze and describe its complexity.no comment. 1 /** 2 * Definition for singly-linked li... 阅读全文

posted @ 2014-04-10 18:01 Eason Liu 阅读(210) 评论(0) 推荐(0)

[Leetcode] Gray Code
摘要:he gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integernrepresenting the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.For example, givenn= 2, return[0,1,3,2]. Its gray code se 阅读全文

posted @ 2014-04-10 17:32 Eason Liu 阅读(2064) 评论(0) 推荐(0)

[Leetcode] Roman to Integer
摘要:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.纯模拟,规则见:Integer to Roman。 1 class Solution { 2 public: 3 map roman; 4 void init() { 5 roman['I'] = 1; 6 roman['V'] = 5; 7 roman['X'] = 10; 8 roman['L'] = ... 阅读全文

posted @ 2014-04-10 17:18 Eason Liu 阅读(158) 评论(0) 推荐(0)

[Leetcode] Sqrt(x)
摘要:Implementint sqrt(int x).Compute and return the square root ofx.牛顿迭代法, 碉堡了。class Solution {public: int sqrt(int x) { double ans = x; while (abs(ans * ans - x) > 0.0001) { ans = (ans + x / ans) / 2; } return (int)ans; }}; 阅读全文

posted @ 2014-04-10 16:50 Eason Liu 阅读(152) 评论(0) 推荐(0)

[Leetcode] Largest Rectangle in Histogram
摘要:Givennnon-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogram where width of each bar is 1, given height =[2,1,5,6,2,3].The largest rectangle is shown in the shaded area, which has ar 阅读全文

posted @ 2014-04-10 15:52 Eason Liu 阅读(809) 评论(0) 推荐(0)

[Leetcode] Unique Binary Search Trees II
摘要:Givenn, generate all structurally uniqueBST's(binary search trees) that store values 1...n.For example,Givenn= 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ ... 阅读全文

posted @ 2014-04-10 15:15 Eason Liu 阅读(1981) 评论(0) 推荐(0)

[Leetcode] Remove Duplicates from Sorted List II
摘要:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving onlydistinctnumbers from the original list.For example,Given1->2->3-... 阅读全文

posted @ 2014-04-10 14:48 Eason Liu 阅读(155) 评论(0) 推荐(0)

[Leetcode] Container With Most Water
摘要:Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of lineiis 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.Note: You 阅读全文

posted @ 2014-04-10 14:11 Eason Liu 阅读(2292) 评论(0) 推荐(0)

[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 阅读(2248) 评论(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 阅读(1864) 评论(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)

[Leetcode] Swap Nodes in Pairs
摘要: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.链表神马的完全是我的克星啊。没有算法,好好 阅读全文

posted @ 2014-04-04 17:29 Eason Liu 阅读(150) 评论(0) 推荐(0)

[Jobdu] 题目1391:顺时针打印矩阵
摘要:题目描述:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵:1 2 3 45 6 7 89 10 11 1213 14 15 16则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.输入:输入可能包含多个测试样例,对于每个测试案例,输入的第一行包括两个整数m和n(1 2 #include 3 using namespace std; 4 5 int a[1000][1000]; 6 7 void print(int x, int y, int m, int n) 8 { 9 for (int i ... 阅读全文

posted @ 2014-04-04 16:05 Eason Liu 阅读(244) 评论(0) 推荐(0)

[Jobdu] 题目1369:字符串的排列
摘要:题目描述:输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。输入:每个测试案例包括1行。输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。输出:对应每组数据,按字典序输出所有排列。样例输入:abcBCA样例输出:abcacbbacbcacabcbaABCACBBACBCACABCBA再复习一下next_permutation,最后一组测试数据9位,用cout超时,关键时候还是得printf啊。 1 #include 2 #include 3 #in.. 阅读全文

posted @ 2014-04-04 15:58 Eason Liu 阅读(544) 评论(0) 推荐(0)

[Jobdu] 题目1283:第一个只出现一次的字符
摘要:题目描述:在一个字符串(1 2 #include 3 #include 4 using namespace std; 5 6 int main() { 7 string s; 8 int a[256]; 9 while (cin >> s) {10 memset(a, 0, sizeof(a));11 int t = 0;12 for (int i = 0; i < s.length(); ++i) {13 ++a[s[i]];14 }15 bool flag... 阅读全文

posted @ 2014-04-04 15:32 Eason Liu 阅读(256) 评论(0) 推荐(0)

[Jobdu] 题目1516:调整数组顺序使奇数位于偶数前面
摘要:题目描述:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。输入:每个输入文件包含一组测试案例。对于每个测试案例,第一行输入一个n,代表该数组中数字的个数。接下来的一行输入n个整数。代表数组中的n个数。输出:对应每个测试案例,输入一行n个数字,代表调整后的数组。注意,数字和数字之间用一个空格隔开,最后一个数字后面没有空格。样例输入:51 2 3 4 5样例输出:1 3 5 2 4对于普通的交换顺序,只要设两个指针分别从头跟尾扫描,当分别遇到不符合条件的位置时停止,然后交换位 阅读全文

posted @ 2014-04-04 15:09 Eason Liu 阅读(2984) 评论(4) 推荐(0)

[Leetcode] Convert Sorted List to Binary Search Tree
摘要:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.跟上一题一样,只是数据结构换成了链表,这里求链表的中位数还是乖乖用len先计算长度吧,用slow、fast指针还得先标记尾指针,反而更麻烦。 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ... 阅读全文

posted @ 2014-04-04 01:22 Eason Liu 阅读(176) 评论(0) 推荐(0)

[Leetcode] Convert Sorted Array to Binary Search Tree
摘要:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.哎,又是被指针坑了,指针作为函数的参数传递时,在函数体内可以修改指针所指的内容,但是不有修改指针本身,所以一时是运行时错误,这里要用指针的引用TreeNode *&,问题得解。算法没什么好说的,递归求解,每次取传入列表的中位数为节点的值。 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 *... 阅读全文

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

[Leetcode] Recover Binary Search Tree
摘要:Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?中序遍历,对于BST,其中序序列是有序的,所以只要找出中序序列中的逆序对,但是要注意保存中间节点,可以使用全局变量,也可以使用引用。 1 /** 2 * Defi. 阅读全文

posted @ 2014-04-04 00:49 Eason Liu 阅读(265) 评论(0) 推荐(0)

[Leetcode] Jump Game II
摘要:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximu... 阅读全文

posted @ 2014-04-03 19:05 Eason Liu 阅读(215) 评论(0) 推荐(0)

[Leetcode] Anagrams
摘要:Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.这题先得知道啥叫Anagrams,知道后其实很简单。首先简单介绍一下Anagram(回文构词法)。Anagrams是指由颠倒字母顺序组成的单词,比如“dormitory”颠倒字母顺序会变成“dirty room”,“tea”会变成“eat”。回文构词法有一个特点:单词里的字母的种类和数目没有改变,只是改变了字母的排列顺序。For example:Input: [" 阅读全文

posted @ 2014-04-03 18:01 Eason Liu 阅读(7088) 评论(0) 推荐(0)

[面试题] Find next higher number with same digits
摘要:Find next higher number with same digits.Example 1 : if num = 25468, o/p = 25486Example 2 : if num = 21765, o/p = 25167Example 3 : If num = 54321, o/p = 54321 (cause it's not possible to gen a higher num than tiz with given digits ).Google的面试题,实际上就是next_permutation。 阅读全文

posted @ 2014-04-03 17:36 Eason Liu 阅读(222) 评论(0) 推荐(0)

[Jobdu] 题目1214:丑数
摘要:题目描述:把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。输入:输入包括一个整数N(1 2 #include 3 using namespace std; 4 5 int a[1501]; 6 7 int getMin(int a, int b, int c) 8 { 9 int tmp = a < b ? a : b;10 return c < tmp ? c : tmp;11 }12 13 void init()14 {15 ... 阅读全文

posted @ 2014-04-03 17:20 Eason Liu 阅读(614) 评论(1) 推荐(0)

[面试题] 两个单链表里交叉的第一个元素
摘要:找出两个单链表里交叉的第一个元素解法:分别遍历list1、list2,计算得到L1,L2;比较最后结点是否相等,相等则表明是两个链表是交叉的如果交叉:长的链表先移动|L1-L2|步,再逐个比较,等到第一个交点! 阅读全文

posted @ 2014-04-03 16:11 Eason Liu 阅读(322) 评论(0) 推荐(0)

[Leetcode] Minimum Window Substring
摘要:Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S="ADOBECODEBANC"T="ABC"Minimum window is"BANC".Note:If there is no such window in S that covers all characters in T, return the emtpy 阅读全文

posted @ 2014-04-03 15:30 Eason Liu 阅读(194) 评论(0) 推荐(0)

[Leetcode] Binary Tree Maximum Path Sum
摘要:Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree, 1 / \ 2 3Return6.后序遍历! 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode... 阅读全文

posted @ 2014-04-03 14:27 Eason Liu 阅读(157) 评论(0) 推荐(0)

[Leetcode] Reorder List
摘要: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}.OH! MY GOD! I HATE LINKED LIST!看似简单,实现起来总会遇到各种指针错误,写程序之前最好先在纸上好好画画,把各种指针关系搞搞清楚。本题的想法就是先将列表平分成两份,后一份逆序,然 阅读全文

posted @ 2014-04-03 13:31 Eason Liu 阅读(804) 评论(0) 推荐(0)

[Leetcode] Search a 2D Matrix
摘要:Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:Integers in each row are sorted from l... 阅读全文

posted @ 2014-04-02 22:55 Eason Liu 阅读(205) 评论(0) 推荐(0)

[Leetcode] Combination Sum
摘要:Given a set of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Thesamerepeated number may be chosen fromCunlimited number of times.Note:All numbers (including target) will be positive integers.Elements in a combination (a1,a2, … ,ak 阅读全文

posted @ 2014-04-02 21:29 Eason Liu 阅读(142) 评论(0) 推荐(0)

[Leetcode] Median of Two Sorted Arrays
摘要:There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).好难啊,总是会有各种边界问题。想法如下:最后从medianof two sorted arrays中看到了一种非常好的方法。原文用英文进行解释,在此我们将其翻译成汉语。该方法的核心是将原问题转变成一个寻找第k小数的问题(假设两个原序列升序排列),这样中位数实际上是第(m+n 阅读全文

posted @ 2014-04-02 18:48 Eason Liu 阅读(286) 评论(0) 推荐(0)

[Jobdu] 题目1463:招聘会
摘要:题目描述:又到毕业季,很多大公司来学校招聘,招聘会分散在不同时间段,小明想知道自己最多能完整的参加多少个招聘会(参加一个招聘会的时候不能中断或离开)。输入:第一行n,有n个招聘会,接下来n行每行两个整数表示起止时间,由从招聘会第一天0点开始的小时数表示。n 2 #include 3 #include 4 #include 5 using namespace std; 6 7 struct data { 8 int st; 9 int ed;10 };11 12 bool cmp(const data &a, const data &b) {13 return ... 阅读全文

posted @ 2014-04-02 01:27 Eason Liu 阅读(272) 评论(0) 推荐(0)

[Leetcode] Merge Two Sorted Lists
摘要: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(... 阅读全文

posted @ 2014-04-02 00:18 Eason Liu 阅读(173) 评论(0) 推荐(0)

[Leetcode] Combinations
摘要:Given two integersnandk, return all possible combinations ofknumbers out of 1 ...n.For example,Ifn= 4 andk= 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]DFS! 1 class Solution { 2 public: 3 void getNextComb(vector > &res, vector v, int n, int k, int idx) { 4 if (idx ... 阅读全文

posted @ 2014-04-01 23:53 Eason Liu 阅读(297) 评论(0) 推荐(0)

[Leetcode] Populating Next Right Pointers in Each Node II
摘要:Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution stil... 阅读全文

posted @ 2014-04-01 23:32 Eason Liu 阅读(170) 评论(0) 推荐(0)

[Leetcode] Insertion Sort List
摘要:Sort a linked list using insertion sort.虽然算法很简单,但是链表操作起来实正是烦啊,特别要注意各种边界条件。 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 public:11 ListNode *inser... 阅读全文

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

[Jobdu] 题目1499:项目安排
摘要:题目描述:小明每天都在开源社区上做项目,假设每天他都有很多项目可以选,其中每个项目都有一个开始时间和截止时间,假设做完每个项目后,拿到报酬都是不同的。由于小明马上就要硕士毕业了,面临着买房、买车、给女友买各种包包的鸭梨,但是他的钱包却空空如也,他需要足够的money来充实钱包。万能的网友麻烦你来帮帮小明,如何在最短时间内安排自己手中的项目才能保证赚钱最多(注意:做项目的时候,项目不能并行,即两个项目之间不能有时间重叠,但是一个项目刚结束,就可以立即做另一个项目,即项目起止时间点可以重叠)。输入:输入可能包含多个测试样例。对于每个测试案例,输入的第一行是一个整数n(1 2 #include 3. 阅读全文

posted @ 2014-04-01 22:13 Eason Liu 阅读(414) 评论(0) 推荐(0)

[Jobdu] 题目1529:棋盘寻宝
摘要:题目描述:现在有一个8*8的棋盘,上面放着64个价值不等的礼物,每个小的棋盘上面放置一个礼物(礼物的价值大于0小于1000),一个人的初始位置在棋盘的左上角,每次他只能向下或向右移动一步,并拿走对应棋盘上的礼物,结束位置在棋盘的右下角,请设计一个算法使其能够获得最大价值的礼物。输入:输入包含多个测试用例,每个测试用例共有8行8列,第i行的第j列的数字代表了该处棋盘上的礼物的价值,每两个数之间用空格隔开。输出:对于每组测试用例,请输出你能够获得最大价值的礼物。样例输入:2 8 15 1 10 5 19 193 5 6 6 2 8 2 1216 3 8 17 12 5 3 1413 3 2 17 阅读全文

posted @ 2014-04-01 21:15 Eason Liu 阅读(430) 评论(0) 推荐(0)

[Jobdu] 题目1530:最长不重复子串
摘要:题目描述:最长不重复子串就是从一个字符串中找到一个连续子串,该子串中任何两个字符都不能相同,且该子串的长度是最大的。输入:输入包含多个测试用例,每组测试用例输入一行由小写英文字符a,b,c...x,y,z组成的字符串,字符串的长度不大于10000。输出:对于每组测试用例,输出最大长度的不重复子串长度... 阅读全文

posted @ 2014-04-01 20:58 Eason Liu 阅读(291) 评论(0) 推荐(0)

[Jobdu] 题目1528:最长回文子串
摘要:题目描述:回文串就是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。回文子串,顾名思义,即字符串中满足回文性质的子串。给出一个只由小写英文字符a,b,c...x,y,z组成的字符串,请输出其中最长的回文子串的长度。输入:输入包含多个测试用例,每组测试用例输入一行由小写英文字符a,b,c...x,y,z组成的字符串,字符串的长度不大于200000。输出:对于每组测试用例,输出一个整数,表示该组测试用例的字符串中所包含的的最长回文子串的长度。样例输入:ababbbbbabba样例输出:344腾讯面试题,max_len初始为1,设为0会出错。 1 #include . 阅读全文

posted @ 2014-04-01 20:09 Eason Liu 阅读(309) 评论(0) 推荐(0)

[Jobdu] 题目1510:替换空格
摘要:题目描述:请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。输入:每个输入文件仅包含一组测试样例。对于每组测试案例,输入一行代表要处理的字符串。输出:对应每个测试案例,出经过处理后的字符串。样例输入:We Are Happy样例输出:We%20Are%20Happy从后向前处理数组。 1 #include 2 #include 3 using namespace std; 4 5 int main() { 6 int count; 7 int size; 8 s... 阅读全文

posted @ 2014-04-01 19:55 Eason Liu 阅读(268) 评论(0) 推荐(0)

[Leetcode] Candy
摘要:There areNchildren standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least one candy.Children with a higher rating get more candies than their neighbors.What is the minimum candies you mu 阅读全文

posted @ 2014-04-01 18:23 Eason Liu 阅读(397) 评论(0) 推荐(0)

[Leetcode] Jump Game
摘要:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you are able to reach the last index.For example:A =[2,3,1,1,4], returntrue.A =[3,2,1,0,4], returnfalse.记录 阅读全文

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

[Leetcode] Longest Valid Parentheses
摘要:Given a string containing just the characters'('and')', find the length of the longest valid (well-formed) parentheses substring.For"(()", the longest valid parentheses substring is"()", which has length = 2.Another example is")()())", where the longest 阅读全文

posted @ 2014-04-01 01:06 Eason Liu 阅读(3954) 评论(1) 推荐(1)

[Leetcode] Triangle
摘要:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4], [6,5,7], [4,1,8,3]]The minimum path sum from top to bottom is11(i.e.,2+3+5+1= 11).在输入的数组中直接修改,这样就不需要额外的空间了,不知道这... 阅读全文

posted @ 2014-04-01 00:04 Eason Liu 阅读(249) 评论(0) 推荐(0)