算法
文章平均质量分 73
liuchenjane
这个作者很懒,什么都没留下…
展开
-
625. Minimum Factorization
Given a positive integer a, find the smallest positive integer b whose multiplication of each digit equals to a.If there is no answer or the answer is not fit in 32-bit signed integer, then return原创 2017-06-21 15:17:42 · 824 阅读 · 0 评论 -
390. Elimination Game
390. Elimination GameThere is a list of sorted integers from 1 to n. Starting from left to right, remove the first number and every other number afterward until you reach the end of the list.原创 2016-10-31 14:53:38 · 317 阅读 · 0 评论 -
139. Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, given s = “leetcode”, dict = [“leet”, “co转载 2016-10-20 21:02:10 · 290 阅读 · 0 评论 -
453. Minimum Moves to Equal Array Elements
453. Minimum Moves to Equal Array ElementsGiven a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n -原创 2016-11-11 10:58:43 · 287 阅读 · 0 评论 -
441. Arranging Coins
441. Arranging CoinsYou have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.Given n, find the total number of full stairc原创 2016-10-30 16:46:54 · 984 阅读 · 0 评论 -
386. Lexicographical Number
Given an integer n, return 1 - n in lexicographical order.For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9].Please optimize your algorithm to use less time and space. The input si转载 2016-10-19 22:02:07 · 318 阅读 · 0 评论 -
103. 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 tree原创 2016-10-04 22:25:52 · 229 阅读 · 0 评论 -
409. Longest Palindrome
409. Longest PalindromGiven a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.This is case sensitive, for原创 2016-10-02 21:49:04 · 705 阅读 · 0 评论 -
414. Third Maximum Number
414. Third Maximum NumberGiven an array of integers, return the 3rd Maximum Number in this array, if it doesn't exist, return the Maximum Number. The time complexity must be O(n) or less. 思路原创 2016-10-15 11:17:17 · 1132 阅读 · 1 评论 -
Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array IIFollow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array nums = [1,1,1,2,2,3],Your function转载 2016-09-27 21:51:44 · 257 阅读 · 0 评论 -
60. Permutation Sequence
60. Permutation SequenceThe set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, for n =转载 2016-11-03 20:37:51 · 388 阅读 · 0 评论 -
413. Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.For example, these are arithmetic sequence:1,原创 2016-10-12 20:30:54 · 1389 阅读 · 0 评论 -
Set Matrix Zeroes
Set Matrix ZeroesGiven a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.Follow up:Did you use extra space?A straight for转载 2016-09-27 10:22:33 · 411 阅读 · 0 评论 -
334. Increasing Triplet Subsequence
334. Increasing Triplet SubsequenceGiven an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.Formally the function should:Return true if t原创 2016-10-22 19:17:54 · 322 阅读 · 0 评论 -
Convert a Number to Hexadecimal
Convert a Number to HexadecimalGiven an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.Note:All letters in hexadecimal原创 2016-09-26 08:45:22 · 384 阅读 · 0 评论 -
Longest Palindromic Substring
QuestionGiven a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.翻译 2016-09-14 15:42:00 · 469 阅读 · 0 评论 -
444. Sequence Reconstruction
444. Sequence ReconstructionCheck whether the original sequence org can be uniquely reconstructed from the sequences in seqs. The org sequence is a permutation of the integers from 1转载 2016-10-31 16:54:08 · 1351 阅读 · 0 评论 -
458. Poor Pigs
458. Poor PigsThere are 1000 buckets, one and only one of them contains poison, the rest are filled with water. They all look the same. If a pig drinks that poison it will die within 15 minutes.转载 2016-11-13 21:25:07 · 3300 阅读 · 1 评论 -
二叉树祖先
LCA,lowest common ancestor(二叉树祖先问题0找出两个节点的最低的公共祖先(LCA,lowest common ancestor)eg: _______3______ / \ ___5__ ___1__/ \ / \6 _2_ 0 8转载 2017-06-15 22:50:50 · 2658 阅读 · 0 评论 -
约瑟夫问题解答
问题: 编号为1,2,····n的n个人按顺时针做成一圈,一开始任选一个正整数作为报数的上限值m,从第一个人开始按顺时针方向自1开始顺时针报数,报到m时停止,这个人出列。从他在顺时针方向上的下一个人开始重新报数,如此下去,直至所有人出列。下面是用STL中的list#include <iostream>#include <list>#include <algorithm>using names原创 2017-05-30 21:38:00 · 661 阅读 · 0 评论 -
Array Nesting
A zero-indexed array A consisting of N different integers is given. The array contains all integers in the range [0, N - 1].Sets S[K] for 0 <= K < N are defined as follows:S[K] = { A[K], A[A[K]], A[A[A原创 2017-05-28 21:19:27 · 1902 阅读 · 0 评论 -
字典序全排列递归总结:值传递与引用传递
问题:给定一个具有n个元素的集合(n>=1),要求输出这个集合中元素的所有可能的排列。 Given a collection of distinct numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [ [1,2,3], [1,3,2],原创 2017-05-28 20:45:22 · 478 阅读 · 0 评论 -
448. Find All Numbers Disappeared in an Array
448. Find All Numbers Disappeared in an ArrayGiven an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1,原创 2016-11-01 14:31:44 · 2025 阅读 · 0 评论 -
从求逆序对和mergeSort谈起(3)
Inversion Count for an array indicates – how far (or close) the array is from being sorted. If array is already sorted then inversion count is 0. If array is sorted in reverse order that inversion coun原创 2017-02-23 09:59:57 · 439 阅读 · 0 评论 -
从求逆序对和mergeSort谈起(2)
应用mergeSort的思想,可以解决许多和下标有关的问题。看了有关题的提示,发现有些能用Divide and Conqure解决的,也可以用 Binary Search Tree,Segment Tree,Binary Index Tree解决。它们都可以把原始的时间复杂度为O(n2)O(n^2)的算法降低为O(nlogn)O(nlogn). 下面从求数组的逆序数对这一问题,用不同的方法去解决。原创 2017-02-21 18:51:08 · 395 阅读 · 0 评论 -
56. Merge Intervals
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].解:按照Interval的start为关键字,递减的排序。 vector merge(vecto原创 2017-02-12 21:11:53 · 484 阅读 · 0 评论 -
从求逆序对和mergeSort谈起
先来谈谈mergeSort, 它是排序算法的一种,核心思想是:对一个数组nums[0,…,n],首先把它分成两部分nums[0,…,mid]和nums[mid+1,…,n],首先两个子数组是排好序的,只要对两个子数组进行整合,然后就排好序了,但是我们可以在对数组整合的时候,作好多事情,。。。 根据主定理,T(0,n)=T(0,(n-1)/2)+T((n-1)/2,n)+C, 其中C是合并时的时间复原创 2017-02-20 22:30:03 · 556 阅读 · 0 评论 -
动态规划预测游戏输赢的问题总结
在leetcode中,经常会遇到判断两人游戏,一方是输还是赢的问题。有guess number higher or lower, can I win,predict the winner等。这类问题都假设双方在最优策略下,甲方是否会赢。 这类问题都可以用动态规划来解决,关键在于采用top-down的备忘录策略,每解决一个小的子问题,都把相应的结果记录在备忘录上,下次遇到相同的问题时,直接查询即可原创 2017-02-09 11:09:31 · 2044 阅读 · 0 评论 -
4. Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 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)).Example 1:nums1 =原创 2017-01-13 10:51:44 · 406 阅读 · 0 评论 -
322. Coin Change
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money转载 2017-01-02 15:46:15 · 231 阅读 · 0 评论 -
469. Convex Polygon
469. Convex PolygonGiven a list of points that form a polygon when joined sequentially, find if this polygon is convex (Convex polygon definition).Note:There are at least 3 and at most转载 2016-12-04 19:58:18 · 1734 阅读 · 0 评论 -
红黑树RBT的(C++)实现
//RBT--红黑树//红黑树是“平衡”搜索树的一种//红黑树满足下面的性质://1.每个节点或是红色或是黑色//2.根节点是黑色的//3.每个叶节点(NIL) 是黑色的//4.如果一个节点是红色的,则它的两个子节点都是黑色的//5.对每个节点,从该节点到其所有后代叶节点的简单路径上,均包含相同数目的黑色节点 #include using namespace std;class原创 2016-11-19 19:21:24 · 773 阅读 · 1 评论 -
475. Heaters
Heaters Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses.Now, you are given positions of houses and heaters on a horizo原创 2016-12-11 13:34:59 · 1125 阅读 · 0 评论 -
396. Rotate Function
396. Rotate Function QuestionEditorial Solution My SubmissionsTotal Accepted: 1777Total Submissions: 6445Difficulty: EasyGiven an array of integers A and let n原创 2016-09-13 15:09:06 · 534 阅读 · 0 评论 -
392. Is Subsequence
392. Is Subsequence QuestionEditorial Solution My SubmissionsTotal Accepted: 5098Total Submissions: 11532Difficulty: MediumGiven a string s and a string t, check if原创 2016-09-13 15:39:14 · 456 阅读 · 0 评论 -
回溯法解决0_1背包问题(迭代和递归)
问题:0/1背包问题例子:weight数组代表物品重量,value数组代表物品价值,M代表背包容量。背包是按单位价值递减的顺序排列的,即value[i]/weight[i]>value[i-1]/weight[i-1].const int weight[Max]={1,11,21,23,33,43,45,55};const int value[Max]={11,21,31,33,43,53原创 2016-09-13 21:47:13 · 8871 阅读 · 2 评论 -
120. Triangle
120. TriangleGiven 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[ [转载 2016-10-10 16:24:59 · 211 阅读 · 0 评论 -
Add Strings
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2.Note:The length of both num1 and num2 is Both num1 and num2 contains only digits 0-9.B原创 2016-10-09 19:27:43 · 264 阅读 · 0 评论 -
31. Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible转载 2016-09-20 10:39:56 · 234 阅读 · 0 评论 -
Sort Colors
Sort ColorsGiven 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转载 2016-09-23 08:33:33 · 208 阅读 · 0 评论