Skip to content

Commit c31eb53

Browse files
committed
提交:283,461.添加注释:94,145
1 parent fd74a9b commit c31eb53

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.mistray.array;
2+
3+
/**
4+
* @author ZJY(MistRay)
5+
* @Project algorithm-study
6+
* @Package com.mistray.array
7+
* @create 2021年03月03日 12:00
8+
* @Desc
9+
* 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
10+
*
11+
* 示例:
12+
*
13+
* 输入: [0,1,0,3,12]
14+
* 输出: [1,3,12,0,0]
15+
* 说明:
16+
*
17+
* 必须在原数组上操作,不能拷贝额外的数组。
18+
* 尽量减少操作次数。
19+
* 思路:
20+
* 1. 非零前移法
21+
* 有零直接略过,将后面的数和0替换,最后得到0的数量,将结尾对应数量的数变为0.
22+
* 使用一个指针遇到0就停,继续走,遇到非0就替换.
23+
* 2. 双指针法
24+
*
25+
*/
26+
public class MoveZeroes283 {
27+
public void moveZeroes(int[] nums) {
28+
int index = 0;
29+
for (int i = 0; i < nums.length; i++) {
30+
if (nums[i] != 0){
31+
nums[index++] = nums[i];
32+
}
33+
}
34+
35+
while(index < nums.length){
36+
nums[index ++] = 0;
37+
}
38+
}
39+
40+
public static void main(String[] args) {
41+
MoveZeroes283 moveZeroes283 = new MoveZeroes283();
42+
int[] xxx = new int[]{0,1,0,3,12};
43+
moveZeroes283.moveZeroes(xxx);
44+
for (int i = 0; i < xxx.length; i++) {
45+
System.out.println(xxx[i]);
46+
}
47+
}
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.mistray.bit;
2+
3+
/**
4+
* @author ZJY(MistRay)
5+
* @Project algorithm-study
6+
* @Package com.mistray.bit
7+
* @create 2021年03月03日 11:44
8+
* @Desc
9+
* 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。
10+
*
11+
* 给出两个整数 x 和 y,计算它们之间的汉明距离。
12+
*
13+
* 注意:
14+
* 0 ≤ x, y < 231.
15+
*
16+
* 示例:
17+
*
18+
* 输入: x = 1, y = 4
19+
*
20+
* 输出: 2
21+
*
22+
* 解释:
23+
* 1 (0 0 0 1)
24+
* 4 (0 1 0 0)
25+
* ↑ ↑
26+
*
27+
* 上面的箭头指出了对应二进制位不同的位置。
28+
*
29+
* 思路:
30+
* 使用亦或(同位不同为true),得到中间变量
31+
* 将中间变量每一位不同的个数相加,最终即可得到汉明距离
32+
*/
33+
public class HammingDistance461 {
34+
public int hammingDistance(int x, int y) {
35+
int z = x ^ y;
36+
int sum = 0;
37+
while (z > 0){
38+
sum += z & 1;
39+
z = z >> 1;
40+
}
41+
return sum;
42+
}
43+
44+
public static void main(String[] args) {
45+
HammingDistance461 hammingDistance461 = new HammingDistance461();
46+
System.out.println(hammingDistance461.hammingDistance(1,4));
47+
}
48+
}

leetcode/src/main/java/com/mistray/tree/BinaryTreeInorderTraversal94.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* @Package com.mistray.tree
1111
* @create 2020年03月06日 13:54
1212
* @Desc
13+
* 给定一个二叉树的根节点 root ,返回它的 中序 遍历。
1314
*/
1415
public class BinaryTreeInorderTraversal94 {
1516

leetcode/src/main/java/com/mistray/tree/BinaryTreePostorderTraversal145.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,16 @@
1010
* @Package com.mistray.tree
1111
* @create 2020年03月06日 14:47
1212
* @Desc
13+
* 给定一个二叉树,返回它的 后序 遍历。
14+
* 前序: 根左右
15+
* 中序: 左根右
16+
* 后序: 左右根
1317
*/
1418
public class BinaryTreePostorderTraversal145 {
1519
public List<Integer> postorderTraversal(TreeNode root) {
20+
if (root == null){
21+
return new ArrayList<>();
22+
}
1623
List<Integer> list = new ArrayList<>();
1724
Stack<TreeNode> stack1 = new Stack<>();
1825
Stack<TreeNode> stack2 = new Stack<>();

0 commit comments

Comments
 (0)