E-COM-NET
首页
在线工具
Layui镜像站
SUI文档
联系我们
推荐频道
Java
PHP
C++
C
C#
Python
Ruby
go语言
Scala
Servlet
Vue
MySQL
NoSQL
Redis
CSS
Oracle
SQL Server
DB2
HBase
Http
HTML5
Spring
Ajax
Jquery
JavaScript
Json
XML
NodeJs
mybatis
Hibernate
算法
设计模式
shell
数据结构
大数据
JS
消息中间件
正则表达式
Tomcat
SQL
Nginx
Shiro
Maven
Linux
Leetcode题解
LeetCode题解
——Create Maximum Number
Giventwoarraysoflength m and n withdigits 0-9 representingtwonumbers.Createthemaximumnumberoflength kk&&j>0&&ans[j-1]0.Whatiftheyareequalagain?Wecontinuetolookthenextdigituntiltheyarenotequal.Ifalldig
u010025211
·
2016-01-16 11:00
dp
greedy
LeetCode题解
——Power of Three
Givenaninteger,writeafunctiontodetermineifitisapowerofthree.Followup:Couldyoudoitwithoutusinganyloop/recursion?Credits:Specialthanksto @dietpepsi foraddingthisproblemandcreatingalltestcases.classSolut
u010025211
·
2016-01-08 18:00
LeetCode
Math
LeetCode题解
——Compare Version Numbers
Comparetwoversionnumbers version1 and version2.If version1 > version2 return1,if version1 num2)return1; num1=0; num2=0; i++; j++; } return0; } };
u010025211
·
2016-01-06 19:00
LeetCode
String
LeetCode题解
——wordBreak2
Givenastring s andadictionaryofwords dict,addspacesin s toconstructasentencewhereeachwordisavaliddictionaryword.Returnallsuchpossiblesentences.Forexample,givens = "catsanddog",dict = ["cat","cats","an
u010025211
·
2016-01-06 18:00
LeetCode
dp
backtracking
Leetcode题解
——Word Break
Givenastring s andadictionaryofwords dict,determineif s canbesegmentedintoaspace-separatedsequenceofoneormoredictionarywords.Forexample,givens = "leetcode",dict = ["leet","code"].Returntruebecause "le
u010025211
·
2016-01-06 17:00
LeetCode
dp
recursive
LeetCode题解
——Rotate Image
Youaregivenan n x n 2Dmatrixrepresentinganimage.Rotatetheimageby90degrees(clockwise).Followup:Couldyoudothisin-place?/* *clockwiserotate *firstreverseuptodown,thenswapthesymmetry *123789741 *456=>456=
u010025211
·
2015-12-16 22:00
LeetCode
LeetCode题解
——Substring with Concatenation of All Words
Youaregivenastring, s,andalistofwords, words,thatareallofthesamelength.Findallstartingindicesofsubstring(s)in s thatisaconcatenationofeachwordin wordsexactlyonceandwithoutanyinterveningcharacters.Fore
u010025211
·
2015-12-16 21:00
LeetCode
String
map
table
hash
LeetCode题解
——Remove Invalid Parentheses
Removetheminimumnumberofinvalidparenthesesinordertomaketheinputstringvalid.Returnallpossibleresults.Note:Theinputstringmaycontainlettersotherthantheparentheses ( and ).Examples:"()())()"->["()()()","(
u010025211
·
2015-12-14 22:00
LeetCode
backtracking
LeetCode题解
——Sudoku Solver
WriteaprogramtosolveaSudokupuzzlebyfillingtheemptycells.Emptycellsareindicatedbythecharacter '.'.Youmayassumethattherewillbeonlyoneuniquesolution.Asudokupuzzle...classSolution{ public: boolcol[10][10]
u010025211
·
2015-12-01 20:00
LeetCode
backtracking
LeetCode题解
——Evaluate Reverse Polish Notation
Evaluatethevalueofanarithmeticexpressionin ReversePolishNotation.Validoperatorsare +, -, *, /.Eachoperandmaybeanintegeroranotherexpression.Someexamples:["2","1","+","3","*"]->((2+1)*3)->9 ["4","13","5
u010025211
·
2015-11-27 15:00
LeetCode
stack
LeetCode题解
——First Missing Positive
Givenanunsortedintegerarray,findthefirstmissingpositiveinteger.Forexample,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Youralgorithmshouldrunin O(n)timeandusesconstantspace.解题思路:如果可以对数组排序就好解决了,但是排序的
u010025211
·
2015-11-22 19:00
LeetCode
array
sort
LeetCode题解
——Minimum Path Sum
Givena m x n gridfilledwithnon-negativenumbers,findapathfromtoplefttobottomrightwhich minimizes thesumofallnumbersalongitspath.Note: Youcanonlymoveeitherdownorrightatanypointintime.解题思路:动态规划,p[i][j]表示
u010025211
·
2015-11-18 20:00
LeetCode
dp
LeetCode题解
——Path Sum II
Givenabinarytreeandasum,findallroot-to-leafpathswhereeachpath'ssumequalsthegivensum.Forexample:Giventhebelowbinarytreeand sum=22,5 /\ 48 //\ 11134 /\/\ 7251 return[ [5,4,11,2], [5,8,4,5] ] 解题思路:在paths
u010025211
·
2015-11-18 19:00
LeetCode
SUM
Path
BinaryTree
LeetCode题解
——Path Sum
Givenabinarytreeandasum,determineifthetreehasaroot-to-leafpathsuchthataddingupallthevaluesalongthepathequalsthegivensum.Forexample:Giventhebelowbinarytreeand sum=22,5 /\ 48 //\ 11134 /\\ 721 returntru
u010025211
·
2015-11-17 21:00
LeetCode
SUM
Path
BinaryTree
LeetCode题解
——Sum Root to Leaf Numbers
Givenabinarytreecontainingdigitsfrom 0-9 only,eachroot-to-leafpathcouldrepresentanumber.Anexampleistheroot-to-leafpath 1->2->3 whichrepresentsthenumber 123.Findthetotalsumofallroot-to-leafnumbers.Fore
u010025211
·
2015-11-17 21:00
TO
root
SUM
num
Leaf
BinaryTree
LeetCode题解
——Reverse Integer
题目: 数字翻转,即输入123,返回321;输入-123,返回-321。 代码: 1 class Solution { 2 public: 3 int reverse(int x) { 4 int result = 0, sign = 1; 5 if(x < 0) //负数转换为正数统一处理 6 { 7
·
2015-11-11 15:12
LeetCode
LeetCode题解
——ZigZag Conversion
题目: 把一个字符串按照Z型排列后打印出来,例如 "PAYPALISHIRING" 重新排列后为3行,即 P A H N A P L S I I G Y I R 那么输出为"PAHNAPLSIIGYIR" 解法: 细节实现题,假如重新排列为5行,那么排列后的下标分布应该为 0 &n
·
2015-11-11 15:11
conversion
LeetCode题解
——Add Two Numbers
题目: 两个数字求和,数字用链表表示,每一个结点代表一位。链表顺序与数字顺序相反,即表头存放数字的最低位。 解法: 分别遍历两个链表的每个结点,对两个结点求和即可。要维护一个变量保存每次相加之后的进位。 更常见的,链表顺序与数字顺序相同,那么做一次链表逆序,求和之后再逆序回来即可。 代码: 1 /** 2 * Definition for sin
·
2015-11-11 15:10
LeetCode
LeetCode题解
——Longest Substring Without Repeating Characters
题目: 给定一个字符串,返回其中不包含重复字符的最长子串长度。 解法: 维持两个指针,第一个指向子串开始,第二个负责遍历,当遍历到的字符出现在子串内时,应计算当前子串长度,并更新最长值;然后第一个指针更新为出现位置的下一个。 代码: 1 class Solution { 2 public: 3 int lengthOfLongestSubs
·
2015-11-11 15:09
substring
LeetCode题解
——Longest Palindromic Substring
题目: 给定一个字符串S,返回S中最长的回文子串。S最长为1000,且最长回文子串是唯一。 解法: ①遍历,对于每个字符,计算以它为中心的回文子串长度(长度为奇数),同时计算以它和右边相邻字符为中心的回文子串长度(长度为偶数)。时间为O(N2)。 ②另外,有一个很奇妙的算法,称为Manacher算法,参考 http://www.cnblogs.com/daolua
·
2015-11-11 15:09
substring
LeetCode题解
——Median of Two Sorted Arrays
题目: 找两个排序数组A[m]和B[n]的中位数,时间复杂度为O(log(m+n))。 解法: 更泛化的,可以找第k个数,然后返回k=(m+n)/2时的值。 代码: 1 class Solution 2 { 3 public: 4 double findMedianSortedArrays(int A[], int m, int B[],
·
2015-11-11 15:08
LeetCode
LeetCode题解
——Two Sum
题目地址:https://oj.leetcode.com/problems/two-sum/ 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 i
·
2015-11-11 15:07
LeetCode
LeetCode题解
——4Sum
题目: 给定一个数组,找出其中和为0的所有4个数组合,每个组合内的4个数非递降。 解法: ①先排序,然后利用4个指针,前两个遍历,后两个在第二个指针之后的部分里夹逼,时间O(N3)。 ②或者利用一个哈希表先保存每两个数的和,然后类似于查找两个数的和等于给定值的过程。 代码: ① 1 class Solution { 2 public: 3
·
2015-11-11 10:04
LeetCode
LeetCode题解
——3Sum Closest
题目: 给定一个数组,和一个指定的值,找出数组中3个数,它的和最接近这个指定值,并返回这个和。 解法: 和上一题找3个数的和为0一样,先排序再遍历,这一次不需要记录路径。 代码: 1 class Solution { 2 public: 3 int threeSumClosest(vector<int> &num, in
·
2015-11-11 10:03
LeetCode
LeetCode题解
——3Sum
题目: 给定一个数组,找出其中和为0的所有3个数的组合。每个组合的3个数都是非递降的。 解法: 先排序再遍历,设置3个指针,第一个依次遍历,第二三个在第一个指针后面的部分里,左右夹逼查找和为第一个数的相反数的组合。时间O(N2)。 代码: 1 class Solution { 2 public: 3 vector<vector<
·
2015-11-11 10:02
LeetCode
LeetCode题解
——Longest Common Prefix
题目: 给定一系列的字符串,找出这些字符串的最长公共前缀。 解法: 暴力法,依次比较每个字符串的每个字符,碰到第一个不同的就返回之前找到的前缀。 代码: 1 class Solution { 2 public: 3 string longestCommonPrefix(vector<string> &strs) { 4
·
2015-11-11 10:02
LeetCode
LeetCode题解
——Roman to Integer
题目: 将罗马数字转换为整数。 解法: 可以参考上一篇数字转换为罗马数字的规则。 代码: 1 class Solution { 2 public: 3 int sym2int(char sym) //罗马数字字符与数字的对应关系 4 { 5 switch(sym) 6 { 7 case
·
2015-11-11 10:01
LeetCode
LeetCode题解
——Integer to Roman
题目: 将整数转换为罗马数字。罗马数字规则可以参考: 维基百科-罗马数字 解法: 类似于进制转换,从大的基数开始,求整数对基数的商和余,来进行转换。 代码: 1 class Solution { 2 public: 3 string intToRoman(int num) { 4 string result; 5
·
2015-11-11 10:00
LeetCode
LeetCode题解
——Container With Most Water
题目: x轴上有一些点,每个点上有一条与x轴垂直的线(线的下端就是这个点,不超出x轴),给出每条线的高度,求这些线与x轴组成的最大面积。 解法: 贪心策略,维持两个指针,分别指向第一个和最后一个元素,对于其中小的一个,它所能围成的最大面积就是到另一个元素之间,所以小的一个要往中间走一步。 代码: 1 class Solution { 2 public
·
2015-11-11 10:59
LeetCode
LeetCode题解
——Palindrome Number
题目: 判断一个数字是不是回文数字,即最高位与最低位相同,次高位与次低位相同,... 解法: 求出数字的位数,然后依次求商和求余判断是否相等。 代码: 1 class Solution { 2 public: 3 bool isPalindrome(int x) { 4 if(x < 0) //负数有符号,肯定不是
·
2015-11-11 10:59
LeetCode
LeetCode题解
——Regular Expression Matching
题目: 正则表达式的匹配,'.'能匹配任何一个字符,'*'之前必须有一个字符,两个结合起来表示之前那个字符出现0到无穷次。 解法: 一定要注意'*'必须结合前面的字符一起使用。 代码: 1 class Solution { 2 public: 3 bool isMatch(const char *s, const char *p) { 4
·
2015-11-11 10:58
LeetCode题解
——String to Integer(atoi)
题目: 字符串转换为数字。 解法: 这道题的意思是要考虑到,如果有前置的空字符,则跳过;如果超出数字范围,则返回最大/最小整数;如果碰到第一个不能转换的字符,则返回。 代码: 1 class Solution { 2 public: 3 int atoi(const char *str) { 4 int sign = 1,
·
2015-11-11 10:57
LeetCode
LeetCode题解
——Unique Path(DP与优化)
题目: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
·
2015-11-11 00:52
LeetCode
LeetCode题解
——Balanced Binary Tree
Givenabinarytree,determineifitisheight-balanced.Forthisproblem,aheight-balancedbinarytreeisdefinedasabinarytreeinwhichthedepthofthetwosubtreesof every nodeneverdifferbymorethan1.递归判断左子树是否是平衡二叉树,递归判断右子
u010025211
·
2015-11-08 21:00
LeetCode
tree
binary
depth
LeetCode题解
——Flatten Binary Tree to Linked List
Givenabinarytree,flattenittoalinkedlistin-place.Forexample,Given1 /\ 25 /\\ 346 Theflattenedtreeshouldlooklike:1 \ 2 \ 3 \ 4 \ 5 \ 6Hints:Ifyounoticecarefullyintheflattenedtree,eachnode'srightchildpoi
u010025211
·
2015-11-08 21:00
LeetCode
tree
binary
flatten
LeetCode题解
——Basic Calculator
Theexpressionstringmaycontainopen ( andclosingparentheses ),theplus + orminussign -, non-negative integersandemptyspaces .Youmayassumethatthegivenexpressionisalwaysvalid.Someexamples:"1+1"=2 "2-1+2"=3
u010025211
·
2015-11-08 17:00
LeetCode
String
stack
Parentheses
LeetCode题解
——Different Ways to Add Parentheses
Givenastringofnumbersandoperators,returnallpossibleresultsfromcomputingallthedifferentpossiblewaystogroupnumbersandoperators.Thevalidoperatorsare+, - and *.Example1Input: "2-1-1".((2-1)-1)=0 (2-(1-1))
u010025211
·
2015-11-08 11:00
LeetCode
String
dp
recursively
LeetCode题解
——Unique Binary Search Trees II
Given n,generateallstructurallyunique BST's (binarysearchtrees)thatstorevalues1...n.Forexample,Given n =3,yourprogramshouldreturnall5uniqueBST'sshownbelow.13321 \///\\ 321132 //\\ 2123 想法一:递归,思想见Uniqu
u010025211
·
2015-11-07 22:00
LeetCode
unique
BST
BST
LeetCode题解
——Unique Binary Search Trees
题目:Given n,howmanystructurallyunique BST's (binarysearchtrees)thatstorevalues1...n?Forexample,Given n =3,thereareatotalof5uniqueBST's.13321 \///\\ 321132 //\\ 2123解题思路:Theproblemcanbesolvedinadynamicp
u010025211
·
2015-11-07 20:00
LeetCode
unique
BST
BST
LeetCode题解
——Symmetric Tree
题目:Givenabinarytree,checkwhetheritisamirrorofitself(ie,symmetricarounditscenter).Forexample,thisbinarytreeissymmetric:1 /\ 22 /\/\ 3443 Butthefollowingisnot:1 /\ 22 \\ 33 解题思路:(1)如果一个树是对称的,那么左子树和右子树的关
u010025211
·
2015-11-06 14:00
LeetCode
tree
Symmetric
LeetCode题解
——Copy List with Random Pointer
Alinkedlistisgivensuchthateachnodecontainsanadditionalrandompointerwhichcouldpointtoanynodeinthelistornull.Returnadeepcopyofthelist.Subscribe toseewhichcompaniesaskedthisquestionAsanoptimisedsolution,
u010025211
·
2015-11-03 21:00
LeetCode
list
copy
Random
pointer
linked
LeetCode题解
——Binary Search Tree Iterator
mplementaniteratoroverabinarysearchtree(BST).YouriteratorwillbeinitializedwiththerootnodeofaBST.Calling next() willreturnthenextsmallestnumberintheBST.Note: next() and hasNext() shouldruninaverageO(1)
u010025211
·
2015-11-03 18:00
LeetCode
tree
search
iterator
binary
LeetCode题解
——Reverse Words in a String
Givenaninputstring,reversethestringwordbyword.Forexample,Givens="theskyisblue",return"blueisskythe".Trytosolveit in-place in O(1)space.Clarification:Whatconstitutesaword?Asequenceofnon-spacecharacters
u010025211
·
2015-11-03 17:00
LeetCode
String
reverse
words
LeetCode题解
(13)--Roman to Integer
https://leetcode.com/problems/roman-to-integer/ 原题: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 思路: 关键是要搞清罗马数字规则。PS:不用考虑错误输
·
2015-11-02 17:39
LeetCode
LeetCode题解
(9)--Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. 思想: 先计算出这个整数的逆序数,然后比较它和原来的数每位是否都相同即可。另外要注意负数没有回文数,还应该考虑overflow一定不是回文数。 AC代码: 1 class Solution { 2 pub
·
2015-11-02 17:38
LeetCode
LeetCode题解
(7)--Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 思想:一个整数的倒序,没啥说的,在long类型上比较来避免overflow。 AC代码: 1 class Solution { 2 pub
·
2015-11-02 17:37
LeetCode
LeetCode题解
(8)--String to Integer (atoi)
Implement atoi to 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 poss
·
2015-11-02 17:37
LeetCode
LeetCode题解
(6)--ZigZag Conversion
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)
·
2015-11-02 17:36
conversion
LeetCode题解
(3)-- Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which t
·
2015-11-02 17:35
substring
LeetCode题解
(2)-- Add Two Numbers
原题: https://leetcode.com/problems/add-two-numbers/ You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes c
·
2015-11-02 17:35
LeetCode
上一页
22
23
24
25
26
27
28
29
下一页
按字母分类:
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
其他