更多答案也可以关注我的GitHub算法仓库——Algorithm
(大家可以根据我每道题打的星来衡量这道题的难易,星越少越难)
所有代码都是在力扣上提交通过的,保证代码的正确无误
基础数据结构:
class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
92.反转链表II
思路是记录反转部分的前一个节点和后一个节点,将其反转后再连接,思路是这个,但是我在实现上可能麻烦一些:
class Solution {
public ListNode reverseBetween(ListNode head, int left, int right) {
ListNode curr = head;
ListNode prev = null;
ListNode next = curr;
int flag = 1;
while (flag < right) {
if (flag == left - 1) {
prev = curr;
}
curr = curr.next;
flag++;
}
next = curr.next;
if (prev == null) {
head = reverse(head, left, right);
prev = head;
}else {
prev.next = reverse(prev.next, left, right);
}
while ( prev.next != null && left <= right) {
prev = prev.next;
}
if (next != null) {
if (prev.next != null) {
prev.next.next = next;
}else {
prev.next = next;
}
}
return head;
}
public ListNode reverse(ListNode head, int left, int right) {
if (left == right) {
return head;
}
ListNode prev = null;
ListNode curr = head;
while (left <= right) {
ListNode nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
left++;
}
return prev;
}
}
200.岛屿数量
广搜、深搜都可以,这里是深搜+剪枝,不用回溯:
class Solution {
public int numIslands(char[][] grid) {
int res = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == '1') {
res++;
dfs(grid, i, j);
}
}
}
return res;
}
public void dfs(char[][] grid, int x, int y) {
grid[x][y] = '0';
if (x < grid.length - 1 && grid[x+1][y] == '1') {
dfs(grid, x+1, y);
}
if (x > 0 && grid[x-1][y] == '1') {
dfs(grid, x-1, y);
}
if (y < grid[0].length - 1 && grid[x][y+1] == '1') {
dfs(grid, x, y+1);
}
if (y > 0 && grid[x][y-1] == '1') {
dfs(grid, x, y-1);
}
}
}
33.搜索旋转排序数组
二分(我当时是懒省事直接O(n)遍历了)
class Solution {
public int search(int[] nums, int target) {
int n = nums.length;
if (n == 0) {
return -1;
}
if (n == 1) {
return nums[0] == target ? 0 : -1;
}
int l = 0, r = n - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (nums[mid] == target) {
return mid;
}
if (nums[0] <= nums[mid]) {
if (nums[0] <= target && target < nums[mid]) {
r = mid - 1;
} else {
l = mid + 1;
}
} else {
if (nums[mid] < target && target <= nums[n - 1]) {
l = mid + 1;
} else {
r = mid - 1;
}
}
}
return -1;
}
}
46.全排列
经典算法,使用dfs+交换+回溯,可以当模板套用:
class Solution {
private List<List<Integer>> list = new ArrayList<>();
public List<List<Integer>> permute(int[] nums) {
dfs(nums, 0, nums.length-1);
return list;
}
public void dfs(int[] array,int start,int end) {
if(start==end) {
List<Integer> li = new ArrayList<>();
for (int i = 0; i < array.length; i++) {
li.add(array[i]);
}
list.add(li);
} else {
for (int i = start; i <= end; i++) {
swap(array,start,i);
dfs(array,start+1,end);
swap(array,start,i);
}
}
}
public void swap(int[] array,int i,int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
142.环形链表 II
当快慢指针相遇之后,使用第三个指针从头开始和慢指针一起向后移动(步伐都是一步),当第三个指针与慢指针相遇时,即为环的入口:
public ListNode detectCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
ListNode res = head;
while (fast != null && slow != null) {
fast = fast.next;
if (fast == null) {
break;
}
fast = fast.next;
slow = slow.next;
if (slow == fast) {
while (slow != res) {
slow = slow.next;
res = res.next;
}
return res;
}
}
return null;
}
23.合并K个排序链表
结合前面那道合并两个链表,我们只需循环调用该方法即可:
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if (lists.length == 0) {
return null;
}
if (lists.length == 1) {
return lists[0];
}
ListNode res = lists[0];
for (int i = 0; i < lists.length-1; i++) {
res = mergeTwoLists(res, lists[i+1]);
}
return res;
}
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) {
return l2;
}
if (l2 == null) {
return l1;
}
if (l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
}else {
l2.next = mergeTwoLists(l2.next, l1);
return l2;
}
}
}
54.螺旋矩阵
模拟打印顺序:
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> list = new ArrayList<>();
int left = 0 ,up = 0;
int right = matrix[0].length - 1;
int down = matrix.length - 1;
int size = matrix.length * matrix[0].length;
while (list.size() < size) {
for (int i = left; i <= right; i++) {
if (list.size() < size) list.add(matrix[up][i]);
}
up++;
for (int i = up; i <= down; i++) {
if (list.size() < size) list.add(matrix[i][right]);
}
right--;
for (int i = right; i >= left; i--) {
if (list.size() < size) list.add(matrix[down][i]);
}
down--;
for (int i = down; i >= up; i--) {
if (list.size() < size) list.add(matrix[i][left]);
}
left++;
}
return list;
}
704.二分查找
如题,就是考二分
public int search(int[] nums, int target) {
int l = 0, r = nums.length-1, m;
while (l <= r) {
m = (r + l)/2;
if (target == nums[m]) {
return m;
}
if (target > nums[m]) {
l = m + 1;
}else {
r = m - 1;
}
}
return -1;
}
232.用栈实现队列
两个栈实现,需要弹出时,将栈s1全部倒入s2,再从s2弹
class MyQueue {
private Stack<Integer> s1 = new Stack<>();
private Stack<Integer> s2 = new Stack<>();
private int front;
public MyQueue() {}
public void push(int x) {
if (s1.empty()) {
front = x;
}
s1.push(x);
}
public int pop() {
if (s2.empty()) {
while(!s1.empty()) {
s2.push(s1.pop());
}
}
return s2.pop();
}
public int peek() {
if (s2.empty()) {
return front;
}else {
return s2.peek();
}
}
public boolean empty() {
return s1.empty() && s2.empty();
}
}
42.接雨水
动规:从左向右,记录最大高度;再从右向左记录最大高度;最后每格的积水为两个数组中较小的那个再减去这个位置的高度:
public int trap(int[] height) {
int res = 0;
int len = height.length;
int[] left = new int[len];
int[] right = new int[len];
left[0] = height[0];
for (int i = 1; i < len; i++) {
left[i] = Math.max(left[i-1], height[i]);
}
right[len-1] = height[len-1];
for (int i = len - 2; i >= 0; i--) {
right[i] = Math.max(height[i], right[i+1]);
}
for (int i = 0; i < len; i++) {
res += Math.min(left[i], right[i]) - height[i];
}
return res;
}
94.二叉树的中序遍历
递归
class Solution {
List<Integer> list = new ArrayList<>();
public List<Integer> inorderTraversal(TreeNode root) {
dfs(root);
return list;
}
public void dfs (TreeNode root) {
if (root != null) {
dfs(root.left);
list.add(root.val);
dfs(root.right);
}
}
}
300.最长上升子序列
经典动规:(现在看看差不多又忘记怎么做了)
public int lengthOfLIS(int[] nums) {
if (nums.length == 0) {
return 0;
}
int[] dp = new int[nums.length];
dp[0] = 1;
int res = 1;
for (int i = 1; i < nums.length; i++) {
dp[i] = 1;
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
res = Math.max(res, dp[i]);
}
return res;
}
143.重排链表
分三步:找中点——>翻转链表——>合并链表
class Solution {
public void reorderList(ListNode head) {
if (head == null) {
return;
}
ListNode mid = middleList(head);
ListNode l2 = reverseList(mid);
mergeList(head, l2);
}
public ListNode middleList(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
public ListNode reverseList(ListNode head) {
ListNode prev = null, next;
while (head != null) {
next = head.next;
head.next = prev;
prev = head;
head = next;
}
return prev;
}
public void mergeList(ListNode l1, ListNode l2) {
ListNode l1_t, l2_t;
while (l1 != null && l2 != null) {
l1_t = l1.next;
l1.next = l2;
l1 = l1_t;
l2_t = l2.next;
l2.next = l1;
l2 = l2_t;
}
}
}
199.二叉树的右视图
向右深搜,技巧点:树的深度==集合大小,所以记录每层的深度,当depth == res.size()
时为第一当这一层的最右节点:
class Solution {
List<Integer> res = new ArrayList<>();
public List<Integer> rightSideView(TreeNode root) {
dfs(root, 0);
return res;
}
public void dfs (TreeNode root, int depth) {
if (root != null) {
if (depth == res.size()) {
res.add(root.val);
}
dfs(root.right, depth+1);
dfs(root.left, depth+1);
}
}
}
70.爬楼梯
就是斐波那契数列,滚动数组解决:
public int climbStairs(int n) {
if (n == 1) {
return 1;
}
int[] dp = new int[3];
dp[0] = 1;
dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[2] = dp[0] + dp[1];
dp[0] = dp[1];
dp[1] = dp[2];
}
return dp[2];
}
22.链表中倒数第k个节点
两个指针,一个从起点,另一个从正数第k个移动:
public ListNode getKthFromEnd(ListNode head, int k) {
ListNode fast = head;
ListNode slow = head;
while (k-- > 0) {
fast = fast.next;
}
while (fast != null) {
fast = fast.next;
slow = slow.next;
}
return slow;
}
124.二叉树中的最大路径和
思路是递归,递归去找左右最大的路径和,个人觉得比较难,需重复做:
class Solution {
int res = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
dfs(root);
return res;
}
public int dfs(TreeNode node) {
if (node == null) {
return 0;
}
int left = Math.max(dfs(node.left), 0);
int right = Math.max(dfs(node.right), 0);
res = Math.max(res, node.val + left + right);
return node.val + Math.max(left, right);
}
}
69.x 的平方根
二分:
public int mySqrt(int x) {
int res = -1, l = 0, r = x,m;
while (l <= r) {
m = l + (r - l)/2;
if ((long)m*m <= x) {
l = m + 1;
res = m;
}else {
r = m - 1;
}
}
return res;
}
56.合并区间
根据左端点排列后开始有序合并:
public int[][] merge(int[][] intervals) {
List<int[]> res = new ArrayList<>();
if (intervals == null || intervals.length == 0) {
return res.toArray(new int[0][]);
}
if (intervals.length == 1) {
return intervals;
}
Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
for (int i = 0; i < intervals.length-1;i++) {
int left = intervals[i][0];
int right = intervals[i][1];
while (right >= intervals[i+1][1] || right >= intervals[i+1][0]) {
if (right < intervals[i+1][1]) {
right = intervals[i+1][1];
}
i++;
if (i == intervals.length - 1) {
break;
}
}
int[] temp = new int[]{left, right};
res.add(temp);
}
if (res.get(res.size() - 1)[1] < intervals[intervals.length -1][1]) {
res.add(intervals[intervals.length -1]);
}
return res.toArray(new int[0][]);
}
19.删除链表的倒数第 N 个结点
我的写法可能麻烦些,主要是一些边界值的判断,思路和前面寻找倒数第N个节点思路一样,只不过是找N+1个,再做删除操作即可:
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode prev = findprev(head, n+1);
if (prev == null) {
if (head.next != null) {
return head.next;
}
return null;
}
if (prev.next != null) {
prev.next = prev.next.next;
}
return head;
}
public ListNode findprev (ListNode head, int k) {
ListNode fast = head;
ListNode slow = head;
while (k-- > 0) {
if (fast == null) {
return null;
}
fast = fast.next;
}
while (fast != null) {
fast = fast.next;
slow = slow.next;
}
return slow;
}
}
1 条评论
你的文章内容非常卖力,让人点赞。http://www.hntdqy.com