
interview
文章平均质量分 76
KiraYin--
这个作者很懒,什么都没留下…
展开
-
[LeetCode]Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space.原创 2013-06-26 03:45:27 · 3039 阅读 · 1 评论 -
[LeetCode] Recover Binary Search Tree
wo 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原创 2013-09-30 11:32:52 · 596 阅读 · 0 评论 -
[LeetCode] Trapping rain water
Given n non-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.For example, Given [0,1,0,2,1,0,1,3,2,1,2,1]原创 2013-09-16 23:53:56 · 926 阅读 · 0 评论 -
[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 consecutive elements sequence is [1, 2, 3原创 2013-09-17 03:19:56 · 728 阅读 · 0 评论 -
[LeetCode] Single Number
Given an array of integers, every element appears twice except for one. Find that single one.Could you implement it without using extra memory?Solution: 利用异或的可交换性!!!!!!!!!!!!!!clas原创 2013-10-02 22:28:21 · 1545 阅读 · 0 评论 -
[LeetCode] Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to原创 2013-10-03 01:28:32 · 3970 阅读 · 0 评论 -
[LeetCode] Single Number II
Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without usi原创 2013-10-04 10:33:20 · 2000 阅读 · 0 评论 -
[LeetCode] Scramble String
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation of s1 = "great": great / \ gr原创 2013-10-05 01:08:57 · 821 阅读 · 0 评论 -
[LeetCode] Largest Rectangle in Histogram
Given n non-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 o原创 2013-09-20 10:20:06 · 776 阅读 · 0 评论 -
[LeetCode] Interleave String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example,Given:s1 = "aabcc",s2 = "dbbca",When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc",原创 2013-03-05 06:56:09 · 2077 阅读 · 0 评论 -
[LeetCode] Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read原创 2013-08-28 13:04:50 · 1070 阅读 · 0 评论 -
[leetcode] Same Tree
1. In-order Traverse and print tree. Compare the printed strings.class Solution {public: bool isSameTree(TreeNode *p, TreeNode *q) { // Start typing your C/C++ solution below //原创 2013-03-04 11:25:23 · 373 阅读 · 0 评论 -
[LeetCode] Convert linked list to BST
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; *//** * Definition for binary tree * stru原创 2013-08-22 12:59:56 · 642 阅读 · 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 "()",原创 2013-08-11 00:05:49 · 2393 阅读 · 0 评论 -
[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], [原创 2013-08-24 03:06:21 · 1028 阅读 · 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"原创 2013-08-26 00:20:19 · 4314 阅读 · 0 评论 -
[leetCode] Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integer原创 2013-08-25 23:13:16 · 805 阅读 · 0 评论 -
[LeetCode] Plus one
class Solution {public: vector plusOne(vector &digits) { // Start typing your C/C++ solution below // DO NOT write int main() function int size=digits.size(); vect原创 2013-08-26 12:16:53 · 843 阅读 · 0 评论 -
[leetCode] Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.class Solution {public: TreeNode *buildTree(vector原创 2013-08-27 07:59:52 · 838 阅读 · 0 评论 -
[LeetCode] Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the combina原创 2013-09-22 23:52:06 · 942 阅读 · 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 .For example,X X X XX O O原创 2013-10-09 12:36:33 · 776 阅读 · 0 评论 -
[LeetCode] CLone Graph
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.OJ's undirected graph serialization:Nodes are labeled from 0 to N - 1, where N is the total nod原创 2013-09-26 12:06:24 · 2060 阅读 · 0 评论 -
[LeetCode] Roman to Integer and Integer to Roman
羅馬數字共有7個,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)。按照下述的規則可以表示任意正整數。需要注意的是罗马数字中没有“0”,與進位制無關。一般認為羅馬數字只用來記數,而不作演算。重複數次:一個羅馬數字重複幾次,就表示這個數的幾倍。右加左減:在較大的羅馬數字的右邊記上較小的羅馬數字,表示大數字加小數字。在較大的羅馬數字的左邊原创 2013-10-27 04:19:22 · 5789 阅读 · 2 评论 -
[LeetCode] Merge K sorted lists
Solution 1:1. use merge2list(), and merge all lists one by one. Time complexity is O(n*longest list length)2. For each node, find the smallest node among all head nodes in O(k) time, Time complexi原创 2013-10-27 12:09:01 · 848 阅读 · 0 评论 -
[LeetCode] Convert Sorted List to Binary Search Tree
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; *//** * Definition for binary tree * stru原创 2013-10-23 09:38:47 · 562 阅读 · 0 评论 -
[LeetCode] Remove Duplicate from Sorted Linkded List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1-原创 2013-10-30 09:31:31 · 790 阅读 · 0 评论 -
[LeetCode] Binary Tree Zigzag level order traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary原创 2013-10-23 12:13:42 · 559 阅读 · 0 评论 -
[LeetCode]Implement Trie (Prefix Tree)
class TrieNode {public: // Initialize your data structure here. TrieNode() { val='0'; end_of_a_word=0; for(int i=0;i<26;i++) {原创 2015-06-16 13:58:04 · 955 阅读 · 0 评论 -
[LeetCode] HouseRobber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent house原创 2015-07-07 11:29:03 · 830 阅读 · 0 评论 -
[LeetCode] Word Ladder
1. Recursively: time limits exceedclass Solution {public: void ladder(string start, string end, unordered_set dict, int &minPath,int paths) { if(dict.empty()) return; if原创 2013-10-11 10:33:35 · 799 阅读 · 0 评论 -
[LeetCode] 2Sum, 3Sum, 4Sum, 3SUm closet
1. 2Sum: hash Table O(n*n)2. 3Sum: 2Sum + 1 : O(n*n)for loop + 左右夹逼法:O(N*N)class Solution {public: vector > threeSum(vector &num) { // Note: The Solution object is instantiated原创 2013-10-27 09:31:52 · 1892 阅读 · 0 评论 -
[LeetCode] Regular Expression matching
'.' 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(c原创 2013-10-27 00:37:49 · 1057 阅读 · 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)).参考:http://fisherlei.blogspot.co原创 2013-09-25 23:23:18 · 971 阅读 · 0 评论 -
[LeetCode] Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most two transactions.Note:You原创 2013-03-14 08:35:09 · 2811 阅读 · 0 评论 -
[LeetCode] Palindrom Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning of s.For example, given s = "aab",Return原创 2013-10-18 04:35:25 · 659 阅读 · 0 评论 -
[LeetCode] Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed from the original string by deleting some (can be non原创 2013-10-24 11:03:05 · 646 阅读 · 0 评论 -
[LeetCode] Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of原创 2013-10-24 11:28:07 · 697 阅读 · 0 评论 -
[LeetCode] Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node.原创 2013-10-19 00:58:18 · 715 阅读 · 0 评论 -
[LeetCode] Populating Next RIght Pointer 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 still work?Note:You may only use constant extr原创 2013-10-19 04:35:14 · 624 阅读 · 0 评论 -
string to integer (atoi)
Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input ca原创 2013-10-25 11:18:32 · 1179 阅读 · 0 评论