提交 5e19abe8 编写于 作者: L liu13

20190122

上级 49eb5a0f
......@@ -26,6 +26,7 @@
| 033 | [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) |Medium| [Java](./code/lc33.java)
| 034 | [Search for a Range](https://leetcode.com/problems/search-for-a-range/) |Medium| [Java](./code/lc34.java)
| 035 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) |Medium| [Java](./code/lc35.java)
| 036 |
| 041 |
......@@ -35,9 +36,10 @@
| 046 | [Permutations](https://leetcode.com/problems/permutations/) |Medium| [Java](./code/lc46.java)
| 048 | [Rotate Image](https://leetcode.com/problems/rotate-image/) |Medium| [Java](./code/lc48.java)
| 049 | [Group Anagrams](https://leetcode.com/problems/anagrams/)|Medium| [Java](./code/lc49.java)
| 050 |
| 053 | [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) |Medium| [Java](./code/lc53.java)
| 054 |
| 055 | [Jump Game](https://leetcode.com/problems/jump-game/) |Medium| [Java](./code/lc55.java)
| 056 | [Merge Intervals](https://leetcode.com/problems/merge-intervals/) |Medium| [Java](./code/lc56.java)
| 062 | [Unique Paths](https://leetcode.com/problems/unique-paths/) |Medium| [Java](./code/lc62.java)
......
package code;
/*
* 36. Valid Sudoku
* 题意:横排,竖排,3*3不能包含相同数字
* 难度:Medium
* 分类:Hash Table
* 思路:用一个Set即可,把行号,列号拼上去
* Tips:注意add如果已经有了,返回的是false
*/
import java.util.HashSet;
public class lc36 {
public boolean isValidSudoku(char[][] board) {
HashSet<String> hs = new HashSet(); // 如果此set已包含该元素,则该调用不更改set并返回false。
for (int i = 0; i < board.length ; i++) {
for (int j = 0; j < board[0].length ; j++) {
if(board[i][j]=='.') continue;
boolean flag1 = hs.add(board[i][j]+"row"+i);
boolean flag2 = hs.add(board[i][j]+"col"+j);
boolean flag3 = hs.add(board[i][j]+"box"+i/3+j/3);
if((!flag1)||(!flag2)||(!flag3)) {
return false;
}
}
}
return true;
}
}
package code;
/*
* 50. Pow(x, n)
* 题意:幂运算
* 难度:Medium
* 分类:Math, Binary Search
* 思路:和lc29的思路类似,数值上的二分。用前一步的结果进行下一步的运算,降低了迭代次数
* Tips:注意溢出的情况,负数的情况。
*/
public class lc50 {
public double myPow(double x, int n) {
if(n == 0) return 1;
if(n==Integer.MIN_VALUE && x!=1 && x!=-1) return 0;
if(n<0){
n = -n;
x = 1/x; //负数变成 1/x
}
return (n%2 == 0) ? myPow(x*x, n/2) : x*myPow(x*x, n/2);
}
}
package code;
/*
* 54. Spiral Matrix
* 题意:螺旋输出矩阵
* 难度:Medium
* 分类:Array
* 思路:很直接的思路,就是循环打印
* Tips:需要注意很多细节,最后调试成功
*/
import java.util.ArrayList;
import java.util.List;
public class lc54 {
public static void main(String[] args) {
int[][] matrix = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
System.out.println(spiralOrder(matrix));
}
public static List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<>();
if(matrix.length==0) return res;
int r=0, c=-1; // -1开始,循环内首先++
int row_r = matrix.length;
int col_r = matrix[0].length;
int row_l = -1;
int col_l =-1;
while( row_l+1<row_r || col_l+1<col_r ){
c++;
if(c<col_r){
while(c<col_r){
res.add(matrix[r][c]);
c++;
}
}else //提前终止
return res;
c--; // -- 恢复正常
r++;
if(r<row_r) {
while (r < row_r) {
res.add(matrix[r][c]);
r++;
}
}else
return res;
r--;
c--;
if(c>col_l){
while(c>col_l){
res.add(matrix[r][c]);
c--;
}
}else
return res;
c++;
r--;
if(r>row_l+1) {
while (r > row_l + 1) {
res.add(matrix[r][c]);
r--;
}
}else
return res;
r++;
row_l++;
row_r--;
col_l++;
col_r--;
}
return res;
}
}
......@@ -39,6 +39,7 @@ Language: Java
| 032 [Java](./code/lc32.java)
| 033 [Java](./code/lc33.java)
| 034 [Java](./code/lc34.java)
| 036 [Java](./code/lc36.java)
| 038 [Java](./code/lc38.java)
| 039 [Java](./code/lc39.java)
| 041 [Java](./code/lc41.java)
......@@ -47,7 +48,9 @@ Language: Java
| 046 [Java](./code/lc46.java)
| 048 [Java](./code/lc48.java)
| 049 [Java](./code/lc49.java)
| 050 [Java](./code/lc50.java)
| 053 [Java](./code/lc53.java)
| 054 [Java](./code/lc54.java)
| 055 [Java](./code/lc55.java)
| 056 [Java](./code/lc56.java)
| 058 [Java](./code/lc58.java)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册