Skip to content

Commit 83ed643

Browse files
committed
Java
1 parent 594b630 commit 83ed643

4 files changed

Lines changed: 162 additions & 76 deletions

File tree

‎docs/AimForOffer/10_深度优先.md‎

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,72 @@ public void solve(char[][] board) {
258258
[太平洋大西洋水流问题](https://leetcode-cn.com/problems/pacific-atlantic-water-flow/)
259259

260260
```java
261+
private int m,n;
262+
private boolean[][] pacific;
263+
private boolean[][] atlantic;
261264

262-
```
265+
private int[][] d={
266+
{-1,0},
267+
{0,1},
268+
{1,0},
269+
{0,-1}
270+
};
271+
272+
private boolean inArea(int x,int y){
273+
return (x>=0 && x<m) && (y>=0 && y<n);
274+
}
275+
276+
private void dfs(int[][] matrix,int startx,int starty,boolean[][] visited){
277+
visited[startx][starty]=true;
278+
for(int i=0;i<4;i++){
279+
int newX=startx+d[i][0];
280+
int newY=starty+d[i][1];
281+
if(inArea(newX,newY)){
282+
if(!visited[newX][newY] && matrix[newX][newY]>=matrix[startx][starty]){
283+
dfs(matrix,newX,newY,visited);
284+
}
285+
}
286+
}
287+
}
263288

289+
public List<List<Integer>> pacificAtlantic(int[][] matrix) {
290+
List<List<Integer>> res=new ArrayList<>();
291+
m=matrix.length;
292+
if(m==0){
293+
return res;
294+
}
295+
n=matrix[0].length;
296+
pacific=new boolean[m][n];
297+
atlantic=new boolean[m][n];
264298

299+
for(int j=0;j<n;j++){
300+
if(!pacific[0][j]){
301+
dfs(matrix,0,j,pacific);
302+
}
303+
if(!atlantic[m-1][j]){
304+
dfs(matrix,m-1,j,atlantic);
305+
}
306+
}
307+
308+
for(int i=0;i<m;i++){
309+
if(!pacific[i][0]){
310+
dfs(matrix,i,0,pacific);
311+
}
312+
if(!atlantic[i][n-1]){
313+
dfs(matrix,i,n-1,atlantic);
314+
}
315+
}
316+
317+
for(int i=0;i<m;i++){
318+
for(int j=0;j<n;j++){
319+
if(pacific[i][j] && atlantic[i][j]){
320+
res.add(Arrays.asList(i,j));
321+
}
322+
}
323+
}
324+
return res;
325+
}
326+
```
265327

266328

267329

‎docs/AimForOffer/9_回溯.md‎

Lines changed: 97 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -174,77 +174,6 @@ public boolean exist(char[][] board, String word) {
174174

175175

176176

177-
## 4、机器人的运动范围(66)
178-
179-
[机器人的运动范围](https://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8?tpId=13&tqId=11219&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
180-
181-
```java
182-
//思路:
183-
//flood-fill 算法
184-
private int m;
185-
private int n;
186-
187-
private boolean[][] visited;
188-
189-
private int[][] d={
190-
{0,1}, //向右
191-
{-1,0},//向上
192-
{0,-1},//向左
193-
{1,0} //向下
194-
};
195-
196-
private boolean inArea(int x,int y){
197-
return (x>=0 && x<m) && (y>=0 && y<n);
198-
}
199-
200-
//获取一个数各个位上的数字相加
201-
private int getNum(int num){
202-
int sum = 0;
203-
while(num>0){
204-
sum += num %10;
205-
num/=10;
206-
}
207-
return sum;
208-
}
209-
210-
private boolean valid(int threshlod ,int x,int y){
211-
return (getNum(x)+getNum(y))<=threshlod;
212-
}
213-
214-
private int walk(int threshold,int startx,int starty){
215-
visited[startx][starty]=true;
216-
int walks = 0;
217-
if(valid(threshold,startx,starty)){
218-
walks=1;
219-
}
220-
for(int i=0;i<4;i++){
221-
int newX = startx+d[i][0];
222-
int newY = starty+d[i][1];
223-
if(inArea(newX,newY)){
224-
if(!visited[newX][newY] && valid(threshold,newX,newY)){
225-
walks += walk(threshold,newX,newY);
226-
}
227-
}
228-
}
229-
return walks;
230-
}
231-
232-
public int movingCount(int threshold, int rows, int cols) {
233-
if(threshold<0){ //threshold<0,则机器人就不能走了
234-
return 0;
235-
}
236-
m = rows;
237-
if(m==0){
238-
return 0;
239-
}
240-
n= cols;
241-
visited = new boolean[m][n];
242-
return walk(threshold,0,0);
243-
}
244-
```
245-
246-
247-
248177
## 5、全排列
249178

250179
[全排列](https://leetcode-cn.com/problems/permutations/)
@@ -267,7 +196,7 @@ public List<List<Integer>> permute(int[] nums) {
267196
return res;
268197
}
269198

270-
//p中保存一个有index的元素的排列
199+
//p中保存一个有 index 的元素的排列
271200
//向这个排列的末尾添加第 (index+1) 个元素,组成有(index+1) 个元素排列
272201
private void generatePermutation(int[] nums,int index,List<Integer> p){
273202
if(index==nums.length){
@@ -335,7 +264,7 @@ private void findUniquePermutation(int[] nums,int index,List<Integer> p){
335264

336265

337266

338-
## 7、字符串的排列
267+
## *7、字符串的排列
339268

340269
[字符串的排列](https://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7?tpId=13&tqId=11180&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
341270

@@ -386,7 +315,36 @@ private void permute(char[] chs,int index,StringBuilder p){
386315
[字母大小写全排列](https://leetcode-cn.com/problems/letter-case-permutation/)
387316

388317
```java
318+
private List<String> res;
319+
320+
//处理 index 位置的数据
321+
//如果 index 位置是字母的话,则替换
322+
private void replaceLetter(int index,StringBuilder p){
323+
if(index==p.length()){
324+
res.add(p.toString());
325+
return;
326+
}
327+
char ch=p.charAt(index);
328+
if(Character.isLetter(ch)){
329+
p.setCharAt(index,Character.toUpperCase(ch));
330+
replaceLetter(index+1,p);
331+
p.setCharAt(index,Character.toLowerCase(ch));
332+
replaceLetter(index+1,p);
333+
}else{
334+
replaceLetter(index+1,p);
335+
}
336+
return;
337+
}
389338

339+
public List<String> letterCasePermutation(String S) {
340+
res=new ArrayList<>();
341+
if(S==null || S.length()==0){
342+
return res;
343+
}
344+
StringBuilder p=new StringBuilder(S);
345+
replaceLetter(0,p);
346+
return res;
347+
}
390348
```
391349

392350

@@ -443,7 +401,73 @@ public List<String> letterCombinations(String digits) {
443401
[组合](https://leetcode-cn.com/problems/combinations/)
444402

445403
```java
404+
//思路一:暴力解法
405+
406+
private List<List<Integer>> res;
407+
408+
//c存储已经找到的组合
409+
//从start开始搜索新的元素
410+
private void findCombination(int n,int k,int start,List<Integer> c){
411+
if(k==c.size()){
412+
res.add(new ArrayList<>(c));
413+
return;
414+
}
415+
for(int i=start;i<=n;i++){
416+
c.add(i);
417+
findCombination(n,k,i+1,c);
418+
c.remove(c.size()-1);
419+
}
420+
return;
421+
}
422+
423+
public List<List<Integer>> combine(int n, int k) {
424+
res=new ArrayList<>();
425+
if(n<=0 || k<=0 || n<k){
426+
return res;
427+
}
428+
List<Integer> c=new ArrayList<>();
429+
findCombination(n,k,1,c);
430+
return res;
431+
}
432+
```
433+
434+
435+
436+
```java
437+
//思路二:优化,进行剪枝
438+
private List<List<Integer>> res;
439+
440+
//c存储已经找到的组合
441+
//从start开始搜索新的元素
442+
private void findCombination(int n,int k,int start,List<Integer> c){
443+
if(k==c.size()){
444+
res.add(new ArrayList<>(c));
445+
return;
446+
}
447+
448+
//TODO:优化--剪枝
449+
//c存储的是已经找到的组合。
450+
//此时还剩下k-c.size()个空位,
451+
//则 [i...n]之间的元素最少要有 k-c.size() 个,即 n-i+1 >= k-c.size()
452+
//所以有 i <= n-(k-c.size())+1
453+
454+
for(int i=start;i<=n-(k-c.size())+1;i++){
455+
c.add(i);
456+
findCombination(n,k,i+1,c);
457+
c.remove(c.size()-1);
458+
}
459+
return;
460+
}
446461

462+
public List<List<Integer>> combine(int n, int k) {
463+
res=new ArrayList<>();
464+
if(n<=0 || k<=0 || n<k){
465+
return res;
466+
}
467+
List<Integer> c=new ArrayList<>();
468+
findCombination(n,k,1,c);
469+
return res;
470+
}
447471
```
448472

449473

‎docs/Java_Concurrency/8_线程池.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ shutdown() 和 shutdownNow() 这两个方法的原理都是**遍历线程池中
207207

208208
**CPU 密集型任务**配置尽可能少的线程数量,如配置 (N cpu) + 1 个线程的线程池。
209209

210-
**IO 密集型任务**则由于需要等待 IO 操作,线程并不是一直在执行任务,则配置尽可能多的线程,如 2 * (N cpu) 。
210+
**IO 密集型任务**则由于需要等待 IO 操作,线程并不是一直在执行任务,则配置尽可能多的线程,如 2 * (N cpu) +1。
211211

212212
**混合型的任务**,如果可以拆分,则将其拆分成一个 CPU 密集型任务和一个 IO 密集型任务,只要这两个任务执行的时间相差不是太大,那么分解后执行的吞吐率要高于串行执行的吞吐率,如果这两个任务执行时间相差太大,则没必要进行分解。
213213

‎docs/Java_Concurrency/9_并发实践.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ synchronized 和 ReentrantLock。
236236

237237
#### 1. 栈封闭
238238

239-
多个线程访问同一个方法的局部变量时,不会出现线程安全问题,因为局部变量存储在虚拟机栈中,属于线程私有的。
239+
**多个线程访问同一个方法的局部变量**时,不会出现线程安全问题,因为局部变量存储在虚拟机栈中,属于线程私有的。
240240

241241
```java
242242
public class StackClosedExample {

0 commit comments

Comments
 (0)