GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 第一次提交 · ShaoNingYa/JavaInterview@546812c

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • README.md
  • docs
    • dataStructures-algorithms
      • Backtracking-NQueens.md
      • 公司真题.md
      • 几道常见的子符串算法题.md
      • 几道常见的链表算法题.md
      • 剑指offer部分编程题.md
      • 数据结构.md
      • 算法学习资源推荐.md
      • 算法面试真题汇总.md
      • 算法题目难点题目总结.md
    • essential-content-for-interview
      • BATJrealInterviewExperience
        • 2019alipay-pinduoduo-toutiao.md
        • 5面阿里,终获offer.md
        • 蚂蚁金服实习生面经总结(已拿口头offer).md
      • PreparingForInterview
        • JavaInterviewLibrary.md
        • JavaProgrammerNeedKnow.md
        • interviewPrepare.md
        • 应届生面试最爱问的几道Java基础问题.md
        • 程序员的简历之道.md
        • 美团面试常见问题总结.md
        • 面试官-你有什么问题要问我.md
      • real-interview-experience-analysis
        • alibaba-1.md
      • 手把手教你用Markdown写一份高质量的简历.md
      • 简历模板.md
      • 面试必备之乐观锁与悲观锁.md
    • interview-experience
      • 各大公司面经.md
      • 面试常见知识.md
      • 面试常见问题分类汇总.md
    • java
      • BIO-NIO-AIO.md
      • Basis
        • Arrays,CollectionsCommonMethods.md
        • final、static、this、super.md
        • 用好Java中的枚举,真的没有那么简单!.md
      • J2EE基础知识.md
      • Java IO与NIO.md
      • Java基础知识.md
      • Java疑难点.md
      • Java程序设计题.md
      • Java编程规范.md
      • Multithread
        • AQS.md
        • Atomic.md
        • JavaConcurrencyAdvancedCommonInterviewQuestions.md
        • JavaConcurrencyBasicsCommonInterviewQuestionsSummary.md
        • ThreadLocal.md
        • java线程池学习总结.md
        • synchronized.md
        • 并发容器总结.md
        • 并发编程基础知识.md
      • What's New in JDK8
        • Java8Tutorial.md
        • Java8foreach指南.md
        • Java8教程推荐.md
      • collection
        • ArrayList-Grow.md
        • ArrayList.md
        • HashMap.md
        • Java集合框架常见面试题.md
        • LinkedList.md
      • jvm
        • GC调优参数.md
        • JDK监控和故障处理工具总结.md
        • JVM垃圾回收.md
        • Java内存区域.md
        • [加餐]大白话带你认识JVM.md
        • jvm 知识点汇总.md
        • 最重要的JVM参数指南.md
        • 类加载器.md
        • 类加载过程.md
        • 类文件结构.md
      • 多线程系列.md
    • microservice
      • 微服务相关资料.md
    • operating-system
      • 后端程序员必备的Linux基础知识.md
      • 操作系统、计算机网络相关知识.md
    • project
      • 消息中间件面试题.md
      • 秒杀项目总结.md
    • system-design
      • authority-certification
        • JWT-advantages-and-disadvantages.md
        • basis-of-authority-certification.md
      • data-communication
        • Kafka入门看这一篇就够了.md
        • Kafka系统设计开篇-面试看这篇就够了.md
        • RocketMQ-Questions.md
        • RocketMQ.md
        • dubbo.md
        • message-queue.md
        • rabbitmq.md
        • summary.md
        • why-use-rpc.md
      • framework
        • ZooKeeper-plus.md
        • ZooKeeper.md
        • ZooKeeper数据模型和常见命令.md
        • spring
          • Spring-Design-Patterns.md
          • Spring.md
          • SpringBean.md
          • SpringInterviewQuestions.md
          • SpringMVC-Principle.md
      • micro-service
        • API网关.md
        • 分布式id生成方案总结.md
      • website-architecture
        • 8 张图读懂大型网站技术架构.md
        • 关于大型网站系统架构你不得不懂的10个问题.md
        • 分布式.md
        • 如何设计一个高可用系统?要考虑哪些地方?.md
      • 设计模式.md

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎README.md‎

