Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit afc0520

Browse files
committed
question 51,52,53,54,55
1 parent 5eb9f47 commit afc0520

File tree

6 files changed

+454
-0
lines changed

6 files changed

+454
-0
lines changed

README.md

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,4 +2035,242 @@ public class Solution {
20352035
//return Math.pow(x, n);
20362036
}
20372037
}
2038+
```
2039+
2040+
## 51. N-Queens
2041+
题目:经典的N皇后问题,给出一个NxN的棋盘,求所有皇后排列的组合。
2042+
!(8-queens)[https://leetcode.com/static/images/problemset/8-queens.png]
2043+
要保证皇后不能互相攻击,就必须要求任意两个皇后不在同一行,同一列和同一对角线上。
2044+
2045+
```
2046+
For example,
2047+
There exist two distinct solutions to the 4-queens puzzle:
2048+
[
2049+
[".Q..", // Solution 1
2050+
"...Q",
2051+
"Q...",
2052+
"..Q."],
2053+
2054+
["..Q.", // Solution 2
2055+
"Q...",
2056+
"...Q",
2057+
".Q.."]
2058+
]
2059+
```
2060+
2061+
一个NP问题,开一个n大的数组,下标表示行值表示列。每加入一个值时先与之前放入数组的值进行判断是否符合规则,符合规则继续下一个。
2062+
2063+
```java
2064+
public class Solution {
2065+
private List<List<String>> resultList = new ArrayList<>();
2066+
private int[] positions;
2067+
private char[] tags;
2068+
2069+
public List<List<String>> solveNQueens(int n) {
2070+
positions = new int[n];
2071+
tags = new char[n];
2072+
Arrays.fill(positions, -1);
2073+
Arrays.fill(tags, '.');
2074+
dp(0, n);
2075+
return resultList;
2076+
}
2077+
2078+
public void dp(int n, int max) {
2079+
if (n == max) {
2080+
List<String> qLine = new ArrayList<>();
2081+
for (int i = 0; i < positions.length; i++) {
2082+
tags[positions[i]] = 'Q';
2083+
String q = new String(tags);
2084+
qLine.add(q);
2085+
tags[positions[i]] = '.';
2086+
}
2087+
resultList.add(qLine);
2088+
return;
2089+
}
2090+
int valueX = n;
2091+
int valueY = 0;
2092+
while (valueY < max) {
2093+
if (hasPlaced(n, valueX, valueY)) {
2094+
positions[n] = valueY;
2095+
dp(n + 1, max);
2096+
}
2097+
valueY++;
2098+
}
2099+
}
2100+
2101+
public boolean hasPlaced(int n, int valueX, int valueY) {
2102+
for (int j = 0; j < n; j++) {
2103+
int x = j;
2104+
int y = positions[j];
2105+
if (positions[j] == valueY || Math.abs(valueX - x) == Math.abs(valueY - y)) {
2106+
return false;
2107+
}
2108+
}
2109+
return true;
2110+
}
2111+
}
2112+
```
2113+
2114+
## 52. N-Queens II
2115+
与上一题一样的题目,不过是求组合的个数而不需要将组合罗列出来
2116+
```java
2117+
public class Solution {
2118+
private int count = 0;
2119+
private int[] positions;
2120+
2121+
public int totalNQueens(int n) {
2122+
positions = new int[n];
2123+
Arrays.fill(positions, -1);
2124+
dp(0, n);
2125+
return count;
2126+
}
2127+
2128+
public void dp(int n, int max) {
2129+
if (n == max) {
2130+
count++;
2131+
return;
2132+
}
2133+
int valueX = n;
2134+
int valueY = 0;
2135+
while (valueY < max) {
2136+
if (hasPlaced(n, valueX, valueY)) {
2137+
positions[n] = valueY;
2138+
dp(n + 1, max);
2139+
}
2140+
valueY++;
2141+
}
2142+
}
2143+
2144+
public boolean hasPlaced(int n, int valueX, int valueY) {
2145+
for (int j = 0; j < n; j++) {
2146+
int x = j;
2147+
int y = positions[j];
2148+
if (positions[j] == valueY || Math.abs(valueX - x) == Math.abs(valueY - y)) {
2149+
return false;
2150+
}
2151+
}
2152+
return true;
2153+
}
2154+
}
2155+
```
2156+
2157+
## 53. Maximum Subarray
2158+
题目:给出一个数组,求一个和最大的子数组。
2159+
```
2160+
For example, given the array [-2,1,-3,4,-1,2,1,-5,4],
2161+
the contiguous subarray [4,-1,2,1] has the largest sum = 6.
2162+
```
2163+
需要一个额外的数组来维护每个位置的最大值,以便进行下一个位置最大值的计算。
2164+
2165+
```java
2166+
public class Solution {
2167+
public int maxSubArray(int[] nums) {
2168+
int[] total = new int[nums.length];
2169+
total[0] = nums[0];
2170+
int totalMax = total[0];
2171+
for (int i = 1; i < nums.length; i++) {
2172+
total[i] = Math.max(nums[i], nums[i] + total[i - 1]);
2173+
totalMax = Math.max(totalMax, total[i]);
2174+
}
2175+
return totalMax;
2176+
}
2177+
}
2178+
```
2179+
2180+
## 54. Spiral Matrix
2181+
题目:给出一组m x n 的二维数字数组,返回螺旋顺序的数字列表。
2182+
```
2183+
Given the following matrix:
2184+
2185+
[
2186+
[ 1, 2, 3 ],
2187+
[ 4, 5, 6 ],
2188+
[ 7, 8, 9 ]
2189+
]
2190+
2191+
You should return [1,2,3,6,9,8,7,4,5].
2192+
```
2193+
将螺旋的顺序分解开来,可以分成4种顺序,依次为从左向右,从上向下,从左向右,从下向上。定义4个变量分别为`m-剩下的行数`,`n-剩下的列数`,`startM-行开始的坐标`,`startN-列开始的地方`。
2194+
接下来就是按顺序求列表了。
2195+
```java
2196+
public class Solution {
2197+
public List<Integer> spiralOrder(int[][] matrix) {
2198+
List<Integer> s = new ArrayList<>();
2199+
int m = matrix.length;
2200+
if(m == 0) return s;
2201+
int n = matrix[0].length;
2202+
int startM = 0, startN = 0;
2203+
while (m > 0 && n > 0) {
2204+
spiral1(startM, startN, m, n, matrix, s);
2205+
startM++;
2206+
m--;
2207+
if (m <= 0 || n <= 0) return s;
2208+
spiral2(startM, startN, m, n, matrix, s);
2209+
n--;
2210+
if (m <= 0 || n <= 0) return s;
2211+
spiral3(startM, startN, m, n, matrix, s);
2212+
m--;
2213+
if (m <= 0 || n <= 0) return s;
2214+
spiral4(startM, startN, m, n, matrix, s);
2215+
startN++;
2216+
n--;
2217+
}
2218+
return s;
2219+
}
2220+
2221+
public void spiral1(int startM, int startN, int m, int n, int[][] matrix, List<Integer> s) {
2222+
for (int k = startN; k < n + startN; k++) {
2223+
s.add(matrix[startM][k]);
2224+
}
2225+
}
2226+
2227+
public void spiral2(int startM, int startN, int m, int n, int[][] matrix, List<Integer> s) {
2228+
for (int k = startM; k < m + startM; k++) {
2229+
s.add(matrix[k][n + startN - 1]);
2230+
}
2231+
}
2232+
2233+
public void spiral3(int startM, int startN, int m, int n, int[][] matrix, List<Integer> s) {
2234+
for (int k = startN + n - 1; k >= startN; k--) {
2235+
s.add(matrix[startM + m - 1][k]);
2236+
}
2237+
}
2238+
2239+
public void spiral4(int startM, int startN, int m, int n, int[][] matrix, List<Integer> s) {
2240+
for (int k = m + startM - 1; k >= startM; k--) {
2241+
s.add(matrix[k][startN]);
2242+
}
2243+
}
2244+
}
2245+
```
2246+
2247+
## 55. Jump Game
2248+
题目:跳棋,给出一组数字,每个数字代表当前可以跳过的最大距离,求是否能够从第一个位置达到最后一个位置。
2249+
```
2250+
For example:
2251+
A = [2,3,1,1,4], return true.
2252+
2253+
A = [3,2,1,0,4], return false.
2254+
```
2255+
要先记录每个位置能够跳到的最远距离,特殊处理当数值为0时能够跳过去。
2256+
```java
2257+
public class Solution {
2258+
public boolean canJump(int[] nums) {
2259+
if (nums.length <= 1) return true;
2260+
if (nums[0] == 0) return false;
2261+
int[] maxs = new int[nums.length];
2262+
maxs[0] = nums[0];
2263+
for (int i = 1; i < nums.length - 1; i++) {
2264+
if (nums[i] == 0) {
2265+
if (maxs[i - 1] <= 1) {
2266+
return false;
2267+
}
2268+
maxs[i] = maxs[i - 1] - 1;
2269+
}
2270+
maxs[i] = Math.max(maxs[i - 1] - 1, nums[i]);
2271+
}
2272+
return true;
2273+
}
2274+
2275+
}
20382276
```

src/JumpGame.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Created by cpacm on 2017/6/26.
3+
*/
4+
public class JumpGame {
5+
public static void main(String[] args) {
6+
System.out.println(canJump(new int[]{0, 1}));
7+
}
8+
9+
public static boolean canJump(int[] nums) {
10+
if (nums.length <= 1) return true;
11+
if (nums[0] == 0) return false;
12+
int[] maxs = new int[nums.length];
13+
maxs[0] = nums[0];
14+
for (int i = 1; i < nums.length; i++) {
15+
if (nums[i] == 0) {
16+
if (maxs[i - 1] <= 1) {
17+
return i >= nums.length - 1;
18+
}
19+
maxs[i] = maxs[i - 1] - 1;
20+
}
21+
maxs[i] = Math.max(maxs[i - 1] - 1, nums[i]);
22+
}
23+
return true;
24+
}
25+
}

src/MaxSubArray.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.Arrays;
2+
3+
/**
4+
* Created by cpacm on 2017/6/26.
5+
*/
6+
public class MaxSubArray {
7+
8+
public static void main(String[] args) {
9+
System.out.println(maxSubArray(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4}));
10+
}
11+
12+
public static int maxSubArray(int[] nums) {
13+
int[] total = new int[nums.length];
14+
total[0] = nums[0];
15+
int totalMax = total[0];
16+
for (int i = 1; i < nums.length; i++) {
17+
total[i] = Math.max(nums[i], nums[i] + total[i - 1]);
18+
totalMax = Math.max(totalMax, total[i]);
19+
}
20+
return totalMax;
21+
22+
}
23+
}

src/NQueens.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.List;
4+
5+
/**
6+
* Created by cpacm on 2017/6/24.
7+
*/
8+
public class NQueens {
9+
10+
private static List<List<String>> resultList = new ArrayList<>();
11+
private static int[] positions;
12+
private static char[] tags;
13+
14+
public static void main(String[] args) {
15+
System.out.println(solveNQueens(8));
16+
}
17+
18+
public static List<List<String>> solveNQueens(int n) {
19+
positions = new int[n];
20+
tags = new char[n];
21+
Arrays.fill(positions, -1);
22+
Arrays.fill(tags, '.');
23+
dp(0, n);
24+
return resultList;
25+
}
26+
27+
public static void dp(int n, int max) {
28+
if (n == max) {
29+
List<String> qLine = new ArrayList<>();
30+
for (int i = 0; i < positions.length; i++) {
31+
tags[positions[i]] = 'Q';
32+
String q = new String(tags);
33+
qLine.add(q);
34+
tags[positions[i]] = '.';
35+
}
36+
resultList.add(qLine);
37+
return;
38+
}
39+
int valueX = n;
40+
int valueY = 0;
41+
while (valueY < max) {
42+
if (hasPlaced(n, valueX, valueY)) {
43+
positions[n] = valueY;
44+
dp(n + 1, max);
45+
}
46+
valueY++;
47+
}
48+
}
49+
50+
public static boolean hasPlaced(int n, int valueX, int valueY) {
51+
for (int j = 0; j < n; j++) {
52+
int x = j;
53+
int y = positions[j];
54+
if (positions[j] == valueY || Math.abs(valueX - x) == Math.abs(valueY - y)) {
55+
return false;
56+
}
57+
}
58+
return true;
59+
}
60+
}

0 commit comments

Comments
 (0)