0%

Description

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.

Note: You can only move either down or right at any point in time.

Example:

1
2
3
4
5
6
7
8
Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum.

Difficulty: Medium

Code:

1
2
3
4
5
class Solution {
public int minPathSum(int[][] grid) {

}
}

题意

给定m x n的网格,填充的都是非负数,从左上角到右下角找出一条线路,使路径上数字之和最小,并返回这个最小和的值。

每次只能向下或者向右移动。

阅读全文 »

Description

A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

Note: m and n will be at most 100.

Example 1:

1
2
3
4
5
6
7
8
9
10
11
12
Input:
[
[0,0,0],
[0,1,0],
[0,0,0]
]
Output: 2
Explanation:
There is one obstacle in the middle of the 3x3 grid above.
There are two ways to reach the bottom-right corner:
1. Right -> Right -> Down -> Down
2. Down -> Down -> Right -> Right

Difficulty: Medium

Code:

1
2
3
4
5
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {

}
}

题意

求机器人在长为m宽为n到格子中,从左上角走到右下角所有不重复路线的个数,每次只能向下或者向右移动。

考虑到在格子中添加一些障碍物,障碍物用1表示,空格子用0表示。

阅读全文 »

Description

A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).

How many possible unique paths are there?

Above is a 7 x 3 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

Example 1:

1
2
3
4
5
6
7
Input: m = 3, n = 2
Output: 3
Explanation:
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Right -> Down
2. Right -> Down -> Right
3. Down -> Right -> Right

Example 2:

1
2
Input: m = 7, n = 3
Output: 28

Difficulty: Medium

Code:

1
2
3
4
5
class Solution {
public int uniquePaths(int m, int n) {

}
}

题意

求机器人在长为m宽为n到格子中,从左上角走到右下角所有不重复路线的个数,每次只能向下或者向右移动。

阅读全文 »

Description

Given a linked list, rotate the list to the right by k places, where k is non-negative.

Example 1:

1
2
3
4
5
Input: 1->2->3->4->5->NULL, k = 2
Output: 4->5->1->2->3->NULL
Explanation:
rotate 1 steps to the right: 5->1->2->3->4->NULL
rotate 2 steps to the right: 4->5->1->2->3->NULL

Example 2:

1
2
3
4
5
6
7
Input: 0->1->2->NULL, k = 4
Output: 2->0->1->NULL
Explanation:
rotate 1 steps to the right: 2->0->1->NULL
rotate 2 steps to the right: 1->2->0->NULL
rotate 3 steps to the right: 0->1->2->NULL
rotate 4 steps to the right: 2->0->1->NULL

Difficulty: Medium

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode rotateRight(ListNode head, int k) {

}
}

题意

对给定链表,将最后k个移动到最前面。

阅读全文 »

这是一篇走心的填坑笔记,自学Java的几年总是在不断学习新的技术,一路走来发现自己踩坑无数,而填上的坑却屈指可数。突然发现,有时候真的不是几年工作经验的问题,有些东西即使工作十年,没有用心去学习过也不过是一个10年大坑罢了(真实感受)。

刚开始接触多线程时,就知道有等待/唤醒这个东西,写过一个demo就再也没有看过了,至于它到底是个什么东西,或者说它能解决什么样的问题,估计大多数人和我一样都是模棱两可。这次笔者就尝试带你搞懂等待/唤醒机制,读完本文你将get到以下几点:

  1. 循环等待带来什么样的问题
  2. 用等待唤醒机制优化循环等待
  3. 等待唤醒机制中的被忽略的细节
阅读全文 »