Skip to content

Commit 8ec764e

Browse files
committed
add leetcode: 1195,48,49
1 parent eb9a286 commit 8ec764e

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package leetCode.multiThread;
2+
3+
import java.util.function.IntConsumer;
4+
5+
/**
6+
* @author : CodeWater
7+
* @create :2022-08-01-16:28
8+
* @Function Description :1195. 交替打印字符串
9+
*/
10+
public class _1195AlternatePrintString {
11+
class FizzBuzz {
12+
private int n;
13+
private volatile int state = 0;
14+
15+
public FizzBuzz(int n) {
16+
this.n = n;
17+
}
18+
19+
// printFizz.run() outputs "fizz".
20+
public void fizz(Runnable printFizz) throws InterruptedException {
21+
// 注意for间隔,省力
22+
for( int i = 3 ; i <= n ; i += 3 ){
23+
//15的倍数不处理,交给fizzbuzz()方法处理
24+
if( i % 15 == 0 ) continue;
25+
while( state != 3 )
26+
Thread.yield();//让当前线程暂停
27+
28+
printFizz.run();
29+
state = 0 ;
30+
31+
}
32+
33+
}
34+
35+
// printBuzz.run() outputs "buzz".
36+
public void buzz(Runnable printBuzz) throws InterruptedException {
37+
for( int i = 5 ; i <= n ; i += 5 ){
38+
if( i % 15 == 0 ) continue;
39+
while( state != 5 )
40+
Thread.yield();
41+
42+
printBuzz.run();
43+
state = 0;
44+
}
45+
}
46+
47+
// printFizzBuzz.run() outputs "fizzbuzz".
48+
public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
49+
for( int i = 15 ; i <= n ; i += 15 ){
50+
while( state != 15 )
51+
Thread.yield();
52+
53+
state = 0;
54+
printFizzBuzz.run();
55+
}
56+
}
57+
58+
// printNumber.accept(x) outputs "x", where x is an integer.
59+
public void number(IntConsumer printNumber) throws InterruptedException {
60+
for( int i = 1 ; i <= n ; i++ ){
61+
while( state != 0 )
62+
Thread.yield();
63+
if( i % 3 != 0 && i % 5 != 0 )
64+
printNumber.accept(i);
65+
else{
66+
// 能够被谁模成0就让哪个线程来处理
67+
if( i % 15 == 0 )
68+
state = 15;
69+
else if( i % 5 == 0 )
70+
state = 5;
71+
else if( i % 3 == 0 )
72+
state = 3;
73+
}
74+
}
75+
}
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package leetCode.subject.number1_50;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-08-01-19:22
6+
* @Function Description :48.旋转图像
7+
*/
8+
public class _48RotatingImage {
9+
class Solution {
10+
/**顺时针转90度= 先斜对角线对称反转+后对称左右反转
11+
(画图很容易理解)
12+
*/
13+
public void rotate(int[][] matrix) {
14+
int n = matrix.length;
15+
// 先对称反转
16+
for( int i = 0 ; i < n ; i++ ){
17+
// 只枚举下三角部分
18+
for(int j = 0 ; j < i ; j++ ){
19+
swap( matrix , i , j , j , i );
20+
}
21+
}
22+
23+
// 在对称反转
24+
for( int i = 0 ; i < n ; i++ ){
25+
for( int j = 0 , k = n - 1 ; j < k ; j++ , k-- )
26+
swap(matrix , i , j , i , k );
27+
}
28+
}
29+
30+
public void swap( int[][] matrix , int x1 , int y1 , int x2 , int y2 ){
31+
int temp = matrix[x1][y1] ;
32+
matrix[x1][y1] = matrix[x2][y2];
33+
matrix[x2][y2] = temp;
34+
35+
}
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package leetCode.subject.number1_50;
2+
3+
import java.util.*;
4+
5+
/**
6+
* @author : CodeWater
7+
* @create :2022-08-01-18:55
8+
* @Function Description :49.字母异位词分组
9+
*/
10+
public class _49Alphabet {
11+
class Solution {
12+
public List<List<String>> groupAnagrams(String[] strs) {
13+
Map<String , List<String> > hash = new HashMap<>();
14+
for( int i = 0 ; i < strs.length ; i++ ){
15+
char[] c = strs[i].toCharArray();
16+
// 把字符变成字符串排序之后就好处理了,原来乱序的都会一样
17+
Arrays.sort( c );
18+
String s = new String(c);
19+
// 一开始如果不在hash中,新建一个对应的list
20+
if( !hash.containsKey(s) )
21+
hash.put( s , new ArrayList<String>() );
22+
// s有对应的键,就把他加入到对应的值的list数组中去
23+
hash.get(s).add(strs[i]);
24+
}
25+
26+
List<List<String>> res = new ArrayList<>();
27+
for(List<String> temp : hash.values() ) res.add(temp);
28+
29+
return res;
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)