Leetcode
文章平均质量分 73
想改名的小雄鹿
东北某研究所的在读菜鸡研究生
展开
-
Container With Most Water "最多盛水问题"
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Fin原创 2017-09-13 08:57:48 · 510 阅读 · 0 评论 -
Longest Palindromic Substring 最长回文子串
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example:Input: "babad"Output: "bab"Note: "aba" is also a valid answer.Ex原创 2017-09-09 10:14:56 · 177 阅读 · 0 评论 -
Remove Nth Node From End of List "移除链表中的倒数第N项"
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原创 2017-09-13 13:31:43 · 277 阅读 · 0 评论 -
钱币组合问题(一):(每种硬币不限次数)
钱币组合问题(一):(每种硬币不限次数)假设我们有8种不同面值的硬币{1,2,5,10,20,50,100,200},用这些硬币组合够成一个给定的数值n。例如n=200,那么一种可能的组合方式为200 = 3 * 1 + 1*2 + 1*5 + 2*20 + 1 * 50 + 1 * 100. 问总过有多少种可能的组合方式?思路一:暴力穷举每种硬币最多为N/coin原创 2017-09-25 07:35:53 · 9648 阅读 · 0 评论 -
钱币组合问题(二):(每种硬币次数受限)
Description设有n种不同的钱币各若干,可用这n种钱币产生许多不同的面值。如给定面值7分,有1分3张,2分3张,5分1张,能组成给定面值7分的方法有如下4种:3个1分+2个2分;1个1分+3个2分;2个1分+1个5分;1个2分+1个5分。 你的编程任务:给定面值m,和n种不同钱币及其张数,求给定面值m能有多少种不同的构成方法数。 Input第1行原创 2017-09-25 11:12:41 · 1516 阅读 · 0 评论 -
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原创 2017-09-25 16:40:18 · 271 阅读 · 0 评论 -
House Robber:在不取相邻元素的情况下对数组求和
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原创 2017-09-26 20:12:19 · 284 阅读 · 0 评论 -
Valid Anagram:验证不考虑字母顺序不同的单词是否相同
Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false.Note:You may ass原创 2017-09-27 13:40:42 · 428 阅读 · 0 评论 -
Power of Two:判断一个数是否是2的幂
Given an integer, write a function to determine if it is a power of two.比如1,2,4,8,16,32,......思路:标准的按位操作数字,把每棵树看成2进制,则可写成00001,00010,00100,01000,10000,....即最多含有一个1,所以按位与,统计1的个数即可public b原创 2017-09-27 21:01:46 · 290 阅读 · 0 评论 -
Combination Sum III:从0-9中选取K个数求和等于n
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.Example 1:Inpu原创 2017-09-28 08:05:44 · 410 阅读 · 0 评论 -
Reverse Integer:将给定数字倒序返回
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321click to show spoilers.Note:The input is assumed to be a 32-bit signed integer. Your function s原创 2017-09-28 10:30:44 · 672 阅读 · 0 评论 -
Letter Combinations of a Phone Number:给定数字求出对应的多个字母的组合可能问题
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原创 2017-10-06 19:13:21 · 613 阅读 · 0 评论 -
Binary Watch:将十位二进制数转换成时间格式
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).Each LED represents a zero or one, with the least significant bit on原创 2017-10-06 21:26:11 · 845 阅读 · 0 评论 -
Valid Palindrome:验证是否为回文结构
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a原创 2017-09-29 10:23:12 · 287 阅读 · 0 评论 -
幸运数4&7:给定正整数n,求不大于n的仅由4和7组成的数字的个数
题目描述:我们称一个十进制正整数是幸运数当且仅当它只由数字4和7构成。现在给出一个正整数n,你需要计算由多少个不大于n的幸运数,由于答案可能非常大,你只需要输出答案除以1000000007后的余数。输入:第一行包含一个整数n,1输出:输出对应的答案测试样例:125621857711254朴素思路:逐个穷举结果:超时,因为数目过大符号说原创 2017-09-21 08:01:02 · 4305 阅读 · 2 评论 -
N-Queens:N*N位置行列45'斜线无重复元素
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queens puzzle.原创 2017-10-08 10:41:27 · 226 阅读 · 0 评论 -
N-Queens II:只记录下结果即可的N皇后问题
Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.思路:还是回溯,只不过把打印结果换成计数即可。class Solution { int sum = 0;原创 2017-10-08 11:07:21 · 247 阅读 · 0 评论 -
Combinations:给定数据范围与位数,求出
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]原创 2017-10-08 11:35:43 · 157 阅读 · 0 评论 -
Edit Distance:只包含插入、删除、替换三种操作的编辑距离
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:原创 2017-10-08 15:55:56 · 1257 阅读 · 0 评论 -
Sqrt(x):求方差
Implement int sqrt(int x).Compute and return the square root of x.思路一:二分搜索。因为搜索范围在1-(x/2)+1之间,所以二分搜索出答案即可思路二:牛顿法。如图,通过切线逐步逼近答案。可以百度牛顿法,或者看看数值计算那本书。这里因为,由上图可知,当f(x) = 0,X0为Xn,X为Xn+1时即可求出下原创 2017-10-08 19:44:04 · 534 阅读 · 0 评论 -
group anagrams :颠倒字母顺序构成同一组词
Given an array of strings, group anagrams together.For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return:[ ["ate", "eat","tea"], ["nat","tan"], ["bat"]]Note: Al原创 2017-09-21 18:44:48 · 469 阅读 · 0 评论 -
Maximum Subarray:数组中最大连续区间和
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] ha原创 2017-10-09 07:37:12 · 607 阅读 · 0 评论 -
Maximum Product Subarray:数组中最大连续区间求积
Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest原创 2017-10-09 09:06:26 · 330 阅读 · 0 评论 -
House Robber II:打家劫舍 取非相邻元素求和最大,且认为第一个元素与最后一个元素相邻
Note: This is an extension of House Robber.After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time原创 2017-10-01 09:46:32 · 471 阅读 · 0 评论 -
House Robber III:打家劫舍 在二叉树结构中取非相邻元素求和取最大
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour原创 2017-10-01 14:51:08 · 725 阅读 · 1 评论 -
Spiral Matrix:旋转打印矩阵
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 ]]转载 2017-10-10 10:47:17 · 293 阅读 · 0 评论 -
Spiral Matrix II:螺旋打印方阵
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.For example,Given n = 3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [原创 2017-10-10 16:04:22 · 203 阅读 · 0 评论 -
Spiral Matrix II:螺旋打印方阵
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.For example,Given n = 3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4原创 2017-10-10 16:07:19 · 202 阅读 · 0 评论 -
Count and Say:循环记录字符串中数字个数
The count-and-say sequence is the sequence of integers with the first five terms as following:1. 12. 113. 214. 12115. 1112211 is read off as "one 1" or 11.11 is read原创 2017-10-27 07:39:06 · 209 阅读 · 0 评论 -
Add Strings:字符串相加
Given two non-negative integers 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.Bo原创 2017-10-27 12:23:38 · 226 阅读 · 0 评论 -
Permutations:排列
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], [2,1,3], [2,3,1], [3,1,2], [3,2,1原创 2017-10-27 13:41:31 · 142 阅读 · 0 评论 -
Permutations II:有重复元素的排列
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], [2,1原创 2017-10-27 15:12:21 · 369 阅读 · 0 评论 -
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原创 2017-10-27 18:28:54 · 244 阅读 · 0 评论 -
Add Digits:逐位求和直至只剩一位
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit,原创 2017-10-05 17:09:16 · 205 阅读 · 0 评论 -
ZigZag Conversion:把字符串改写成Z字排列
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I原创 2017-10-06 11:01:34 · 277 阅读 · 0 评论 -
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 c原创 2017-10-06 13:38:13 · 350 阅读 · 0 评论 -
Combination Sum:非重复数组中选取若干元素求和等于给定值
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen原创 2017-10-06 14:03:22 · 786 阅读 · 0 评论 -
Stickers to Spell Word:多个单词拆拼接成完整以个目标单词所耗最小数目
We are given N different types of stickers. Each sticker has a lowercase English word on it.You would like to spell out the given target string by cutting individual letters from your collection o原创 2017-10-29 08:32:02 · 342 阅读 · 0 评论 -
Majority Element II:求个数多余1/3的元素
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. 思路:朴素想法肯定是排序后遍历,但是限制了复杂度,所以类似于上提,考虑摩尔投票发。首先,原创 2017-10-12 09:34:33 · 216 阅读 · 0 评论 -
Majority Element:找最多且多于一半的元素
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element原创 2017-10-12 09:36:26 · 192 阅读 · 0 评论