LeetCode-SolutionLeetCode-Solution

本仓库为LeetCode练习题题解记录,会持续更新各种语言的题解。同时也欢迎使用各种语言的同♂好们加入此项目,为开源贡献力量。

LeetCode is the best platform to help you enhance your skills, expand your knowledge and prepare for technical interviews.

如果想知道各大公司 LeetCode 面试题,可以参看 Companies.md。

附上@Blankj的镇楼诗:

明有科举八股,今有 LeetCode。
八股定格式而取文采心意,LeetCode 定题目且重答案背诵。
美其名曰:”practice makes perfect.”
为何今不如古?
非也非也,
科举为国取士,LeetCode 为 Google 筛码工,各取所需也。

目录

题解汇总

题目 题解 难度 Java JavaScript kotlin
001 Two Sum Easy ✅ ✅ ✅
002 Add Two Numbers Medium ✅   ✅
003 Longest Substring Without Repeating Characters Medium ✅   ✅
004 Median of Two Sorted Arrays Hard ✅   ✅
005 Longest Palindromic Substring Medium ✅    
006 ZigZag Conversion Medium ✅    
007 Reverse Integer Easy ✅ ✅ ✅
008 String to Integer (atoi) Medium ✅    
009 Palindrome Number Easy ✅ ✅ ✅
010 Regular Expression Matching Hard ✅    
011 Container With Most Water Medium ✅    
012 Integer to Roman Medium ✅    
013 Roman to Integer Easy ✅ ✅ ✅
014 Longest Common Prefix Easy ✅ ✅ ✅
015 3Sum Medium ✅    
016 3Sum Closest Medium ✅    
017 Letter Combinations of a Phone Number Medium ✅    
018 4Sum Medium ✅    
019 Remove Nth Node From End of List Medium ✅    
020 Valid Parentheses Easy ✅ ✅ ✅
021 Merge Two Sorted Lists Easy ✅ ✅ ✅
022 Generate Parentheses Medium ✅    
023 Merge k Sorted Lists Hard ✅    
024 Swap Nodes in Pairs Medium ✅    
025 Reverse Nodes in k-Group Hard ✅    
026 Remove Duplicates from Sorted Array Easy ✅ ✅ ✅
027 Remove Element Easy ✅ ✅ ✅
028 Implement strStr() Easy ✅ ✅ ✅
029 Divide Two Integers Medium ✅    
030 Substring with Concatenation of All Words Hard ✅    
033 Search in Rotated Sorted Array Medium ✅    
035 Search Insert Position Easy ✅ ✅ ✅
038 Count and Say Easy ✅ ✅ ✅
043 Multiply Strings Medium ✅    
044 Wildcard Matching Hard ✅    
049 Group Anagrams Medium ✅    
050 Pow(x, n) Medium ✅    
053 Maximum Subarray Easy ✅ ✅ ✅
056 Merge Intervals Medium ✅    
057 Insert Interval Hard ✅    
058 Length of Last Word Easy ✅ ✅ ✅
066 Plus One Easy ✅ ✅ ✅
067 Add Binary Easy ✅ ✅ ✅
068 Text Justification Hard ✅    
069 Sqrt(x) Easy ✅ ✅ ✅
070 Climbing Stairs Easy ✅ ✅ ✅
083 Remove Duplicates from Sorted List Easy ✅ ✅ ✅
088 Merge Sorted Array Easy ✅ ✅ ✅
100 Same Tree Easy ✅ ✅ ✅
101 Symmetric Tree Easy ✅ ✅ ✅
104 Maximum Depth of Binary Tree Easy ✅ ✅ ✅
107 Binary Tree Level Order Traversal II Easy ✅ ✅ ✅
108 Convert Sorted Array to Binary Search Tree Easy ✅ ✅ ✅
110 Balanced Binary Tree Easy ✅ ✅ ✅
111 Minimum Depth of Binary Tree Easy ✅ ✅ ✅
112 Path Sum Easy ✅ ✅ ✅
118 Pascal’s Triangle Easy ✅ ✅ ✅
119 Pascal’s Triangle II Easy ✅ ✅ ✅
121 Best Time to Buy and Sell Stock Easy ✅ ✅ ✅
122 Best Time to Buy and Sell Stock II Easy ✅ ✅ ✅
125 Valid Palindrome Easy   ✅ ✅
136 Single Number Easy   ✅ ✅
141 Linked List Cycle Easy ✅   -
155 Min Stack Easy ✅ ✅ ✅
160 Intersection of Two Linked Lists Easy ✅   -
167 Two Sum II - Input array is sorted Easy   ✅ -
168 Excel Sheet Column Title Easy   ✅ -
169 Majority Element Easy   ✅ -
171 excel-sheet-column-number Easy   ✅ -
226 Invert Binary Tree Easy ✅ ✅ ✅
504 Base 7 Easy     ✅
543 Diameter of Binary Tree Easy ✅   ✅
554 Brick Wall Medium ✅    
561 Array Partition I Easy ✅ ✅  
643 Maximum Average Subarray I Easy ✅ ✅  
645 Set Mismatch Easy ✅ ✅  
653 Two Sum IV - Input is a BST Easy ✅ ✅  
657 Judge Route Circle Easy ✅ ✅  
661 Image Smoother Easy ✅ ✅  
665 Non-decreasing Array Easy ✅ ✅  
669 Trim a Binary Search Tree Easy ✅ ✅  
671 Second Minimum Node In a Binary Tree Easy ✅ ✅  
674 Longest Continuous Increasing Subsequence Easy ✅ ✅  
680 Valid Palindrome II Easy ✅ ✅  
682 Baseball Game Easy ✅ ✅  
686 Repeated String Match Easy ✅ ✅  
687 Longest Univalue Path Easy ✅ ✅  
693 Binary Number with Alternating Bits Easy ✅ ✅  
695 Max Area of Island Easy ✅ ✅  
696 Count Binary Substrings Easy ✅ ✅  
697 Degree of an Array Easy ✅ ✅  
717 1-bit and 2-bit Characters Easy ✅ ✅  
720 Longest Word in Dictionary Easy ✅ ✅  
724 Find Pivot Index Easy ✅ ✅  
728 Self Dividing Numbers Easy ✅ ✅  
733 Flood Fill Easy ✅ ✅  
744 Find Smallest Letter Greater Than Target Easy ✅ ✅  
746 Min Cost Climbing Stairs Easy ✅ ✅  
747 Largest Number At Least Twice of Others Easy ✅ ✅  
762 Prime Number of Set Bits in Binary Representation Easy ✅ ✅  
766 Toeplitz Matrix Easy ✅ ✅  
771 Jewels and Stones Easy ✅ ✅ ✅
783 Minimum Distance Between BST Nodes Easy ✅ ✅  
784 Letter Case Permutation Easy   ✅  
788 Rotated Digits Easy   ✅  
796 Rotate String Easy   ✅  
804 Unique Morse Code Words Easy ✅    
806 Number of Lines To Write String Easy   ✅  

