Description
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
Given an integer n, return the number of distinct solutions to the n-queens puzzle.
Example:
1 | Input: 4 |
Thinking outside the box
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
Given an integer n, return the number of distinct solutions to the n-queens puzzle.
Example:
1 | Input: 4 |
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
Given an integer n, return all distinct solutions to the n-queens puzzle.
Each solution contains a distinct board configuration of the n-queens’ placement, where 'Q'
and '.'
both indicate a queen and an empty space respectively.
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:
1 | Input: intervals = [[1,3],[6,9]], newInterval = [2,5] |
Example 2:
1 | Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8] |
这里介绍两种常用的排序对象:对数组的排序和对list的排序。
数组的排序主要采用Arrays.sort()方法,默认为升序。常用API有:
1 | Arrays.sort(array_name,startindex,endindex); |
若想降序,可以先将数组转换为list:
1 | List tmp = Arrays.asList(a1); |
List排序主要采用Comparator类中的sort方法,主要API有:
1 | Collections.sort(Listname); |
可以看出两者的自定义排序均为重写compare方法。
Given a collection of intervals, merge all overlapping intervals.
Example 1:
1 | Input: [[1,3],[2,6],[8,10],[15,18]] |
Example 2:
1 | Input: [[1,4],[4,5]] |
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 is defined as a character sequence consists of non-space characters only.
Example:
1 | Input: "Hello World" |
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
Example 1:
1 | Input: |
Example 2:
1 | Input: |
Implement pow(x, n), which calculates x raised to the power n (xn).
Example 1:
1 | Input: 2.00000, 10 |
Example 2:
1 | Input: 2.10000, 3 |
Example 3:
1 | Input: 2.00000, -2 |
Note: