Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Index/模拟.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
| [59. 螺旋矩阵 II](https://leetcode-cn.com/problems/spiral-matrix-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/spiral-matrix-ii/solution/yi-ti-shuang-jie-xiang-jie-xing-zhuang-j-24x8/) | 中等 | 🤩🤩🤩🤩 |
| [65. 有效数字](https://leetcode-cn.com/problems/valid-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/valid-number/solution/gong-shui-san-xie-zi-fu-chuan-mo-ni-by-a-7cgc/) | 困难 | 🤩🤩🤩 |
| [73. 矩阵置零](https://leetcode-cn.com/problems/set-matrix-zeroes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/valid-number/solution/gong-shui-san-xie-zi-fu-chuan-mo-ni-by-a-7cgc/) | 中等 | 🤩🤩🤩🤩 |
| [168. Excel表列名称](https://leetcode-cn.com/problems/excel-sheet-column-title/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/excel-sheet-column-title/solution/gong-shui-san-xie-cong-1-kai-shi-de-26-j-g2ur/) | 简单 | 🤩🤩🤩 |
| [190. 颠倒二进制位](https://leetcode-cn.com/problems/reverse-bits/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reverse-bits/solution/yi-ti-san-jie-dui-cheng-wei-zhu-wei-fen-ub1hi/) | 简单 | 🤩🤩🤩 |
| [263. 丑数](https://leetcode-cn.com/problems/ugly-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/ugly-number/solution/gong-shui-san-xie-jian-dan-de-fen-qing-k-dlvg/) | 简单 | 🤩🤩 |
| [566. 重塑矩阵](https://leetcode-cn.com/problems/reshape-the-matrix/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reshape-the-matrix/solution/jian-dan-ti-zhong-quan-chu-ji-ke-yi-kan-79gv5/) | 简单 | 🤩🤩🤩 |
Expand Down
77 changes: 77 additions & 0 deletions LeetCode/161-170/168. Excel表列名称(简单).md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
### 题目描述

这是 LeetCode 上的 **[168. Excel表列名称](https://leetcode-cn.com/problems/excel-sheet-column-title/solution/gong-shui-san-xie-cong-1-kai-shi-de-26-j-g2ur/)** ,难度为 **简单**。

Tag : 「模拟」

给定一个正整数,返回它在 Excel 表中相对应的列名称。

例如,
```
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB
...
```
示例 1:
```
输入: 1
输出: "A"
```
示例 2:
```
输入: 28
输出: "AB"
```
示例 3:
```
输入: 701
输出: "ZY"
```

---

### 模拟

这是一道从 $1$ 开始的的 $26$ 进制转换题。

对于一般性的进制转换题目,只需要不断地对 $columnNumber$ 进行 `%` 运算取得最后一位,然后对 $columnNumber$ 进行 `/` 运算,将已经取得的位数去掉,直到 $columnNumber$ 为 $0$ 即可。

一般性的进制转换题目无须进行额外操作,是因为我们是在「每一位数值范围在 $[0,x)$」的前提下进行「逢 $x$ 进一」。

但本题需要我们将从 $1$ 开始,因此在执行「进制转换」操作前,我们需要先对 $columnNumber$ 执行减一操作,从而实现整体偏移。

代码:
```Java []
class Solution {
public String convertToTitle(int cn) {
StringBuilder sb = new StringBuilder();
while (cn > 0) {
cn--;
sb.append((char)(cn % 26 + 'A'));
cn /= 26;
}
sb.reverse();
return sb.toString();
}
}
```
* 时间复杂度:$O(\log_{26}{cn})$
* 空间复杂度:不算构造答案所消耗的空间,复杂度为 $O(1)$

---

### 最后

这是我们「刷穿 LeetCode」系列文章的第 `No.168` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先将所有不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。

为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode

在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。

13 changes: 6 additions & 7 deletions LeetCode/811-820/815. 公交路线(困难).md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Tag : 「图论 BFS」、「双向 BFS」、「图论搜索」

### 基本分析

为了方便,我们令每辆公交站为一个「车站」,由一个「车站」可以进入一条或多条「路线」。
为了方便,我们令每个公交站为一个「车站」,由一个「车站」可以进入一条或多条「路线」。

问题为从「起点车站」到「终点车站」,所进入的最少路线为多少。

Expand Down Expand Up @@ -200,25 +200,24 @@ class Solution {

// 双向 BFS
while (!d1.isEmpty() && !d2.isEmpty()) {
int t = -1;
int res = -1;
if (d1.size() <= d2.size()) {
t = update(d1, m1, m2, t);
res = update(d1, m1, m2);
} else {
t = update(d2, m2, m1, s);
res = update(d2, m2, m1);
}
if (t != -1) return t;
if (res != -1) return res;
}

return 0x3f3f3f3f; // never
}
int update(Deque<Integer> d, Map<Integer, Integer> cur, Map<Integer, Integer> other, int target) {
int update(Deque<Integer> d, Map<Integer, Integer> cur, Map<Integer, Integer> other) {
// 取出当前所在的路线,与进入该路线所花费的距离
int poll = d.pollFirst();
int step = cur.get(poll);

// 遍历该路线所包含的车站
for (int station : rs[poll]) {
if (station == target) return step;
// 遍历将由该线路的车站发起的路线
Set<Integer> lines = map.get(station);
if (lines == null) continue;
Expand Down