Skip to content

Commit 523ae79

Browse files
committed
20190302
1 parent 028e140 commit 523ae79

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

code/lc242.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package code;
2+
/*
3+
* 242. Valid Anagram
4+
* 题意:字符串t是否为s打乱后的重排列
5+
* 难度:Easy
6+
* 分类:Hash Table, Sort
7+
* 思路:
8+
* Tips:
9+
*/
10+
public class lc242 {
11+
public boolean isAnagram(String s, String t) {
12+
if(s.length()!=t.length()) return false;
13+
int[] chs = new int[26];
14+
for(int i=0; i<s.length(); i++){
15+
chs[s.charAt(i)-'a']++;
16+
}
17+
for(int i=0; i<t.length(); i++){
18+
chs[t.charAt(i)-'a']--;
19+
if(chs[t.charAt(i)-'a']<0) return false;
20+
}
21+
return true;
22+
}
23+
}

code/lc268.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package code;
2+
/*
3+
* 268. Missing Number
4+
* 题意:找出 0~n 中少的那个数
5+
* 难度:Easy
6+
* 分类:Array, Math, Bit Manipulation
7+
* 思路:两种巧妙的方法,时间空间都是O(1)
8+
* 异或
9+
* 求和以后,减去所有
10+
* Tips:
11+
*/
12+
public class lc268 {
13+
public int missingNumber(int[] nums) {
14+
int res = nums.length*(nums.length+1)/2;
15+
for(int i:nums) res-=i;
16+
return res;
17+
}
18+
public int missingNumber2(int[] nums) {
19+
int res = nums.length;
20+
for(int i=0; i<nums.length; i++) res^=i^nums[i];
21+
return res;
22+
}
23+
}

code/lc326.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package code;
2+
/*
3+
* 326. Power of Three
4+
* 题意:判断该数是否为3的幂
5+
* 难度:Easy
6+
* 分类:Math
7+
* 思路:除以3,除到不能整除,判断是否为1
8+
* Tips:
9+
*/
10+
public class lc326 {
11+
public boolean isPowerOfThree(int n) {
12+
if (n < 1) return false;
13+
while (n % 3 == 0) n /= 3;
14+
return n == 1;
15+
}
16+
public boolean isPowerOfThree2(int n) {
17+
if(n==1) return true;
18+
double d = n;
19+
while(d>1){
20+
d=d/3;
21+
if(d==1) return true;
22+
}
23+
return false;
24+
}
25+
}

code/lc329.java

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package code;
2+
/*
3+
* 329. Longest Increasing Path in a Matrix
4+
* 题意:寻找最长的递增路径
5+
* 难度:Hard
6+
* 分类:Depth-first Search, Topological Sort, Memoization
7+
* 思路:带记忆的dfs,之前计算的最优解可以直接合并
8+
* Tips:
9+
*/
10+
public class lc329 {
11+
public int longestIncreasingPath(int[][] matrix) {
12+
if(matrix.length==0) return 0;
13+
int[][] cache = new int[matrix.length][matrix[0].length]; //存储计算过的结果
14+
int res = 0;
15+
for (int i = 0; i < matrix.length ; i++) {
16+
for (int j = 0; j < matrix[0].length ; j++) {
17+
res = Math.max(dfs(matrix, i, j, cache), res);
18+
}
19+
}
20+
return res;
21+
}
22+
23+
public int dfs(int[][] matrix, int i, int j, int[][] cache){
24+
int max = 1;
25+
if(cache[i][j]!=0) return cache[i][j];
26+
if( i>0 && matrix[i-1][j]>matrix[i][j]) max = Math.max(dfs(matrix, i-1, j, cache)+1, max);
27+
if( j>0 && matrix[i][j-1]>matrix[i][j]) max = Math.max(dfs(matrix, i, j-1, cache)+1, max);
28+
if( i+1<matrix.length && matrix[i+1][j]>matrix[i][j]) max = Math.max(dfs(matrix, i+1, j, cache)+1, max);
29+
if( j+1<matrix[0].length && matrix[i][j+1]>matrix[i][j]) max = Math.max(dfs(matrix, i, j+1, cache)+1, max);
30+
cache[i][j] = max;
31+
return max;
32+
}
33+
}

0 commit comments

Comments
 (0)