Ruizhi Ma's Blog

Keep Calm and Carry On

Problem 86

Partition List

问题描述 https://leetcode.com/problems/partition-list/ 代码 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; }...

Problem 83

Remove Duplicates from Sorted List

问题描述 https://leetcode.com/problems/remove-duplicates-from-sorted-list/ 解决思路 如果当前节点的值和下一个节点的值相等,就将下个节点指向下下个节点;否则将当前节点指向下个节点。 代码 /** * Definition for singly-linked list. * public class ListNode {...

Problem 82

Rmove Duplicate from Sorted Array II

问题描述 https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 解决思路 不断的比较当前节点与前一个节点和下一个节点的值,若相等,则往后移动;不想等,设为realNode 代码 /** * Definition for singly-linked list. * public class ListNo...

Problem 61

Rotate List

问题描述 https://leetcode.com/problems/rotate-list/ 解决思路 用双指针fast和slow指向head,先用fast算出listNode的长度len,然后先移动fast到k%len的位置,然后将slow和fast同时向后移动k%len。最后此时slow指向的应该是新listNode的尾节点(除去null先不谈),slow.next应该是新节点的头节...

Problem 60

Permutation Sequence

问题描述 https://leetcode.com/problems/permutation-sequence/ 解决思路一(Time Out) 先用递归,回溯,讲全排列集合写出来,然后再找到对应的第几个,转为String类型。 问题解惑 如何将List转为String? 首先用迭代器,将List中的每一个元素拿出来,再将他们通过append,加到StringBuilder类型str...

Problem 56

Merge Intervals

问题描述 https://leetcode.com/problems/merge-intervals/ 解决思路 先将所给的每个数组,按照首元素进行排序,然后分情况讨论: 上一个数组首元素等于当前数组首元素的时候,若上一个数组的尾元素小于当前数组尾元素,则将当前数组尾元素,设置为新数组的尾元素。 上一个数组首元素小于当前数组首元素的时候,分下面的情况: 2.1 上一个数组的尾元素...

Problem 55

Jump Game

问题描述 https://leetcode.com/problems/jump-game/submissions/ 解决思路 举个例子:[2,3,1,4,3] 一开始在2的位置,此时既可以跳到3和1,然后在3的地方可以跳到1,4,3.此时已经到队尾,返回true.解题核心思路就是用reach记录下能到达的最远距离(每次迭代的时候,在上一次的reach和当前nums[i]+i中取得最大值),...

Problem 59

Spiral Matrix II

问题描述 https://leetcode.com/problems/spiral-matrix-ii/ 解决思路 这题和54基本差不多,思路一样的,需要单独处理一下中间会剩下一个的情况。 代码 class Solution { public int[][] generateMatrix(int n) { int[][] res = new int[n][n]; ...

Problem 58

Length of Last Word

问题描述 Given a string s consists of upper/lower-case alphabets and empty space characters ‘ ‘, return the length of last word in the string. If the last word does not exist, return 0. Note: A word ...

Problem 54

Spiral Matrix

问题描述 https://leetcode.com/problems/spiral-matrix/ 解决思路 设置四个角,然后不断地以螺旋状将数组加入res结果集。最后需要单独处理一下最后剩下一行或者一列的情况。 代码 class Solution { public List<Integer> spiralOrder(int[][] matrix) { ...