题目要求

1
2
3
4
5
6
7
8
9
10
11
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

For example,
Given the following matrix:

[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return [1,2,3,6,9,8,7,4,5].

按照顺时针方向旋转访问数组中的元素

阅读全文 »

题目要求

1
2
3
4
5
6
7
8
9
10
11
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.

For example,
Given n = 3,

You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]

也就是将递加的数字按照顺时针的顺序依次填入数组之中

这道题目联系到Spiral Matrix I,其实就相当好解决了。Spiral Matrix I可以参考我的这篇博客

阅读全文 »

题目要求

1
2
3
4
5
6
7
8
9
10
11
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).

You may assume that the intervals were initially sorted according to their start times.

Example 1:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].

Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].

This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].

给定一组顺序排列且相互之间没有重叠的区间,输入一个区间,将它插入到当前的区间数组中,并且将需要合并的区间合并,之后返回插入并且合并后的区间。

阅读全文 »

题目要求

1
2
3
4
5
6
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?

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

Note: m and n will be at most 100.

一个机器人站在下标为(1,1)的起点走向下标为(m,n)的终点。机器人只允许往下走和往右走。问一共有多少条独特的路线?

阅读全文 »

题目要求

1
2
3
4
5
6
7
8
9
10
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2.

Note:

1. The length of both num1 and num2 is < 110.
2. Both num1 and num2 contains only digits 0-9.
3. Both num1 and num2 does not contain any
leading zero.
4. You must not use any built-in BigInteger library or
convert the inputs to integer directly.

将两个String形式的数字相乘的结果用String的形式返回。不准使用Int(java)以外的形式来记录数字。

阅读全文 »

题目要求

1
2
3
4
5
6
7
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

Write a function to determine if a given target is in the array.

The array may contain duplicates.

相比于I,II中添加了数组中可能存在重复值的条件。大家可以先参考一下我的这篇关于I的博客,在这个基础上,考虑如何实现这道题目。

阅读全文 »

题目要求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Follow up for "Unique Paths":

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.

For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.

[
[0,0,0],
[0,1,0],
[0,0,0]
]
The total number of unique paths is 2.

Note: m and n will be at most 100.

这是Unique Path题目系列。关于Unique Path I请参考我的这篇博客。相比于I,这里添加的需求是说,某些节点上存在路障。存在路障的节点会在数组中被标记为1。请问从起点到终点有多少条独立路径。

阅读全文 »

题目要求

1
2
3
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.

计算从左上角到右下角的最短路径长度。只能向下或者向右移动。
类似的题目请参考我的博客Unique Path IUnique Path II

阅读全文 »

题目要求

1
2
3
4
5
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.

将链表中重复的元素全部删除,返回新的头结点。

相比于Remove Duplicates from Sorted List I,这里将重复的元素全部删除。
想要了解Remove Duplicates from Sorted List I,请参考我的这篇博客

阅读全文 »

题目要求

The set [1,2,3,…,n] contains a total of n! unique permutations.

By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):

1."123"
2."132"
3."213"
4."231"
5."312"
6."321"
Given n and k, return the kth permutation sequence.

Note: Given n will be between 1 and 9 inclusive.

假设按照题中给的排列组合的顺序,假设有1~n个数字,返回第k个排列组合的结果。

阅读全文 »
0%