Ruizhi Ma's Blog

Keep Calm and Carry On

Problem 22

Generate Parenteses

问题描述 https://leetcode.com/problems/generate-parentheses/ 解决思路(2019-12-22) 递归回溯 代码 class Solution { public List<String> generateParenthesis(int n) { List<String> res = new ...

Problem 21

Merge Two Sorted Lists

问题描述 https://leetcode.com/problems/merge-two-sorted-lists/ 解决思路(2019-12-21) compare every value of every node in l1 and l2 if l1.val less than l2.val, then add l1 into the new list or add l2 ...

Problem 20

Valid Parentheses

问题描述 https://leetcode.com/problems/valid-parentheses/ 解决思路(2019-12-07) use stack to store all left parentheses if encounter right parenthesis, pop the parenthese in stack if poped parenthsi...

Problem 19

Remove Nth Node From End of List

问题描述 https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 解决思路(2019-12-06) set a dummy node set slow node and fast node, and they interval is n move slow and fast node simultaneo...

Problem 18

4Sum

问题描述 https://leetcode.com/problems/4sum/ 解决思路 基本和3Sum一致 代码 class Solution { public List<List<Integer>> fourSum(int[] nums, int target) { Arrays.sort(nums); ...

Problem 17

Letter Combination of a Phone Number

问题描述 https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 解决思路 use hashMap to store numbers and corresponding charaters recursive to add charaters into the String curr while...

Problem 16

问题描述 https://leetcode.com/problems/3sum-closest/ 解决思路 和15题很像! sort array use base to represent the first number, left points the second and right points the right calculate the sum of them,...

Problem 15

问题描述 https://leetcode.com/problems/3sum/ 解决思路 sort the nums get a number in nums, let’s say a, and expect the sum of two other numbersis -a set left pointer and right pointer in the rest of...

Problem 14

longest Common Prefix

问题描述 https://leetcode.com/problems/longest-common-prefix/ 解决思路 将第一个字符串整体拿来和剩下的所有字符串比较,如果不等,则第一个字符串从后往前减少一位;若相等,则返回字符。 问题解惑 indexOf的用法:在这个地方,strs[i].indexOf(prefix)的意思是,从头开始查找是否包含prefix的字符串。 当然ind...

Problem 13

Roman to Integer

问题描述 https://leetcode.com/problems/roman-to-integer/ 解决思路(2019-07-03) 仔细分析罗马数字的技术方法,可以发现,如果不需要动用减法,则从右向左,依次变大;如果需要动用减法,则右边的会小于左边的。 用HashMap将罗马数字和阿拉伯数字建立键值关系 从后向前遍历给定字符串,如果前一位num大于后一位last,则sum...