0%

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.

pic

Given an integer n, return the number of distinct solutions to the n-queens puzzle.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Input: 4
Output: 2
Explanation: There are two distinct solutions to the 4-queens puzzle as shown below.
[
[".Q..", // Solution 1
"...Q",
"Q...",
"..Q."],

["..Q.", // Solution 2
"Q...",
"...Q",
".Q.."]
]

Difficulty: Hard

Code:

1
2
3
4
5
class Solution {
public int totalNQueens(int n) {

}
}

题意

N皇后问题,将n个皇后放在nxn的棋盘上,保证任意两个皇后不能相互攻击。

给定正整数n,求不重复的N皇后问题答案的个数。

阅读全文 »

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.

pic

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.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
Input: 4
Output: [
[".Q..", // Solution 1
"...Q",
"Q...",
"..Q."],

["..Q.", // Solution 2
"Q...",
"...Q",
".Q.."]
]
Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.

Difficulty: Hard

Code:

1
2
3
4
5
class Solution {
public List<List<String>> solveNQueens(int n) {

}
}

题意

N皇后问题,将n个皇后放在nxn的棋盘上,保证任意两个皇后不能相互攻击。

国际象棋中,皇后不仅能横竖走,还能走两个斜线。诞生八皇后问题,在一个8x8的棋盘上才能放8个皇后,保证不能相互攻击,即不能处于同一横线、竖线、斜线上。

给定正整数n,求不重复的N皇后问题答案。’Q’,’.’分别表示棋盘上的皇后和空格。

阅读全文 »

Description

Implement pow(x, n), which calculates x raised to the power n (xn).

Example 1:

1
2
Input: 2.00000, 10
Output: 1024.00000

Example 2:

1
2
Input: 2.10000, 3
Output: 9.26100

Example 3:

1
2
3
Input: 2.00000, -2
Output: 0.25000
Explanation: 2-2 = 1/22 = 1/4 = 0.25

Note:

  • -100.0 < x < 100.0
  • n is a 32-bit signed integer, within the range [−231, 231 − 1]

Difficulty: Medium

Code:

1
2
3
4
5
class Solution {
public double myPow(double x, int n) {

}
}

题意

求x的n次方,x范围是(-100.0, 100.0),n是32位有符号整数其范围是[−231, 231 − 1]

阅读全文 »

Description

Given an array of strings, group anagrams together.

Example:

1
2
3
4
5
6
7
Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]

Note:

  • All inputs will be in lowercase.
  • The order of your output does not matter.

Difficulty: Medium

Code:

1
2
3
4
5
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {

}
}

题意

给定一组字符串,按字谜(颠倒字母而成的字)分组。所有输入为小写,结果顺序不要求。

阅读全文 »

Description

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Note:

You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

Example 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
Given input matrix = 
[
[1,2,3],
[4,5,6],
[7,8,9]
],

rotate the input matrix in-place such that it becomes:
[
[7,4,1],
[8,5,2],
[9,6,3]
]

Example 2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Given input matrix =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
],

rotate the input matrix in-place such that it becomes:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]

Difficulty: Medium

Code:

1
2
3
4
5
class Solution {
public void rotate(int[][] matrix) {

}
}

题意

给定一个n x n的二维矩阵表示一张图片,将图片顺时针旋转90度。要求直接修改二维矩阵,不允许使用额外的二维矩阵去做旋转。

阅读全文 »