Copy file name to clipboard
+243Lines changed: 243 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
<!--<p align="center">
2+
<a href="https://github.com/Snailclimb/JavaGuide" target="_blank">
3+
<img src="https://my-blog-to-use.oss-cn-beijing.aliyuncs.com/2019-3/logo - 副本.png" width=""/>
4+
</a>
5+
</p>
6+
7+
<p align="center">
8+
<a href="https://snailclimb.gitee.io/javaguide"><img src="https://img.shields.io/badge/阅读-read-brightgreen.svg" alt="阅读"></a>
9+
<a href="#联系我"><img src="https://img.shields.io/badge/chat-微信群-blue.svg" alt="微信群"></a>
10+
<a href="#公众号"><img src="https://img.shields.io/badge/%E5%85%AC%E4%BC%97%E5%8F%B7-JavaGuide-lightgrey.svg" alt="公众号"></a>
11+
<a href="#公众号"><img src="https://img.shields.io/badge/PDF-Java面试突击-important.svg" alt="公众号"></a>
12+
<a href="#投稿"><img src="https://img.shields.io/badge/support-投稿-critical.svg" alt="投稿"></a>
13+
<a href="https://xiaozhuanlan.com/javainterview?rel=javaguide"><img src="https://img.shields.io/badge/Java-面试指南-important" alt="投稿"></a>
14+
</p>-->
15+
16+
## 目录
17+
[TOC]
18+
19+
## 项目准备
20+
21+
- [项目准备知识](docs/project/秒杀项目总结.md)
22+
23+
## 面试知识点
24+
25+
- [各公司面试知识点汇总](docs/interview-experience/面试常见知识.md)
26+
27+
## 公司面经
28+
- [各公司面试经验汇总](docs/interview-experience/各大公司面经.md)
29+
30+
## Java
31+
32+
### 基础
33+
34+
35+
### 容器(包括juc)
36+
37+
38+
39+
### 并发
40+
41+
42+
43+
### JVM
44+
45+
46+
### I/O
47+
48+
49+
### Java8
50+
51+
52+
53+
## 计算机网络
54+
55+
56+
## 计算机操作系统
57+
58+
## Linux
59+
60+
61+
## 数据结构与算法
62+
63+
### 数据结构
64+
65+
66+
67+
### 算法
68+
69+
- [算法面试真题汇总](docs/dataStructures-algorithms/算法面试真题汇总.md)
70+
- [算法题型难点总结](docs/dataStructures-algorithms/算法题目难点题目总结.md)
71+
72+
## 数据库
73+
74+
### MySQL
75+
76+
77+
78+
79+
80+
81+
## 系统设计
82+
83+
### 常用框架
84+
85+
#### Spring
86+
87+
#### SpringBoot
88+
89+
- [微服务相关资料](docs/microservice/微服务相关资料.md)
90+
91+
## 分布式
92+
93+
### 消息中间件
94+
95+
- [消息中间件面试题总结](docs/project/消息中间件面试题.md)
96+
97+
### redis
98+
99+
### 分布式系统
100+
101+
102+
103+
104+
## 设计模式
105+
106+
107+
108+
## 面试指南
109+
110+
111+
112+
## Java学习常见问题汇总
113+
114+
115+
## 工具
116+
117+
### Git
118+
119+
120+
### Docker
121+
122+
123+
124+
### 其他
125+
126+
127+
### 资源
128+
129+
### 书单
130+
131+
132+
### 实战项目推荐
133+
134+
135+
136+
137+
<!--## 说明
138+
139+
### JavaGuide介绍
140+
141+
* **对于 Java 初学者来说:** 本文档倾向于给你提供一个比较详细的学习路径,让你对于Java整体的知识体系有一个初步认识。另外,本文的一些文章
142+
也是你学习和复习 Java 知识不错的实践;
143+
* **对于非 Java 初学者来说:** 本文档更适合回顾知识,准备面试,搞清面试应该把重心放在那些问题上。要搞清楚这个道理:提前知道那些面试常见,不是为了背下来应付面试,而是为了让你可以更有针对的学习重点。
144+
145+
Markdown 格式参考:[Github Markdown格式](https://guides.github.com/features/mastering-markdown/),表情素材来自:[EMOJI CHEAT SHEET](https://www.webpagefx.com/tools/emoji-cheat-sheet/)。
146+
147+
利用 docsify 生成文档部署在 Github pages: [docsify 官网介绍](https://docsify.js.org/#/)
148+
149+
### 作者的其他开源项目推荐
150+
151+
1. [springboot-guide](https://github.com/Snailclimb/springboot-guide) : 适合新手入门以及有经验的开发人员查阅的 Spring Boot 教程(业余时间维护中,欢迎一起维护)。
152+
2. [programmer-advancement](https://github.com/Snailclimb/programmer-advancement) : 我觉得技术人员应该有的一些好习惯!
153+
3. [spring-security-jwt-guide](https://github.com/Snailclimb/spring-security-jwt-guide) :从零入门 !Spring Security With JWT(含权限验证)后端部分代码。
154+
155+
### 关于转载
156+
157+
如果你需要转载本仓库的一些文章到自己的博客的话,记得注明原文地址就可以了。
158+
159+
### 如何对该开源文档进行贡献
160+
161+
1. 笔记内容大多是手敲,所以难免会有笔误,你可以帮我找错别字。
162+
2. 很多知识点我可能没有涉及到,所以你可以对其他知识点进行补充。
163+
3. 现有的知识点难免存在不完善或者错误,所以你可以对已有知识点进行修改/补充。
164+
165+
### 为什么要做这个开源文档?
166+
167+
初始想法源于自己的个人那一段比较迷茫的学习经历。主要目的是为了通过这个开源平台来帮助一些在学习 Java 或者面试过程中遇到问题的小伙伴。
168+
169+
### 投稿
170+
171+
由于我个人能力有限,很多知识点我可能没有涉及到,所以你可以对其他知识点进行补充。大家也可以对自己的文章进行自荐,对于不错的文章不仅可以成功在本仓库展示出来更可以获得作者送出的 50 元左右的任意书籍进行奖励(当然你也可以直接折现50元)。
172+
173+
### 联系我
174+
175+
添加我的微信备注“Github”,回复关键字 **“加群”** 即可入群。
176+
177+
![个人微信](https://my-blog-to-use.oss-cn-beijing.aliyuncs.com/2019-7/wechat3.jpeg)
178+
179+
### Contributor
180+
181+
下面是笔主收集的一些对本仓库提过有价值的pr或者issue的朋友,人数较多,如果你也对本仓库提过不错的pr或者issue的话,你可以加我的微信与我联系。下面的排名不分先后!
182+
183+
<a href="https://github.com/fanofxiaofeng">
184+
<img src="https://avatars0.githubusercontent.com/u/3983683?s=460&v=4" width="45px">
185+
</a>
186+
<a href="https://github.com/LiWenGu">
187+
<img src="https://avatars0.githubusercontent.com/u/15909210?s=460&v=4" width="45px">
188+
</a>
189+
<a href="https://github.com/fanchenggang">
190+
<img src="https://avatars2.githubusercontent.com/u/8225921?s=460&v=4" width="45px">
191+
</a>
192+
<a href="https://github.com/Rustin-Liu">
193+
<img src="https://avatars2.githubusercontent.com/u/29879298?s=400&v=4" width="45px">
194+
</a>
195+
196+
<a href="https://github.com/ipofss">
197+
<img src="https://avatars1.githubusercontent.com/u/5917359?s=460&v=4" width="45px"></a>
198+
<a href="https://github.com/Gene1994">
199+
<img src="https://avatars3.githubusercontent.com/u/24930369?s=460&v=4" width="45px">
200+
</a>
201+
<a href="https://github.com/spikesp">
202+
<img src="https://avatars0.githubusercontent.com/u/12581996?s=460&v=4" width="45px"></a>
203+
<a href="https://github.com/illusorycloud">
204+
<img src="https://avatars3.githubusercontent.com/u/31980412?s=460&v=4" width="45px">
205+
</a>
206+
<a href="https://github.com/kinglaw1204">
207+
<img src="https://avatars1.githubusercontent.com/u/20039931?s=460&v=4" width="45px">
208+
</a>
209+
<a href="https://github.com/jun1st">
210+
<img src="https://avatars2.githubusercontent.com/u/14312378?s=460&v=4" width="45px">
211+
</a>"
212+
<a href="https://github.com/fantasygg">
213+
<img src="https://avatars3.githubusercontent.com/u/13445354?s=460&v=4" width="45px">
214+
</a>
215+
<a href="https://github.com/debugjoker">
216+
<img src="https://avatars3.githubusercontent.com/u/26218005?s=460&v=4" width="45px">
217+
</a>
218+
<a href="https://github.com/zhyank">
219+
<img src="https://avatars0.githubusercontent.com/u/17696240?s=460&v=4" width="45px">
220+
</a>
221+
<a href="https://github.com/Goose9527">
222+
<img src="https://avatars2.githubusercontent.com/u/43314997?s=460&v=4" width="45px">
223+
</a>
224+
<a href="https://github.com/yuechuanx">
225+
<img src="https://avatars3.githubusercontent.com/u/19339293?s=460&v=4" width="45px">
226+
</a>
227+
<a href="https://github.com/cnLGMing">
228+
<img src="https://avatars2.githubusercontent.com/u/15910705?s=460&v=4" width="45px">
229+
</a>
230+
<a href="https://github.com/fanchenggang">
231+
<img src="https://avatars0.githubusercontent.com/u/20358122?s=460&v=4" width="45px">
232+
</a>
233+
234+
### 公众号
235+
236+
如果大家想要实时关注我更新的文章以及分享的干货的话,可以关注我的公众号。
237+
238+
**《Java面试突击》:** 由本文档衍生的专为面试而生的《Java面试突击》V2.0 PDF 版本[公众号](#公众号)后台回复 **"Java面试突击"** 即可免费领取!
239+
240+
**Java工程师必备学习资源:** 一些Java工程师常用学习资源公众号后台回复关键字 **“1”** 即可免费无套路获取。
241+
242+
![我的公众号](https://my-blog-to-use.oss-cn-beijing.aliyuncs.com/2019-6/167598cd2e17b8ec.png)
243+
-->
Collapse file

‎docs/dataStructures-algorithms/Backtracking-NQueens.md‎

Copy file name to clipboard
+145Lines changed: 145 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# N皇后
2+
[51. N皇后](https://leetcode-cn.com/problems/n-queens/)
3+
### 题目描述
4+
> n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。
5+
>
6+
![ANUzjA.png](https://s2.ax1x.com/2019/03/26/ANUzjA.png)
7+
>
8+
上图为 8 皇后问题的一种解法。
9+
>
10+
给定一个整数 n,返回所有不同的 n 皇后问题的解决方案。
11+
>
12+
每一种解法包含一个明确的 n 皇后问题的棋子放置方案,该方案中 'Q' 和 '.' 分别代表了皇后和空位。
13+
14+
示例:
15+
16+
```
17+
输入: 4
18+
输出: [
19+
[".Q..", // 解法 1
20+
"...Q",
21+
"Q...",
22+
"..Q."],
23+
24+
["..Q.", // 解法 2
25+
"Q...",
26+
"...Q",
27+
".Q.."]
28+
]
29+
解释: 4 皇后问题存在两个不同的解法。
30+
```
31+
32+
### 问题分析
33+
约束条件为每个棋子所在的行、列、对角线都不能有另一个棋子。
34+
35+
使用一维数组表示一种解法,下标(index)表示行,值(value)表示该行的Q(皇后)在哪一列。
36+
每行只存储一个元素,然后递归到下一行,这样就不用判断行了,只需要判断列和对角线。
37+
### Solution1
38+
当result[row] = column时,即row行的棋子在column列。
39+
40+
对于[0, row-1]的任意一行(i 行),若 row 行的棋子和 i 行的棋子在同一列,则有result[i] == column;
41+
若 row 行的棋子和 i 行的棋子在同一对角线,等腰直角三角形两直角边相等,即 row - i == Math.abs(result[i] - column)
42+
43+
布尔类型变量 isValid 的作用是剪枝,减少不必要的递归。
44+
```
45+
public List<List<String>> solveNQueens(int n) {
46+
// 下标代表行,值代表列。如result[0] = 3 表示第1行的Q在第3列
47+
int[] result = new int[n];
48+
List<List<String>> resultList = new LinkedList<>();
49+
dfs(resultList, result, 0, n);
50+
return resultList;
51+
}
52+
53+
void dfs(List<List<String>> resultList, int[] result, int row, int n) {
54+
// 递归终止条件
55+
if (row == n) {
56+
List<String> list = new LinkedList<>();
57+
for (int x = 0; x < n; ++x) {
58+
StringBuilder sb = new StringBuilder();
59+
for (int y = 0; y < n; ++y)
60+
sb.append(result[x] == y ? "Q" : ".");
61+
list.add(sb.toString());
62+
}
63+
resultList.add(list);
64+
return;
65+
}
66+
for (int column = 0; column < n; ++column) {
67+
boolean isValid = true;
68+
result[row] = column;
69+
/*
70+
* 逐行往下考察每一行。同列,result[i] == column
71+
* 同对角线,row - i == Math.abs(result[i] - column)
72+
*/
73+
for (int i = row - 1; i >= 0; --i) {
74+
if (result[i] == column || row - i == Math.abs(result[i] - column)) {
75+
isValid = false;
76+
break;
77+
}
78+
}
79+
if (isValid) dfs(resultList, result, row + 1, n);
80+
}
81+
}
82+
```
83+
### Solution2
84+
使用LinkedList表示一种解法,下标(index)表示行,值(value)表示该行的Q(皇后)在哪一列。
85+
86+
解法二和解法一的不同在于,相同列以及相同对角线的校验。
87+
将对角线抽象成【一次函数】这个简单的数学模型,根据一次函数的截距是常量这一特性进行校验。
88+
89+
这里,我将右上-左下对角线,简称为“\”对角线;左上-右下对角线简称为“/”对角线。
90+
91+
“/”对角线斜率为1,对应方程为y = x + b,其中b为截距。
92+
对于线上任意一点,均有y - x = b,即row - i = b;
93+
定义一个布尔类型数组anti_diag,将b作为下标,当anti_diag[b] = true时,表示相应对角线上已经放置棋子。
94+
但row - i有可能为负数,负数不能作为数组下标,row - i 的最小值为-n(当row = 0,i = n时),可以加上n作为数组下标,即将row -i + n 作为数组下标。
95+
row - i + n 的最大值为 2n(当row = n,i = 0时),故anti_diag的容量设置为 2n 即可。
96+
97+
![ANXG79.png](https://s2.ax1x.com/2019/03/26/ANXG79.png)
98+
99+
“\”对角线斜率为-1,对应方程为y = -x + b,其中b为截距。
100+
对于线上任意一点,均有y + x = b,即row + i = b;
101+
同理,定义数组main_diag,将b作为下标,当main_diag[row + i] = true时,表示相应对角线上已经放置棋子。
102+
103+
有了两个校验对角线的数组,再来定义一个用于校验列的数组cols,这个太简单啦,不解释。
104+
105+
**解法二时间复杂度为O(n!),在校验相同列和相同对角线时,引入三个布尔类型数组进行判断。相比解法一,少了一层循环,用空间换时间。**
106+
107+
```
108+
List<List<String>> resultList = new LinkedList<>();
109+
110+
public List<List<String>> solveNQueens(int n) {
111+
boolean[] cols = new boolean[n];
112+
boolean[] main_diag = new boolean[2 * n];
113+
boolean[] anti_diag = new boolean[2 * n];
114+
LinkedList<Integer> result = new LinkedList<>();
115+
dfs(result, 0, cols, main_diag, anti_diag, n);
116+
return resultList;
117+
}
118+
119+
void dfs(LinkedList<Integer> result, int row, boolean[] cols, boolean[] main_diag, boolean[] anti_diag, int n) {
120+
if (row == n) {
121+
List<String> list = new LinkedList<>();
122+
for (int x = 0; x < n; ++x) {
123+
StringBuilder sb = new StringBuilder();
124+
for (int y = 0; y < n; ++y)
125+
sb.append(result.get(x) == y ? "Q" : ".");
126+
list.add(sb.toString());
127+
}
128+
resultList.add(list);
129+
return;
130+
}
131+
for (int i = 0; i < n; ++i) {
132+
if (cols[i] || main_diag[row + i] || anti_diag[row - i + n])
133+
continue;
134+
result.add(i);
135+
cols[i] = true;
136+
main_diag[row + i] = true;
137+
anti_diag[row - i + n] = true;
138+
dfs(result, row + 1, cols, main_diag, anti_diag, n);
139+
result.removeLast();
140+
cols[i] = false;
141+
main_diag[row + i] = false;
142+
anti_diag[row - i + n] = false;
143+
}
144+
}
145+
```

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.