
LeetCode
LeetCode Problem Solving Report
15wylu
小学生程序员
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
151. Reverse Words in a String
Description https://leetcode.com/problems/reverse-words-in-a-string/ 给定一个字符串,按单词反转。要求忽略前导、尾部空格,单词间的多余空格保留一个。 Input: "the sky is blue", Output: "blue is sky the". Solving Ideas 类似 《剑指offer》 翻转单词顺序,不同的...原创 2019-01-09 15:47:36 · 323 阅读 · 0 评论 -
541. Reverse String II
Description https://leetcode.com/problems/reverse-string-ii/ 给定一个字符串和一个整数k,需要反转从字符串开头算起的每2k个字符的前k个字符。 如果剩下少于k个字符,则反转所有字符; 如果小于2k但大于或等于k个字符,则反转前k个字符。 Solving Ideas https://leetcode.com/problems/reverse...原创 2019-01-09 14:31:27 · 159 阅读 · 0 评论 -
344. Reverse String
Description https://leetcode.com/problems/reverse-string/ 字符串反转 Solving Ideas 时间复杂度:O(n)O(n)O(n) 空间复杂度:O(n)O(n)O(n) Solution class Solution { public String reverseString(String s) { // retu...原创 2019-01-09 13:50:06 · 142 阅读 · 1 评论 -
92. Reverse Linked List II
Description https://leetcode.com/problems/reverse-linked-list-ii/ 将链表第m个结点至第n个结点的部分反转,要求只扫描一遍链表 Input: 1->2->3->4->5->NULL, m = 2, n = 4 Output: 1->4->3->2->5->NULL Solv...原创 2019-01-09 13:31:22 · 127 阅读 · 0 评论 -
206. Reverse Linked List
Description https://leetcode.com/problems/reverse-linked-list/ 反转单向链表 Solving Ideas https://leetcode.com/problems/reverse-linked-list/solution/ 时间复杂度:O(n)O(n)O(n) 空间复杂度:O(1)O(1)O(1) Solution /** * De...原创 2019-01-08 23:10:58 · 128 阅读 · 0 评论 -
LeetCode 72. Edit Distance
Description https://leetcode.com/problems/edit-distance/ 给定两个单词word1和word2,找到将word1转换为word2所需的最小操作次数。每次允许的操作:插入、删除、修改一个字母 Solving Ideas https://www.youtube.com/watch?v=We3YDTzNXEk 动态规划: State: dp[i][j...原创 2019-01-06 17:44:36 · 153 阅读 · 0 评论 -
64. Minimum Path Sum
Description https://leetcode.com/problems/minimum-path-sum/ 给定一个m∗nm*nm∗n的矩阵grid,矩阵所有元素都为非负整数,求从矩阵左上角到右下角的最短路径和,移动方向只能向右或向下 Solving Ideas https://blog.youkuaiyun.com/qq_32767041/article/details/85928140 动态规...原创 2019-01-06 15:48:43 · 229 阅读 · 0 评论 -
63. Unique Paths II
Description https://leetcode.com/problems/unique-paths-ii/ Solving Ideas https://leetcode.com/problems/unique-paths-ii/solution/ 动态规划: State: dp[i][j]: 从grid[0][0]到grid[i][j]的路径数 State Transition: dp...原创 2019-01-06 15:22:36 · 135 阅读 · 0 评论 -
62. Unique Paths
Description https://leetcode.com/problems/unique-paths/ Solving Ideas https://www.youtube.com/watch?v=GO5QHC_BmvM 动态规划: dp[i][j]: 从grid[0][0]到grid[i][j]的路径数 dp[0][j] = 1,dp[i][0] = 1 dp[i][j] = dp[i ...原创 2019-01-06 14:40:34 · 146 阅读 · 0 评论 -
32. Longest Valid Parentheses
Description https://leetcode.com/problems/longest-valid-parentheses/ Solving Ideas https://leetcode.com/problems/longest-valid-parentheses/solution/ 动态规划: dp[i]: 以第 i 个元素为结尾的最长有效子串的长度 if s[i] = ‘)’ ...原创 2019-01-06 14:14:40 · 258 阅读 · 0 评论 -
6. ZigZag Conversion
Description https://leetcode.com/problems/zigzag-conversion/ Solving Ideas https://leetcode.com/problems/zigzag-conversion/solution/ 遍历字符串,可以轻松确定每个字符所属的ZigZag模式中的哪一行。 使用min(numRows, s.length())确定ZigZ...原创 2019-01-02 12:02:22 · 216 阅读 · 0 评论 -
8. String to Integer (atoi)
Description https://leetcode.com/problems/string-to-integer-atoi/ 题意: 实现一个将字符串转换为整数的函数。 Solving Ideas 详见注释 Solution class Solution { public int myAtoi(String str) { if (str == null) return...原创 2019-01-01 16:56:05 · 136 阅读 · 0 评论 -
516. Longest Palindromic Subsequence
Description https://leetcode.com/problems/longest-palindromic-subsequence/ 题意: 给定一个字符串,寻找最长回文子序列,返回其长度。 Solving Ideas https://www.youtube.com/watch?v=_nCsPn7_OgI&t=57s 动态规划: dp[i][j]: s.substring...原创 2018-12-31 23:31:04 · 257 阅读 · 0 评论 -
5. Longest Palindromic Substring
Description https://leetcode.com/problems/longest-palindromic-substring/ 题意: 给定一个字符串,寻找最长回文子串,返回其长度。 Solving Ideas https://leetcode.com/problems/longest-palindromic-substring/solution/ 若任意一个回文的中心设为C,则...原创 2018-12-31 17:14:09 · 143 阅读 · 0 评论 -
409. Longest Palindrome
Description https://leetcode.com/problems/longest-palindrome/ 题意: 给定一个由小写或大写字母组成的字符串,找到可以用这些字母构建的最长回文串的长度。 Solving Ideas https://leetcode.com/problems/longest-palindrome/solution/ 首先统计每个字母出现的次数,然后利用贪心...原创 2018-12-30 23:15:27 · 142 阅读 · 0 评论 -
3. Longest Substring Without Repeating Characters
Description https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题意: 给定一个字符串,返回最长无重复字符子串的长度。 Solving Ideas https://leetcode.com/problems/longest-substring-without-repeating-ch...原创 2018-12-30 22:49:29 · 110 阅读 · 0 评论 -
7. Reverse Integer
Description https://leetcode.com/problems/reverse-integer/ 题目大意: 给定一个有符号的32位整数,返回整数反转后的结果,反转后如果溢出则返回0。 Solving Ideas https://leetcode.com/problems/reverse-integer/solution/ 在执行res = res * 10 + pop前需要先...原创 2018-12-29 20:32:55 · 91 阅读 · 0 评论 -
1. Two Sum
Description https://leetcode.com/problems/two-sum/ 题目大意: 给定一个整数数组,返回两个数字的索引,使它们对应的元素相加等于target。 Solving Ideas https://leetcode.com/problems/two-sum/solution/ Solution import java.util.HashMap; class ...原创 2018-12-29 18:33:19 · 106 阅读 · 0 评论 -
4. Median of Two Sorted Arrays
Description https://leetcode.com/problems/median-of-two-sorted-arrays/ 题目大意: 给定两个有序数组nums1和nums2,长度分别为m和n,要求在O(log(m+n))时间复杂度内找出两个有序数组的中位数。 Solving Ideas https://www.youtube.com/watch?v=LPFhl65R7ww 基本...原创 2018-12-29 17:28:34 · 120 阅读 · 0 评论 -
445. Add Two Numbers II
Description https://leetcode.com/problems/add-two-numbers-ii/ 题目大意: 给定两个非空链表,表示两个非负整数,每个结点包含一个数字。对这两个非负整数求和并将其作为链表返回。除了数字0本身,这两个非负整数不包含前导0。要求不修改原列表。 Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -&g...原创 2018-12-28 20:37:35 · 430 阅读 · 0 评论 -
2. Add Two Numbers
Description https://leetcode.com/problems/add-two-numbers/ 题目大意: 给定两个非空链表,表示两个非负整数。数字以相反的顺序存储,每个结点包含一个数字。对这两个非负整数求和并将其作为链表返回。除了数字0本身,这两个非负整数不包含前导0。 Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Out...原创 2018-12-28 17:33:09 · 448 阅读 · 0 评论 -
371. Sum of Two Integers
Description https://leetcode.com/problems/sum-of-two-integers/ 题目大意: 不用加减做加法。 Solving Ideas 位运算,参考剑指offer 不用加减乘除做加法 Solution class Solution { public int getSum(int a, int b) { int sum, car...原创 2018-12-28 15:36:43 · 108 阅读 · 0 评论 -
147. Insertion Sort List
Description https://leetcode.com/problems/insertion-sort-list/ 题目大意: 使用插入排序算法对链表进行排序。 Solving Ideas 插入排序 Solution /** * Definition for singly-linked list. * public class ListNode { * int val; ...原创 2018-12-28 14:44:51 · 121 阅读 · 0 评论 -
148. Sort List
Description https://leetcode.com/problems/sort-list/ 题目大意: 对链表进行排序。要求时间复杂度O(n log(n)),空间复杂度O(1)。 Solving Ideas https://leetcode.com/problems/sort-list/discuss/46714/Java-merge-sort-solution 基于归并排序,需要l...原创 2018-12-28 12:43:31 · 126 阅读 · 0 评论 -
260. Single Number III
Description https://leetcode.com/problems/single-number-iii/ 题目大意: 给定一个数组,所有元素都出现两次,除了两个元素只出现一次,找出这两个只出现一次的元素。要求时间复杂度O(n),空间复杂度O(1)。 Solving Ideas https://blog.youkuaiyun.com/qq_32767041/article/details/838...原创 2018-12-27 18:55:37 · 131 阅读 · 0 评论 -
137. Single Number II
Description https://leetcode.com/problems/single-number-ii/ 题目大意: 给定一个数组,所有元素都出现三次,除了一个元素只出现一次,找出这个只出现一次的元素。要求时间复杂度O(n),空间复杂度O(1)。 Solving Ideas https://leetcode.com/problems/single-number-ii/discuss/...原创 2018-12-27 16:48:29 · 166 阅读 · 0 评论 -
136. Single Number
Description https://leetcode.com/problems/single-number/ 题目大意: 给定一个非空的整数数组,数组中除了一个数字外,其它数字都出现了两次,要求在线性时间、无额外辅助空间的条件下找出这个只出现了一次的数字。 Solving Ideas 位运算 0^a=a a^a=0 根据位运算的特点,可以知道,当我们对数组元素依次进行异或运算后,所有出现偶...原创 2018-12-27 14:52:48 · 133 阅读 · 0 评论 -
138. Copy List with Random Pointer
Description https://leetcode.com/problems/copy-list-with-random-pointer/ 题目大意:复制链表,链表结点有一个random指针指向任意一个链表结点或者为null。 Solving Ideas 参考剑指offer 复杂链表的复制 Solution /** * Definition for singly-linked list...原创 2018-12-26 23:29:31 · 143 阅读 · 0 评论 -
140. Word Break II
Description https://leetcode.com/problems/word-break-ii/ 题目大意:给定一个字符串s,和一个词典,找出s被分割成一个或多个字典单词的空格分隔的所有可能结果。 Solving Ideas DFS+动态规划 相比于分治法,动态规划对于相同的子问题只会求解一次,通过存储子问题的解能够减少重复求解相同子问题所花的时间。这里使用一个map来保存子串可...原创 2018-12-26 22:29:21 · 214 阅读 · 0 评论 -
139. Word Break
Description https://leetcode.com/problems/word-break/ 题目大意:给定一个字符串s,和一个词典,判断s是否可以被分割成一个或多个字典单词的空格分隔序列。 Solving Ideas 动态规划 代码中的dp[i]表示以索引为i的字符为结尾的子串是否符合要求,如果dp[i]=true,说明子串s.substring(0, i+1)可以被分割成一个或...原创 2018-12-25 19:52:32 · 111 阅读 · 0 评论 -
141. Linked List Cycle
Description ttps://leetcode.com/problems/linked-list-cycle/ 题目大意:判断单链表是否含有环 Solving Ideas 使用快慢两个指针判断链表是否含有环,如果两指针相遇即有环 Solution class Solution { public boolean hasCycle(ListNode head){ if (...原创 2018-11-23 23:17:23 · 119 阅读 · 0 评论 -
142. Linked List Cycle II
Description https://leetcode.com/problems/reorder-list/ 题目大意:找到单向链表中环的入口结点 Solving Ideas 使用快慢两个指针判断链表是否含有环 如果有环,则统计环中的结点数目 接着使指针p2从头开始,先走结点数目的步数,然后p1从头开始与p2一起前进直至p1==p2 更详细的说明请移步链表中环的入口结点 Solution c...原创 2018-11-23 22:33:19 · 162 阅读 · 0 评论 -
143. Reorder List
Description https://leetcode.com/problems/reorder-list/ Solving Ideas 使用快慢两个指针找到中间结点 接着从中间结点处拆分链表,反转中间结点之后的链表 最后合并两个链表 Solution class Solution { public void reorderList(ListNode head) { ...原创 2018-11-08 18:40:48 · 125 阅读 · 0 评论 -
144. Binary Tree Preorder Traversal
Description https://leetcode.com/problems/binary-tree-preorder-traversal/ Solving Ideas 前序遍历 Solution 递归 import java.util.*; class Solution { private void preorderCore(TreeNode root, List<Integ...原创 2018-11-08 15:29:25 · 137 阅读 · 0 评论 -
145. Binary Tree Postorder Traversal
Description https://leetcode.com/problems/binary-tree-postorder-traversal Solving Ideas 后序遍历 Solution 递归 import java.util.*; class Solution { private void travelCore(TreeNode root, List<Integer...原创 2018-11-08 15:10:44 · 126 阅读 · 0 评论 -
150. Evaluate Reverse Polish Notation
Description https://leetcode.com/problems/evaluate-reverse-polish-notation/ Solving Ideas 逆波兰表达式又称后缀表达式,是四则运算的表达方式。 中缀表达式:3 * (2 + 1) 前缀表达式:*3+21 后缀表达式:321+* Solution import java.util.LinkedList; c...原创 2018-11-02 19:28:18 · 123 阅读 · 0 评论 -
111. Minimum Depth of Binary Tree
Description https://leetcode.com/problems/minimum-depth-of-binary-tree/ Solving Ideas 递归 若为根结点为空,则返回0; 若左子树为空,则返回右子树的最小深度+1; 若右子树为空,则返回左子树的最小深度+1; 否则取左、右子树最小深度的较小值+1。 Solution class Solution { p...原创 2018-11-02 18:43:30 · 110 阅读 · 0 评论