库维护须知

请大家遵守规范,勤加维护。对规范有异议可以提出意见,经过讨论,并落地合理的规范。

注: 为方便阐述规范,以下内容以“使用Java语言解答第001题-Two Sum”为例进行说明。

本仓库的起源

为避免误解,以下称RichCodersAndMe/LeetCode-Solution为本仓库, 被介绍的仓库称为此仓库。

来源1: @Blankj的awsome-java-leetcode

此仓库由@Blankj于2017年4月18日创建, 不定期更新优质Java代码和题解,颇受好评,至今已有star数3521颗(star数量以仓库原址为准)。截止本仓库建立为止, 此仓库已累计拥有63题已AC的Java代码。

来源2: @Wang-YS的leetcode

此仓库由@Wang-YS于2018年2月28日创建,不定期更新优质JavaScript代码,属于个人练习项目。截止本仓库建立为止, 此仓库已累计拥有33题已AC的JavaScript代码。

来源3 @CodeAntStudio的LeetCode-Solution

此仓库由@Mukyu、@Taryn、@WangXin于2018年3月31日创建,由@Mukyu提供Java题解,@Taryn提供JavaScript题解,@WangXin提供kotlin题解,属于刚刚萌生想法的而创建的新仓库。截止本仓库建立为止, 此仓库已累计拥有5题已AC的Java代码、2题已AC的JavaScript代码、3题已AC的kotlin代码。

文档编写规范

举例说明:
当提交首页表格未收录的题目时:

总而言之三步走: 代码、题解、首页README.md

commit信息提交规范

feat:提交新的AC代码
fix:修补bug
docs:文档修改
style: 格式(不影响代码运行的变动)
refactor:重构(即不是新增功能,也不是修改bug的代码变动)
test:增加测试
chore:构建过程或辅助工具的变动

基本可以按照上述情况对号入座, 还是不明白的参看历史提交记录(git log)。

commit前先pull, 尽量避免产生不必要的git分叉.

commit信息模板: feat: add the solution of `Two Sum`(001) with Java

添加新语言代码

目前已使用的代码有: Java、JavaScript、kotlin 添加新的语言的解题代码, 请按字母序排列,在对应列添加解题代码且附链接,文档编写相关细节参看commit信息提交规范。

其他问题

1 同题的同种语言的代码如何处理?

建议保留一份即可(优先保留高效代码), 如果有更好/值得一说的思路,可以在题解中体现。 如果非要保留自己的代码, 请遵守以下规则:

2 未完善的题解

目前存在大量未完善的题解, 原因是@Wang-YS大佬的原仓库没有写题解,只有已AC的js代码。 大概分散在561~804题。那个啥,大家努力完善吧。

贡献者名单

排名不分先后