07 2013 档案

摘要:Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array. Here are few examples. [1,3,5,6], 5 → 2 [1,3,5,6], 2 → 1 [1,3,5,6], 7 → 4 [1,3,5,6], 0 → 0class Solutio 阅读全文
posted @ 2013-07-17 08:40 一只会思考的猪 阅读(103) 评论(0) 推荐(0)
摘要: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 off as "one 2, then one 1" or 1211. Given an integer n, generate the nth sequence. Note 阅读全文
posted @ 2013-07-17 08:25 一只会思考的猪 阅读(183) 评论(0) 推荐(0)
摘要:Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space.题意是是说:在正整数的范围里面求第一个缺失的数A[0] = 1 A[1] = 2....A[i] = i+1; 正确的存储.此题目没有好好解答出来。再思考。class Solution {public: 阅读全文
posted @ 2013-07-16 23:23 一只会思考的猪 阅读(176) 评论(0) 推荐(0)
摘要:Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]You should return [1,2,3,6,9,8,7,4,5].//DFS 版本//之后需要设计一个非递归的版本//甚至给定(i,j)推断给定位置的valueclass Solution {public: voi... 阅读全文
posted @ 2013-07-16 23:01 一只会思考的猪 阅读(175) 评论(0) 推荐(0)
摘要: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 @ 2013-07-14 19:44 一只会思考的猪 阅读(247) 评论(0) 推荐(0)
摘要:Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Given n will always be valid. Try to do this in one 阅读全文
posted @ 2013-07-14 18:20 一只会思考的猪 阅读(140) 评论(0) 推荐(0)
摘要:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.class Solution {public: struct Node{ ListNode *p; Node(ListNode* pointer):p(pointer){ } //make heap的时候,k和2k 2k+1的孩子比较,这样就会采用 rs.p->val bool operator val > rs.p-... 阅读全文
posted @ 2013-07-14 18:07 一只会思考的猪 阅读(190) 评论(0) 推荐(0)
摘要:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]&qu 阅读全文
posted @ 2013-07-14 17:51 一只会思考的猪 阅读(191) 评论(0) 推荐(0)
摘要: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. You may not modify the values in the list, only nodes itself can be changed.class Solution 阅读全文
posted @ 2013-07-14 16:59 一只会思考的猪 阅读(162) 评论(0) 推荐(0)
摘要:Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new length.class Solution {public: //这个版本的快排要记住 //这个版本的Partition是 Hoare的版本 int removeElement(int A[], int n, in... 阅读全文
posted @ 2013-07-14 12:11 一只会思考的猪 阅读(232) 评论(0) 推荐(0)
摘要:Divide two integers without using multiplication, division and mod operator.class Solution {public: int divide(int dividend, int divisor) { // Start typing your C/C++ solution below // DO NOT write int main() function assert(divisor); int ans = 0, step = 1, negative = ... 阅读全文
posted @ 2013-07-14 11:28 一只会思考的猪 阅读(213) 评论(0) 推荐(0)
摘要:Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative.class Solution {public: string add(string num1, string num2){ int i = 0, j = 0, c = 0,m = 0; string ans; while(i < num1.s... 阅读全文
posted @ 2013-07-14 00:07 一只会思考的猪 阅读(210) 评论(0) 推荐(0)
摘要: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.Your goal is to reach the last index in the minimum number of jumps. For example: Given array A = [2,3,1,1,4] The minim 阅读全文
posted @ 2013-07-13 17:34 一只会思考的猪 阅读(213) 评论(0) 推荐(0)
摘要:Implement pow(x, n).//目前写的不太好,坑比较多(n 0? x : 1.0/x; } double a = pow(x,n/2); return ans*a*a; }}; 阅读全文
posted @ 2013-07-13 16:33 一只会思考的猪 阅读(160) 评论(0) 推荐(0)
摘要:Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6.class Solution {public: int maxSubArray(int A[], int n) { // Start typin... 阅读全文
posted @ 2013-07-13 14:44 一只会思考的猪 阅读(99) 评论(0) 推荐(0)
摘要: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], return true.A = [3,2,1,0,4], return fals 阅读全文
posted @ 2013-07-13 14:35 一只会思考的猪 阅读(158) 评论(0) 推荐(0)
摘要:Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. bool compare(const Interval & ls, const Interval & rs){ return ls.start merge(vector &intervals) { // Start typing your C/C++ solution below... 阅读全文
posted @ 2013-07-13 13:34 一只会思考的猪 阅读(171) 评论(0) 推荐(0)
摘要:Given a list, rotate the list to the right by k places, where k is non-negative.For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL.注意rotate回头的情况:{1,2,3}, 5wrong:{1,2,3}right:{2,3,1}突然想到,在白板上写程序,因为space limited,最好不要用无谓的{,},而且尽量在同一行使得代码在保证可读性的情况下 阅读全文
posted @ 2013-07-13 11:05 一只会思考的猪 阅读(199) 评论(0) 推荐(0)
摘要:Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100".class Solution {public: string addBinary(string a, string b) { // Start typing your C/C++ solution below // DO NOT write int main() function string ans; revers. 阅读全文
posted @ 2013-07-13 08:54 一只会思考的猪 阅读(170) 评论(0) 推荐(0)
摘要:Given a collection of integers that might contain duplicates, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets. For example, If S = [1,2,2], a solution is:[ [2], [1], [1,2,2], [2,2], [1,2], []]class Sol... 阅读全文
posted @ 2013-07-13 08:22 一只会思考的猪 阅读(166) 评论(0) 推荐(0)
摘要:Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1].class Solution {public: vector > ans; void dfs(vector &num,vector &path){ if (path.size() == n.. 阅读全文
posted @ 2013-07-12 08:42 一只会思考的猪 阅读(151) 评论(0) 推荐(0)
摘要:Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].class Solution {public: vector > ans; void dfs(vector &num,vector &path){ if (path.size() == num.size()){ ... 阅读全文
posted @ 2013-07-12 08:23 一只会思考的猪 阅读(158) 评论(0) 推荐(0)
摘要:Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectively in the grid.For example,There is one obstacle in the middle of a 3x3 grid as illustrated below.[ [0,0,0], [ 阅读全文
posted @ 2013-07-12 07:34 一只会思考的猪 阅读(236) 评论(0) 推荐(0)
摘要:A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).How many possible 阅读全文
posted @ 2013-07-12 07:24 一只会思考的猪 阅读(136) 评论(0) 推荐(0)
摘要:Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at any point in time.问题:大数据的时候TLE,问题搜索的时候,走了很多重复的子路径所以不能进行DFS(DFS本质上是暴力),而应该DPDP[i,j] = 代表从A[0,0]到A[i,j]的p 阅读全文
posted @ 2013-07-12 07:09 一只会思考的猪 阅读(163) 评论(0) 推荐(0)
摘要: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.class Solution {public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { // Start typing your C/C++ solution below // DO NOT write int ma... 阅读全文
posted @ 2013-07-12 06:26 一只会思考的猪 阅读(111) 评论(0) 推荐(0)
摘要:Given a number represented as an array of digits, plus one to the number.class Solution {public: vector plusOne(vector &digits) { // Start typing your C/C++ solution below // DO NOT write int main() function vector ans; int c = 0, k = 0; for(int i = digits.size(... 阅读全文
posted @ 2013-07-10 22:54 一只会思考的猪 阅读(209) 评论(0) 推荐(0)
摘要:Implement int sqrt(int x).Compute and return the square root of x.思路:Sqrt(double)需要进行逼近。目前这种方法掌握不好class Solution { public: int sqrt(int x) { // Start typing your C/C++ solution below // DO NOT write int main() function // Start typing your Java solution below... 阅读全文
posted @ 2013-07-10 22:37 一只会思考的猪 阅读(194) 评论(0) 推荐(0)
摘要:You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?class Solution {public: int climbStairs(int n) { // Start typing your C/C++ solution below // DO NOT write int main() f... 阅读全文
posted @ 2013-07-10 22:09 一只会思考的猪 阅读(141) 评论(0) 推荐(0)
摘要:Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word: a) Insert a character b) Delete a character c) Replace a character思路:普通的DP很好写,问题是路径压缩的DP压缩如果进行,空间如何重复利用 阅读全文
posted @ 2013-07-10 21:41 一只会思考的猪 阅读(162) 评论(0) 推荐(0)
摘要:Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.思路:完全的in-place,利用了第一次出现0位置的第i行和第j列存储将来可能出现0的行的下标和列的下标class Solution {public: void setZeroes(vector > &matrix) { // Start typing your C/C++ solution below // DO NOT write int main() function ... 阅读全文
posted @ 2013-07-10 21:18 一只会思考的猪 阅读(197) 评论(0) 推荐(0)
摘要:Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each row is greater than the last integer of the previous row.For example,Consider the following matrix:[ [1, .. 阅读全文
posted @ 2013-07-10 04:36 一只会思考的猪 阅读(137) 评论(0) 推荐(0)
摘要: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 integers 0, 1, and 2 to represent the color red, white, and blue respectively. Note: You are not suppose to use t 阅读全文
posted @ 2013-07-10 04:28 一只会思考的猪 阅读(231) 评论(0) 推荐(0)
摘要: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],]class Solution {public: void dfs(vector> &ans, vector path,int n, int k, int ok_n){ if (ok_n == k){ ans.... 阅读全文
posted @ 2013-07-10 03:59 一只会思考的猪 阅读(183) 评论(0) 推荐(0)
摘要:Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.class Solution {public: void merge(int A[], int m, int B[], int n) {... 阅读全文
posted @ 2013-07-09 21:58 一只会思考的猪 阅读(144) 评论(0) 推荐(0)
摘要:Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ? m ? n ? length of list.class Solution {public: void rev 阅读全文
posted @ 2013-07-09 21:47 一只会思考的猪 阅读(170) 评论(0) 推荐(0)
摘要:Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3].class Solution {public: int removeDuplicates(int A[], int n) { // Start typing your C/C++ sol 阅读全文
posted @ 2013-07-09 21:07 一只会思考的猪 阅读(120) 评论(0) 推荐(0)
摘要: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->1->2->3, return 2->3.class Solution {public: ListNode *deleteDuplica 阅读全文
posted @ 2013-07-09 20:24 一只会思考的猪 阅读(159) 评论(0) 推荐(0)
摘要:Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3.class Solution {public: ListNode *deleteDuplicates(ListNode *head) { // Start typing your C/C++ solution 阅读全文
posted @ 2013-07-08 08:28 一只会思考的猪 阅读(121) 评论(0) 推荐(0)
摘要:Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node's key.The right subtree of a node contains only nodes with keys greater than the node's key.Both the left an 阅读全文
posted @ 2013-07-02 01:00 一只会思考的猪 阅读(143) 评论(0) 推荐(0)
摘要:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the following is not: 1 / \ 2 2 \ \ 3 3 Note: Bonus points if you could solve it both recursively and itera... 阅读全文
posted @ 2013-07-02 00:54 一只会思考的猪 阅读(145) 评论(0) 推荐(0)
摘要: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 tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its zigzag level order traversal as:[ [3], [20,9], [ 阅读全文
posted @ 2013-07-02 00:38 一只会思考的猪 阅读(150) 评论(0) 推荐(0)
摘要: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 tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its zigzag level order traversal as:[ [3], [20,9], [ 阅读全文
posted @ 2013-07-02 00:35 一只会思考的猪 阅读(149) 评论(0) 推荐(0)
摘要:Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.class Solution {public: int maxDepth(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() f... 阅读全文
posted @ 2013-07-02 00:29 一只会思考的猪 阅读(123) 评论(0) 推荐(0)
摘要: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: void f(vector &preorder,int s1, int e1,vector & inorder,int s2, int e2, TreeNode *& root){ if (s1 > e1){ return; ... 阅读全文
posted @ 2013-07-02 00:25 一只会思考的猪 阅读(153) 评论(0) 推荐(0)
摘要:Given inorder and postorder traversal of a tree, construct the binary tree.Note: You may assume that duplicates do not exist in the tree.class Solution {public: void f(vector &inorder,int s1, int e1,vector & postorder,int s2, int e2, TreeNode *& root){ if (s1 > e1){ return; ... 阅读全文
posted @ 2013-07-02 00:18 一只会思考的猪 阅读(159) 评论(0) 推荐(0)
摘要:Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its bottom-up level order traversal as:[ [15,7] [9,20], [3],]class Solution 阅读全文
posted @ 2013-07-01 23:55 一只会思考的猪 阅读(177) 评论(0) 推荐(0)
摘要:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.class Solution {public: void f(vector & v, int start, int end, TreeNode *&root){ if (start > end){ return; } int m = start + (end - start)/2; if (!root){ ... 阅读全文
posted @ 2013-07-01 23:24 一只会思考的猪 阅读(113) 评论(0) 推荐(0)
摘要:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.class Solution {public: void f(vector & v, int start, int end, TreeNode *&root){ if (start > end){ return; } int m = start + (end - start)/2; if (!ro... 阅读全文
posted @ 2013-07-01 23:19 一只会思考的猪 阅读(144) 评论(0) 推荐(0)
摘要:Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.class Solution {public: bool f(TreeNode *root, int & height){ if (!root){ ... 阅读全文
posted @ 2013-07-01 23:01 一只会思考的猪 阅读(136) 评论(0) 推荐(0)