随笔分类 -  Algorithm

上一页 1 ··· 10 11 12 13 14

[Leetcode] Permutations
摘要: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].与Next Permutation一样,n个元素的排列共有n!个,所以只要执行n!次next_permutation就行。 1 class Solution { 2 public: 3 bool nextPermutation(vector &num) { 阅读全文

posted @ 2014-03-29 14:16 Eason Liu 阅读(557) 评论(0) 推荐(0)

[Leetcode] Next Permutation
摘要:Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possibl... 阅读全文

posted @ 2014-03-29 13:34 Eason Liu 阅读(3923) 评论(0) 推荐(3)

[Leetcode] String to Integer (atoi)
摘要:Implementatoito 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 cases.Notes:It is intended for this problem to be specified vaguely (ie, no given input specs). You are respo 阅读全文

posted @ 2014-03-28 22:06 Eason Liu 阅读(162) 评论(0) 推荐(0)

[Leetcode] Sum Root to Leaf Numbers
摘要:Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.An example is the root-to-leaf path1->2->3which represents the number123.Find the total sum of all root-to-leaf numbers.For example, 1 / \ 2 3The root-to-leaf path1->2represents the number12.T 阅读全文

posted @ 2014-03-28 19:22 Eason Liu 阅读(182) 评论(0) 推荐(0)

[Leetcode] Interleaving String
摘要:Givens1,s2,s3, find whethers3is formed by the interleaving ofs1ands2.For example,Given:s1="aabcc",s2="dbbca",Whens3="aadbbcbcac", return true.Whens3="aadbbbaccc", return false.动态规划,类似于求最长公共子序列。 1 class Solution { 2 public: 3 bool isInterleave(string s1, string 阅读全文

posted @ 2014-03-28 16:30 Eason Liu 阅读(815) 评论(1) 推荐(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.没什么好说的,了解原理即可,使用递归。 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNo... 阅读全文

posted @ 2014-03-28 14:43 Eason Liu 阅读(238) 评论(0) 推荐(0)

[Leetcode] Two Sum
摘要:Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two nu... 阅读全文

posted @ 2014-03-28 14:38 Eason Liu 阅读(208) 评论(0) 推荐(0)

[Leetcode] Climbing Stairs
摘要:You are climbing a stair case. It takesnsteps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb... 阅读全文

posted @ 2014-03-28 14:34 Eason Liu 阅读(195) 评论(0) 推荐(0)

[Leetcode] Reverse Words in a String
摘要:Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".click to show clarification.Clarification:What constitutes a word?A sequence of non-space characters constitutes a word.Could the input string contain leading o 阅读全文

posted @ 2014-03-28 14:31 Eason Liu 阅读(380) 评论(0) 推荐(0)

[Leetcode] First Missing Positive
摘要:Given an unsorted integer array, find the first missing positive integer.For example,Given[1,2,0]return3,and[3,4,-1,1]return2.Your algorithm should run inO(n) time and uses constant space.方法比较巧,先把所有元素的值-1,重新组织元素使A[i] = i,这样只要找到第一个A[i] != i 的 i。注意要用while循环,for循环的continue是从下一次循环开始的。 1 class Solution { 阅读全文

posted @ 2014-03-28 14:23 Eason Liu 阅读(168) 评论(0) 推荐(0)

for循环中的break与continue
摘要:break: 跳出循环,执行for循环下面的语句。continue: 跳出本次循环,执行下次循环。 阅读全文

posted @ 2014-03-28 14:14 Eason Liu 阅读(671) 评论(0) 推荐(0)

上一页 1 ··· 10 11 12 13 14