Skip to content

Commit f85b5a7

Browse files
committed
commit solution 171 205
1 parent 73c432d commit f85b5a7

File tree

10 files changed

+218
-4
lines changed

10 files changed

+218
-4
lines changed

Diff for: index-tags.md

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
| [13](https://leetcode-cn.com/problems/roman-to-integer) | [罗马数字转整数](/solution/1-99/0013.roman-to-integer/) | `数学`,`字符串` | <font color=green>简单</font> ||
5454
| [50](https://leetcode-cn.com/problems/powx-n) | [pow(x, n)](/solution/1-99/0050.pow%28x%2c-n%29/) | `数学`,`二分查找` | <font color=blue>中等</font> ||
5555
| [69](https://leetcode-cn.com/problems/sqrtx) | [x 的平方根](/solution/1-99/0069.sqrt%28x%29/) | `数学`,`二分查找` | <font color=green>简单</font> ||
56+
| [171](https://leetcode-cn.com/problems/excel-sheet-column-number) | [excel表列序号](/solution/100-199/0171.excel-sheet-column-number/) | `数学` | <font color=green>简单</font> ||
5657
| [202](https://leetcode-cn.com/problems/happy-number) | [快乐数](/solution/200-299/0202.happy-number/) | `哈希表`,`数学` | <font color=green>简单</font> ||
5758
| [204](https://leetcode-cn.com/problems/count-primes) | [计数质数](/solution/200-299/0204.count-primes/) | `哈希表`,`数学` | <font color=green>简单</font> ||
5859
| [268](https://leetcode-cn.com/problems/missing-number) | [缺失数字](/solution/200-299/0268.missing-number/) | `位运算`,`数组`,`数学` | <font color=green>简单</font> ||
@@ -68,6 +69,7 @@
6869
| [187](https://leetcode-cn.com/problems/repeated-dna-sequences) | [重复的dna序列](/solution/100-199/0187.repeated-dna-sequences/) | `位运算`,`哈希表` | <font color=blue>中等</font> ||
6970
| [202](https://leetcode-cn.com/problems/happy-number) | [快乐数](/solution/200-299/0202.happy-number/) | `哈希表`,`数学` | <font color=green>简单</font> ||
7071
| [204](https://leetcode-cn.com/problems/count-primes) | [计数质数](/solution/200-299/0204.count-primes/) | `哈希表`,`数学` | <font color=green>简单</font> ||
72+
| [205](https://leetcode-cn.com/problems/isomorphic-strings) | [同构字符串](/solution/200-299/0205.isomorphic-strings/) | `哈希表` | <font color=green>简单</font> ||
7173
| [217](https://leetcode-cn.com/problems/contains-duplicate) | [存在重复元素](/solution/200-299/0217.contains-duplicate/) | `数组`,`哈希表` | <font color=green>简单</font> ||
7274
| [219](https://leetcode-cn.com/problems/contains-duplicate-ii) | [存在重复元素 ii](/solution/200-299/0219.contains-duplicate-ii/) | `数组`,`哈希表` | <font color=green>简单</font> ||
7375

Diff for: index-type.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
| [168](https://leetcode-cn.com/problems/excel-sheet-column-title) | [excel表列名称](/solution/100-199/0168.excel-sheet-column-title/) | `数学` | <font color=green>简单</font> |
5050
| [169](https://leetcode-cn.com/problems/majority-element) | [多数元素](/solution/100-199/0169.majority-element/) | `位运算`,`数组`,`分治算法` | <font color=green>简单</font> ||
5151
| [170](https://leetcode-cn.com/problems/two-sum-iii-data-structure-design) | [两数之和 iii - 数据结构设计](/solution/100-199/0170.two-sum-iii---data-structure-design/) | `设计`,`哈希表` | <font color=green>简单</font> |
52-
| [171](https://leetcode-cn.com/problems/excel-sheet-column-number) | [excel表列序号](/solution/100-199/0171.excel-sheet-column-number/) | `数学` | <font color=green>简单</font> |
52+
| [171](https://leetcode-cn.com/problems/excel-sheet-column-number) | [excel表列序号](/solution/100-199/0171.excel-sheet-column-number/) | `数学` | <font color=green>简单</font> ||
5353
| [172](https://leetcode-cn.com/problems/factorial-trailing-zeroes) | [阶乘后的零](/solution/100-199/0172.factorial-trailing-zeroes/) | `数学` | <font color=green>简单</font> |
5454
| [175](https://leetcode-cn.com/problems/combine-two-tables) | [组合两个表](/solution/100-199/0175.combine-two-tables/) | | <font color=green>简单</font> |
5555
| [176](https://leetcode-cn.com/problems/second-highest-salary) | [第二高的薪水](/solution/100-199/0176.second-highest-salary/) | | <font color=green>简单</font> |
@@ -67,7 +67,7 @@
6767
| [202](https://leetcode-cn.com/problems/happy-number) | [快乐数](/solution/200-299/0202.happy-number/) | `哈希表`,`数学` | <font color=green>简单</font> ||
6868
| [203](https://leetcode-cn.com/problems/remove-linked-list-elements) | [移除链表元素](/solution/200-299/0203.remove-linked-list-elements/) | `链表` | <font color=green>简单</font> |
6969
| [204](https://leetcode-cn.com/problems/count-primes) | [计数质数](/solution/200-299/0204.count-primes/) | `哈希表`,`数学` | <font color=green>简单</font> ||
70-
| [205](https://leetcode-cn.com/problems/isomorphic-strings) | [同构字符串](/solution/200-299/0205.isomorphic-strings/) | `哈希表` | <font color=green>简单</font> |
70+
| [205](https://leetcode-cn.com/problems/isomorphic-strings) | [同构字符串](/solution/200-299/0205.isomorphic-strings/) | `哈希表` | <font color=green>简单</font> ||
7171
| [206](https://leetcode-cn.com/problems/reverse-linked-list) | [反转链表](/solution/200-299/0206.reverse-linked-list/) | `链表` | <font color=green>简单</font> |
7272
| [217](https://leetcode-cn.com/problems/contains-duplicate) | [存在重复元素](/solution/200-299/0217.contains-duplicate/) | `数组`,`哈希表` | <font color=green>简单</font> ||
7373
| [219](https://leetcode-cn.com/problems/contains-duplicate-ii) | [存在重复元素 ii](/solution/200-299/0219.contains-duplicate-ii/) | `数组`,`哈希表` | <font color=green>简单</font> ||
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# [171. Excel表列序号](https://leetcode-cn.com/problems/excel-sheet-column-number/description/)
2+
3+
### 题目描述
4+
5+
<p>给定一个Excel表格中的列名称,返回其相应的列序号。</p>
6+
7+
<p>例如,</p>
8+
9+
<pre> A -&gt; 1
10+
B -&gt; 2
11+
C -&gt; 3
12+
...
13+
Z -&gt; 26
14+
AA -&gt; 27
15+
AB -&gt; 28
16+
...
17+
</pre>
18+
19+
<p><strong>示例 1:</strong></p>
20+
21+
<pre><strong>输入:</strong> &quot;A&quot;
22+
<strong>输出:</strong> 1
23+
</pre>
24+
25+
<p><strong>示例&nbsp;2:</strong></p>
26+
27+
<pre><strong>输入: </strong>&quot;AB&quot;
28+
<strong>输出:</strong> 28
29+
</pre>
30+
31+
<p><strong>示例&nbsp;3:</strong></p>
32+
33+
<pre><strong>输入: </strong>&quot;ZY&quot;
34+
<strong>输出:</strong> 701</pre>
35+
36+
<p><strong>致谢:</strong><br>
37+
特别感谢&nbsp;<a href="http://leetcode.com/discuss/user/ts">@ts</a>&nbsp;添加此问题并创建所有测试用例。</p>
38+
39+
### 解题思路
40+
41+
### 具体解法
42+
43+
#### **Golang**
44+
```go
45+
func titleToNumber(s string) int {
46+
res := 0
47+
for _, char := range s {
48+
res *= 26
49+
res += int(char-'A') + 1
50+
}
51+
return res
52+
}
53+
```
54+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=171 lang=golang
5+
*
6+
* [171] Excel表列序号
7+
*/
8+
9+
// @lc code=start
10+
func titleToNumber(s string) int {
11+
res := 0
12+
for _, char := range s {
13+
res *= 26
14+
res += int(char-'A') + 1
15+
}
16+
return res
17+
}
18+
19+
// @lc code=end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestTitleToNumber(t *testing.T) {
8+
var ret int
9+
var s string
10+
ret = 1
11+
s = "A"
12+
if ret != titleToNumber(s) {
13+
t.Fatalf("case fails %v\n", ret)
14+
}
15+
16+
ret = 28
17+
s = "AB"
18+
if ret != titleToNumber(s) {
19+
t.Fatalf("case fails %v\n", ret)
20+
}
21+
22+
ret = 701
23+
s = "ZY"
24+
if ret != titleToNumber(s) {
25+
t.Fatalf("case fails %v\n", ret)
26+
}
27+
28+
}

Diff for: solution/100-199/_sidebar.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
- [168. excel表列名称](solution/100-199/0168.excel-sheet-column-title/)
7878
- [169. 多数元素 ✅](solution/100-199/0169.majority-element/)
7979
- [170. 两数之和 iii - 数据结构设计](solution/100-199/0170.two-sum-iii---data-structure-design/)
80-
- [171. excel表列序号](solution/100-199/0171.excel-sheet-column-number/)
80+
- [171. excel表列序号](solution/100-199/0171.excel-sheet-column-number/)
8181
- [172. 阶乘后的零](solution/100-199/0172.factorial-trailing-zeroes/)
8282
- [173. 二叉搜索树迭代器](solution/100-199/0173.binary-search-tree-iterator/)
8383
- [174. 地下城游戏](solution/100-199/0174.dungeon-game/)

Diff for: solution/200-299/0205.isomorphic-strings/README.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# [205. 同构字符串](https://leetcode-cn.com/problems/isomorphic-strings/description/)
2+
3+
### 题目描述
4+
5+
<p>给定两个字符串&nbsp;<em><strong>s&nbsp;</strong></em>和&nbsp;<strong><em>t</em></strong>,判断它们是否是同构的。</p>
6+
7+
<p>如果&nbsp;<em><strong>s&nbsp;</strong></em>中的字符可以被替换得到&nbsp;<strong><em>t&nbsp;</em></strong>,那么这两个字符串是同构的。</p>
8+
9+
<p>所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。两个字符不能映射到同一个字符上,但字符可以映射自己本身。</p>
10+
11+
<p><strong>示例 1:</strong></p>
12+
13+
<pre><strong>输入:</strong> <strong><em>s</em></strong> = <code>&quot;egg&quot;, </code><strong><em>t = </em></strong><code>&quot;add&quot;</code>
14+
<strong>输出:</strong> true
15+
</pre>
16+
17+
<p><strong>示例 2:</strong></p>
18+
19+
<pre><strong>输入:</strong> <strong><em>s</em></strong> = <code>&quot;foo&quot;, </code><strong><em>t = </em></strong><code>&quot;bar&quot;</code>
20+
<strong>输出:</strong> false</pre>
21+
22+
<p><strong>示例 3:</strong></p>
23+
24+
<pre><strong>输入:</strong> <strong><em>s</em></strong> = <code>&quot;paper&quot;, </code><strong><em>t = </em></strong><code>&quot;title&quot;</code>
25+
<strong>输出:</strong> true</pre>
26+
27+
<p><strong>说明:</strong><br>
28+
你可以假设&nbsp;<em><strong>s&nbsp;</strong></em>和 <strong><em>t </em></strong>具有相同的长度。</p>
29+
30+
### 解题思路
31+
32+
33+
### 具体解法
34+
35+
1. 设置`s``t``hash table`
36+
2. 遍历随意一个字符串
37+
3. 将遍历到的字符串的当作`key`, 字符串下标 `i+1` 当作`value`保存在`map`
38+
4. 判断`map`中相同下标的字符串的对应下标位置是否相同
39+
40+
#### **Golang**
41+
```go
42+
func isIsomorphic(s string, t string) bool {
43+
sMap, tMap := map[uint8]int{}, map[uint8]int{}
44+
for i := range s {
45+
if sMap[s[i]] != tMap[t[i]] {
46+
return false
47+
} else {
48+
sMap[s[i]] = i + 1
49+
tMap[t[i]] = i + 1
50+
}
51+
}
52+
return true
53+
}
54+
```
55+
56+

Diff for: solution/200-299/0205.isomorphic-strings/solution.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=205 lang=golang
5+
*
6+
* [205] 同构字符串
7+
*/
8+
9+
// @lc code=start
10+
func isIsomorphic(s string, t string) bool {
11+
sMap, tMap := map[uint8]int{}, map[uint8]int{}
12+
for i := range s {
13+
if sMap[s[i]] != tMap[t[i]] {
14+
return false
15+
} else {
16+
sMap[s[i]] = i + 1
17+
tMap[t[i]] = i + 1
18+
}
19+
}
20+
return true
21+
}
22+
23+
// @lc code=end
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestIsIsomorphic(t *testing.T) {
8+
var ret bool
9+
var s string
10+
var ts string
11+
12+
ts = "add"
13+
s = "egg"
14+
ret = true
15+
if ret != isIsomorphic(s, ts) {
16+
t.Fatalf("case fails %v\n", ret)
17+
}
18+
19+
ts = "bar"
20+
s = "foo"
21+
ret = false
22+
if ret != isIsomorphic(s, ts) {
23+
t.Fatalf("case fails %v\n", ret)
24+
}
25+
26+
ts = "title"
27+
s = "paper"
28+
ret = true
29+
if ret != isIsomorphic(s, ts) {
30+
t.Fatalf("case fails %v\n", ret)
31+
}
32+
}

Diff for: solution/200-299/_sidebar.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
- [202. 快乐数 ✅](solution/200-299/0202.happy-number/)
1212
- [203. 移除链表元素](solution/200-299/0203.remove-linked-list-elements/)
1313
- [204. 计数质数 ✅](solution/200-299/0204.count-primes/)
14-
- [205. 同构字符串](solution/200-299/0205.isomorphic-strings/)
14+
- [205. 同构字符串](solution/200-299/0205.isomorphic-strings/)
1515
- [206. 反转链表](solution/200-299/0206.reverse-linked-list/)
1616
- [207. 课程表](solution/200-299/0207.course-schedule/)
1717
- [208. 实现 trie (前缀树)](solution/200-299/0208.implement-trie-prefix-tree/)

0 commit comments

Comments
 (0)