04 2014 档案

摘要:Generate ParenthesesGivennpairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, givenn= 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"public class Solution { private ArrayLi 阅读全文
posted @ 2014-04-02 20:37 boole 阅读(120) 评论(0) 推荐(0)
摘要:Plus OneGiven a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.public class Solution { public int[] plusOne(int[] digits) { int carry = 1; int temp; ... 阅读全文
posted @ 2014-04-02 20:14 boole 阅读(202) 评论(0) 推荐(0)
摘要:Container With Most WaterGivennnon-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 阅读全文
posted @ 2014-04-02 19:55 boole 阅读(112) 评论(0) 推荐(0)
摘要:Maximum SubarrayFind 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.public class Solution { public int maxSubArray(int[] A) { ... 阅读全文
posted @ 2014-04-01 22:14 boole 阅读(96) 评论(0) 推荐(0)
摘要:Remove Duplicates from Sorted ListGiven a sorted linked list, delete all duplicates such that each element appear onlyonce.For example,Given1->1->2, return1->2.Given1->1->2->3->3, return1->2->3./** * Definition for singly-linked list. * public class ListNode { * int val; * 阅读全文
posted @ 2014-04-01 22:01 boole 阅读(118) 评论(0) 推荐(0)
摘要:Search Insert PositionGiven 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 阅读全文
posted @ 2014-04-01 22:00 boole 阅读(129) 评论(0) 推荐(0)
摘要:Populating Next Right Pointers in Each NodeGiven a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set toNULL.In... 阅读全文
posted @ 2014-04-01 21:59 boole 阅读(131) 评论(0) 推荐(0)
摘要:Binary Tree Preorder TraversalGiven a binary tree, return thepreordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,2,3]./** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * ... 阅读全文
posted @ 2014-04-01 21:58 boole 阅读(117) 评论(0) 推荐(0)
摘要:Binary Tree Inorder TraversalGiven a binary tree, return theinordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,3,2]./** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * Tr... 阅读全文
posted @ 2014-04-01 21:54 boole 阅读(79) 评论(0) 推荐(0)
摘要:Linked List CycleGiven a linked list, determine if it has a cycle in it./** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public boolean hasCycle... 阅读全文
posted @ 2014-04-01 21:50 boole 阅读(95) 评论(0) 推荐(0)
摘要:Unique Binary Search TreesGivenn, how many structurally uniqueBST's(binary search trees) that store values 1...n?For example,Givenn= 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ ... 阅读全文
posted @ 2014-04-01 21:43 boole 阅读(125) 评论(0) 推荐(0)
摘要:Best Time to Buy and Sell Stock IISay 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 as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not e 阅读全文
posted @ 2014-04-01 21:41 boole 阅读(111) 评论(0) 推荐(0)
摘要:Reverse IntegerReverse digits of an integer.Example1:x = 123, return 321Example2:x = -123, return -321public class Solution { public int reverse(int x) { int positive = 1; int result = 0; if(x 0) { result = result*10 + x%10; x ... 阅读全文
posted @ 2014-04-01 21:40 boole 阅读(136) 评论(0) 推荐(0)
摘要:Same TreeGiven two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value./** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode... 阅读全文
posted @ 2014-04-01 21:38 boole 阅读(113) 评论(0) 推荐(0)
摘要:Maximum Depth of Binary TreeGiven 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./** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode righ... 阅读全文
posted @ 2014-04-01 21:36 boole 阅读(77) 评论(0) 推荐(0)
摘要:Single NumberGiven an array of integers, every element appearstwiceexcept for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?public class Solution { public int singleNumber(int[] A) { int a = 0;... 阅读全文
posted @ 2014-04-01 21:35 boole 阅读(129) 评论(0) 推荐(0)