Skip to content

Commit 653be02

Browse files
committed
review-1
1 parent d0bfd51 commit 653be02

6 files changed

Lines changed: 83 additions & 57 deletions

File tree

‎src/main/java/com/chen/algorithm/znn/bitmap/test461/Solution.java‎

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,49 +20,16 @@
2020
*
2121
* @Auther: zhunn
2222
* @Date: 2020/11/07 22:23
23-
* @Description: 汉明距离:1-内置位计数功能;2-移位;3-布赖恩·克尼根算法(x&(x-1))
23+
* @Description: 汉明距离:0-首选-位运算;1-内置位计数功能;2-移位;3-布赖恩·克尼根算法(x&(x-1))
2424
*/
2525
public class Solution {
2626

2727
/**
28-
* 1-内置位计数功能
29-
*
3028
* @param x
3129
* @param y
3230
* @return
3331
*/
3432
public int hammingDistance(int x, int y) {
35-
return Integer.bitCount(x ^ y);
36-
}
37-
38-
/**
39-
* 2-移位
40-
*
41-
* @param x
42-
* @param y
43-
* @return
44-
*/
45-
public int hammingDistance2(int x, int y) {
46-
int dis = x ^ y;
47-
int res = 0;
48-
49-
while (dis != 0) {
50-
if ((dis & 1) == 1) {
51-
res++;
52-
}
53-
dis = dis >> 1;
54-
}
55-
return res;
56-
}
57-
58-
/**
59-
* 3-布赖恩·克尼根算法(x&(x-1))
60-
*
61-
* @param x
62-
* @param y
63-
* @return
64-
*/
65-
public int hammingDistance3(int x, int y) {
6633
int dis = x ^ y;
6734
int res = 0;
6835
while (dis != 0) {
@@ -72,10 +39,59 @@ public int hammingDistance3(int x, int y) {
7239
return res;
7340
}
7441

42+
///**
43+
// * 1-内置位计数功能
44+
// *
45+
// * @param x
46+
// * @param y
47+
// * @return
48+
// */
49+
//public int hammingDistance1(int x, int y) {
50+
// return Integer.bitCount(x ^ y);
51+
//}
52+
//
53+
///**
54+
// * 2-移位
55+
// *
56+
// * @param x
57+
// * @param y
58+
// * @return
59+
// */
60+
//public int hammingDistance2(int x, int y) {
61+
// int dis = x ^ y;
62+
// int res = 0;
63+
//
64+
// while (dis != 0) {
65+
// if ((dis & 1) == 1) {
66+
// res++;
67+
// }
68+
// dis = dis >> 1;
69+
// }
70+
// return res;
71+
//}
72+
//
73+
///**
74+
// * 3-布赖恩·克尼根算法(x&(x-1))
75+
// *
76+
// * @param x
77+
// * @param y
78+
// * @return
79+
// */
80+
//public int hammingDistance3(int x, int y) {
81+
// int dis = x ^ y;
82+
// int res = 0;
83+
// while (dis != 0) {
84+
// res++;
85+
// dis = dis & (dis - 1);
86+
// }
87+
// return res;
88+
//}
89+
7590
@Test
7691
public void test() {
77-
System.out.println(hammingDistance(1, 4));
78-
System.out.println(hammingDistance2(1, 4));
79-
System.out.println(hammingDistance3(1, 4));
92+
System.out.println(hammingDistance(1, 7));
93+
//System.out.println(hammingDistance1(1, 7));
94+
//System.out.println(hammingDistance2(1, 7));
95+
//System.out.println(hammingDistance3(1, 7));
8096
}
8197
}

‎src/main/java/com/chen/algorithm/znn/frequency/test146/LRUCache.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*
3434
* @Auther: zhunn
3535
* @Date: 2020/11/08 14:47
36-
* @Description: LRU缓存机制:哈希表 + 双向链表
36+
* @Description: LRU缓存机制:哈希表 + 双向链表(手动实现)
3737
*/
3838
public class LRUCache {
3939

‎src/main/java/com/chen/algorithm/znn/frequency/test208/Trie.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
public class Trie {
2121

2222
public Trie root;
23-
public char val;
2423
public Trie[] children = new Trie[26];
24+
public char val;
2525
public boolean isWord;
2626

2727
public Trie() {

‎src/main/java/com/chen/algorithm/znn/frequency/test48/Solution.java‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@
4444
*/
4545
public class Solution {
4646

47-
47+
/**
48+
* 顺时针旋转90°,先转置再反转每一行
49+
* 逆时针旋转90°,先转置再反转每一列
50+
* @param matrix
51+
*/
4852
public void rotate(int[][] matrix) {
4953

5054
int n = matrix.length;
@@ -64,8 +68,8 @@ public void rotate(int[][] matrix) {
6468
for (int i = 0; i < n; i++) {
6569
for (int j = 0; j < m / 2; j++) {
6670
int temp = matrix[i][j];
67-
matrix[i][j] = matrix[i][m - j - 1];
68-
matrix[i][m - j - 1] = temp;
71+
matrix[i][j] = matrix[i][m - 1 - j];
72+
matrix[i][m - 1 - j] = temp;
6973
}
7074
}
7175
}

‎src/main/java/com/chen/algorithm/znn/frequency/test7/Solution.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.chen.algorithm.znn.frequency.test7;
22

3+
import org.junit.Test;
4+
35
/**
46
* 7. 整数反转
57
* 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
@@ -19,7 +21,7 @@
1921
*/
2022
public class Solution {
2123

22-
public static int reverse1(int x) {
24+
public int reverse(int x) {
2325
int res = 0;
2426
while (x != 0) {
2527
res = res * 10 + x % 10;
@@ -31,7 +33,8 @@ public static int reverse1(int x) {
3133
return res;
3234
}
3335

34-
public static void main(String[] args) {
35-
System.out.println(reverse1(123));
36+
@Test
37+
public void test() {
38+
System.out.println(reverse(123));
3639
}
3740
}

‎src/main/java/com/chen/algorithm/znn/frequency/test9/Solution.java‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.chen.algorithm.znn.frequency.test9;
22

3+
import org.junit.Test;
4+
35
/**
46
* 9. 回文数
57
* 判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
@@ -21,28 +23,29 @@
2123
*/
2224
public class Solution {
2325

24-
public static boolean isPalindrome1(int x) {
26+
public boolean isPalindrome(int x) {
2527
// 特殊情况:
2628
// 当 x < 0 时,x 不是回文数。
2729
// 同样地,如果数字的最后一位是 0,为了使该数字为回文,
2830
// 则其第一位数字也应该是 0
2931
// 只有 0 满足这一属性
3032
if (x < 0 || (x != 0 && x % 10 == 0)) return false;
31-
int rev = 0;
32-
while (x > rev) {
33-
rev = rev * 10 + x % 10;
33+
int res = 0;
34+
while (x > res) {
35+
res = res * 10 + x % 10;
3436
x /= 10;
3537
}
3638
// 当数字长度为奇数时,我们可以通过 revertedNumber/10 去除处于中位的数字。
37-
return rev == x || rev / 10 == x;
39+
return res == x || res / 10 == x;
3840
}
3941

40-
public static void main(String[] args) {
41-
System.out.println(isPalindrome1(-121));
42-
System.out.println(isPalindrome1(0));
43-
System.out.println(isPalindrome1(1000));
44-
System.out.println(isPalindrome1(12321));
45-
System.out.println(isPalindrome1(1221));
46-
System.out.println(isPalindrome1(1231));
42+
@Test
43+
public void test() {
44+
System.out.println(isPalindrome(-121));
45+
System.out.println(isPalindrome(0));
46+
System.out.println(isPalindrome(1000));
47+
System.out.println(isPalindrome(12321));
48+
System.out.println(isPalindrome(1221));
49+
System.out.println(isPalindrome(1231));
4750
}
4851
}

0 commit comments

Comments
 (0)