Skip to content

Commit f67de4e

Browse files
JavaScalaDeveloperJavaScalaDeveloper
authored andcommitted
剑指offer2
1 parent 42fb93e commit f67de4e

File tree

24 files changed

+439
-533
lines changed

24 files changed

+439
-533
lines changed

change/note/多线程.md

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -779,59 +779,44 @@ public class AlternatePrinting {
779779

780780
```java
781781
class PrintABC {
782-
private static final Object lock = new Object();
783-
private static int state = 0;
782+
private static final Object lock = new Object();
783+
private static int state = 0;
784784

785-
public static class ThreadA extends Thread {
786-
public void run() {
787-
try {
788-
synchronized (lock) {
789-
for (int i = 0; i < 10; i++) {
790-
while (state % 3 != 0)
791-
lock.wait();
792-
System.out.print("A");
793-
state++;
794-
lock.notifyAll();
795-
}
796-
}
797-
} catch (InterruptedException e) {
798-
e.printStackTrace();
785+
public static class ThreadA extends Thread {
786+
public void run() {
787+
synchronized (lock) {
788+
for (int i = 0; i < 10; i++) {
789+
while (state % 3 != 0) lock.wait();
790+
System.out.print("A");
791+
state++;
792+
lock.notifyAll();
799793
}
800794
}
801795
}
796+
}
802797

803798
public static class ThreadB extends Thread {
804799
public void run() {
805-
try {
806-
synchronized (lock) {
807-
for (int i = 0; i < 10; i++) {
808-
while (state % 3 != 1)
809-
lock.wait();
810-
System.out.print("B");
811-
state++;
812-
lock.notifyAll();
813-
}
800+
synchronized (lock) {
801+
for (int i = 0; i < 10; i++) {
802+
while (state % 3 != 1) lock.wait();
803+
System.out.print("B");
804+
state++;
805+
lock.notifyAll();
814806
}
815-
} catch (InterruptedException e) {
816-
e.printStackTrace();
817807
}
818808
}
819809
}
820810

821811
public static class ThreadC extends Thread {
822812
public void run() {
823-
try {
824-
synchronized (lock) {
825-
for (int i = 0; i < 10; i++) {
826-
while (state % 3 != 2)
827-
lock.wait();
828-
System.out.print("C");
829-
state++;
830-
lock.notifyAll();
831-
}
813+
synchronized (lock) {
814+
for (int i = 0; i < 10; i++) {
815+
while (state % 3 != 2) lock.wait();
816+
System.out.print("C");
817+
state++;
818+
lock.notifyAll();
832819
}
833-
} catch (InterruptedException e) {
834-
e.printStackTrace();
835820
}
836821
}
837822
}
@@ -1285,23 +1270,18 @@ public class BoundedBuffer {
12851270
while (buffer.size() == maxSize) { // 当缓冲区已满时,生产者等待
12861271
wait();
12871272
}
1288-
12891273
buffer.add(item);
12901274
System.out.println("Produced: " + item);
1291-
12921275
notifyAll(); // 唤醒所有等待的线程(包括生产者和消费者)
12931276
}
12941277

12951278
public synchronized int consume() throws InterruptedException {
12961279
while (buffer.size() == 0) { // 当缓冲区为空时,消费者等待
12971280
wait();
12981281
}
1299-
13001282
int item = buffer.remove();
13011283
System.out.println("Consumed: " + item);
1302-
13031284
notifyAll(); // 唤醒所有等待的线程(包括生产者和消费者)
1304-
13051285
return item;
13061286
}
13071287
}

change/note/常用算法的解题思路.md

Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -28,65 +28,55 @@
2828

2929
Java中可以使用位运算来实现二进制加减乘除运算。具体实现如下:
3030

31-
## 二进制加法
32-
33-
二进制加法可以使用上述的位运算实现。假设需要对两个二进制数a和b进行加法运算,可以使用如下代码:
34-
35-
```java
36-
int result=0; // 存储计算结果
37-
int carry=0; // 存储进位结果
38-
while(b!=0){
39-
result=a^b; // 不进位的结果
40-
carry=(a&b)<<1; // 进位的结果
41-
a=result;
42-
b=carry;
43-
}
44-
System.out.println(a); // 输出计算结果
45-
```
46-
47-
## 二进制减法
48-
49-
二进制减法可以先将减数按位取反,然后加上1,最后进行二进制加法。具体实现如下:
31+
## 二进制加减乘除
5032

5133
```java
52-
int negate=~b+1; // 取反加1
53-
int result=a+negate; // 二进制加法
54-
System.out.println(result); // 输出计算结果
55-
```
56-
57-
## 二进制乘法
34+
public class BinaryOperations {
35+
// 二进制加法
36+
public int add(int a, int b) {
37+
while (b != 0) {
38+
int carry = a & b; // 计算进位
39+
a = a ^ b; // 计算无进位的和
40+
b = carry << 1; // 左移进位,继续相加
41+
}
42+
return a;
43+
}
5844

59-
二进制乘法可以使用位运算中的左移运算(<<)和按位与运算(&)实现。假设需要对两个二进制数a和b进行乘法运算,可以使用如下代码:
45+
// 二进制减法
46+
public int sub(int a, int b) {
47+
b = add(~b, 1); // 将减数取反再加1,转换为加法运算
48+
return add(a, b);
49+
}
6050

61-
```java
62-
int result=0; // 存储计算结果
63-
while(b!=0){
64-
if((b&1)==1){ // 如果b的最低位是1
65-
result+=a; // 将a加到计算结果中
66-
}
67-
a<<=1; // 左移一位
68-
b>>=1; // 右移一位
51+
// 二进制乘法
52+
public static int binaryMultiplication(int a, int b) {
53+
int product = 0;
54+
while (b != 0) {
55+
if ((b & 1) != 0) { // 如果b的最低位为1
56+
product = binaryAddition(product, a); // 将a累加到结果中
57+
}
58+
a = a << 1; // a左移1位
59+
b = b >>> 1; // b右移1位
6960
}
70-
System.out.println(result); // 输出计算结果
71-
```
72-
73-
## 二进制除法
74-
75-
二进制除法可以使用位运算中的右移运算(>>)和按位与运算(&)实现。假设需要对两个二进制数a和b进行除法运算,可以使用如下代码:
61+
return product;
62+
}
7663

77-
```java
78-
int quotient=0; //
79-
int remainder=a; // 余数
80-
while(remainder>=b){
81-
int shift=0; // 移位数
82-
while((b<<shift)<=remainder){ // 找到最大的移位数
83-
shift++;
84-
}
85-
shift--; // 因为最后一次移位是多余的,所以需要减去1
86-
quotient+=(1<<shift); // 将移位数加到商中
87-
remainder-=(b<<shift); // 减去移位后的值
88-
}
89-
System.out.println(quotient); // 输出商
64+
// 二进制除法
65+
public static int binaryDivision(int a, int b) {
66+
int quotient = 0;
67+
int remainder = 0;
68+
for (int i = 31; i >= 0; i--) {
69+
int shiftedDividend = a >>> i; // a右移,获取商的位
70+
remainder = remainder << 1; // 余数左移1位
71+
remainder = remainder | (shiftedDividend & 1); // 余数的最低位更新为商的位
72+
if (remainder >= b) {
73+
remainder = remainder - b; // 更新余数
74+
quotient = quotient | (1 << i); // 商的对应位设为1
75+
}
76+
}
77+
return quotient;
78+
}
79+
}
9080
```
9181

9282
# Java里int类型的2到16进制表示方法

change/scripts/Main.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

change/scripts/MergeMdFiles.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
import java.nio.file.attribute.BasicFileAttributes;
66

77
public class MergeMdFiles {
8+
private static final int EXPORT_SIZE=500;
89

910
public static void main(String[] args) throws IOException {
1011
String directory = "D:\\WorkSpaces\\leetcode-all\\solution"; // 修改为目录路径
11-
String outputFile = "D:\\WorkSpaces\\leetcode-all\\change\\scripts\\out\\leetcode-v2023.md"; // 修改为输出文件路径
12+
String outputFile = "D:\\WorkSpaces\\leetcode-all\\change\\scripts\\out\\leetcode-"+EXPORT_SIZE+"-v2023.md"; // 修改为输出文件路径
1213

1314
File dir = new File(directory);
1415
if (!dir.exists() || !dir.isDirectory()) {
@@ -31,14 +32,14 @@ private static void mergeMdFiles(File dir, OutputStream os) throws IOException {
3132
} else if (basicFileAttributes.isRegularFile() && file.getName().endsWith(".md")) {
3233
// 忽略solution根目录的文件
3334
if (file.getPath().length() > 60) {
34-
// String num = file.getPath().substring(46, 50);
35-
// int no = Integer.parseInt(num);
35+
String num = file.getPath().substring(46, 50);
36+
int no = Integer.parseInt(num);
3637
// 导出前2000道
37-
// if (no <= 2000) {
38+
if (no <= EXPORT_SIZE) {
3839
FileInputStream fis = new FileInputStream(file);
3940
copy(fis, os);
4041
fis.close();
41-
// }
42+
}
4243

4344
}
4445
}

change/scripts/Solution.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package change.scripts;
2+
3+
import java.util.Arrays;
4+
5+
/*
6+
非负整数数组,将元素随机排序,将所有元素连接起来形成一个新的数字,找到最小的数字
7+
010,100->10010
8+
5,56->556
9+
0,56->56
10+
*/
11+
public class Solution {
12+
public static void main(String[] args) {
13+
}
14+
15+
16+
}
17+
18+

0 commit comments

Comments
 (0)