---
title: '2022/09/08 Lintcode records'
disqus: hackmd
---
{%hackmd BJrTq20hE %}
2022/09/08 Lintcode records
===


## Table of Problems
:::info
**1. A+B 問題
2. 反轉一個3位整數
3. 計算圓周長和面積
4. 巴什博奕
5. 判斷數字與字母字元
6. 閏年
7. 大小寫轉換
8. 月份天數
9. 簡單計算器
10. 三數之中的最大值
11. 列印X
12. 陣列的最大值
13. 生成給定大小的陣列
14. 移動零
15. 尋找最大值
16. 交換陣列兩個元素
17. Fizz buzz問題
18. 冰雹猜想
19. 加一
20. 迴文數II**
:::
Solving process
---
### 1. A+B 問題
>不使用符號做運算的話就稍微複雜一點!
---
### 2. 反轉一個3位整數
>注意轉int的部分
---
### 3. 計算圓周長和面積
>list 基本運用
---
### 4. 巴什博奕
>注意贏的規則, 比較偏腦筋急轉彎
---
### 5. 判斷數字與字母字元
>延伸: 可以研究正則化那邊的做法
---
### 6. 閏年
>閏年定義要注意邏輯即可
---
### 7. 大小寫轉換
```python=
#小写字母 \text{a - z}a - z 的 \text{ASCII}ASCII 码范围为 [97, 122][97,122]
#大写字母 \text{A - Z}A - Z 的 \text{ASCII}ASCII 码范围为 [65, 90][65,90]
# ord以一个字符(长度为1的字符串)作为参数,返回对应的 ASCII 数值
return chr(ord(character)-32)
```
---
### 8. 月份天數
>可以不用定義兩個array, 節省空間
---
### 9. 簡單計算器
>題目沒說除法要四捨五入整數啊!
```python=
return int(eval(f'{a}{op}{b}'))
```
---
### 10. 三數之中的最大值
>python太方便了! think: 如果沒有max函數該怎麼做? 做排序:
```python=
list1=[num1,num2,num3]
# return max(list1)
for i in range(3):
for j in range(3-i-1):
if list1[j] < list1[j+1]:
list1[j],list1[j+1]=list1[j+1],list1[j]
return list1[0]
```
---
### 11. 列印X
```python=
# 思考規則!
s = []
for i in range(n):
Str = ''
for j in range(n):
if (i==j or i+j==n-1):
Str = Str + "X"
else:
Str = Str + " "
s.append(Str)
return s
# 作圖出來釐清思路很有幫助
```
---
### 12. 陣列的最大值
>逐一比對, 如果有比自己大的就替換
---
### 13. 生成給定大小的陣列
>nothing special
---
### 14. 移動零
>卡有點久! 但最後想出來了!
```python=
# 找到所有非零的整數,依序塞進array, 然後剩餘的值全填0
s = []
for i in nums:
if(i!=0):
s.append(i)
nums_len = len(nums)
s_len = len(s)
diff = nums_len - s_len
for i in range(diff):
s.append(0)
nums.clear()
nums.extend(s)
```
---
### 15. 尋找最大值
>遞迴版本值得深究
```python=
def maxNum(self, nums):
return self.max_num(nums, 0, len(nums) - 1)
def max_num(self, nums, start, end):
if start > end:
return float('-inf')
return max(nums[start], self.max_num(nums, start + 1, end))
```
---
### 16. 交換陣列兩個元素
>用一個swap作暫存
---
### 17. Fizz buzz問題
>nothing special
---
### 18. 冰雹猜想
>基本運算, nothing special
---
### 19. 加一
```python=
# 將數組轉成一個數字然後+1
Len = len(digits)-1
s = 0
for i in digits:
s += i*10**Len
Len -= 1
s += 1
# 將數字再轉回數組 >>> 這個想比較久! 算是python的特殊解?!
result = [int(x) for x in str(s)]
return result
```
---
### 20. 迴文數II
```python=
# 先得到一個整數的二進位
# 取bin之後, index=2之後才是值
num = bin(n)[2:]
# 確認是否為回文數
### num[-i-1]關鍵! 不容易想!
for i in range(len(num)):
if num[i] != num[-i-1]:
return False
return True
```
---
> Read more about lintcode here: https://www.lintcode.com/problem/
Coding flows
---
```sequence
Lintcode->Leetcode: Easy to midium and hard
Note left of Lintcode: Initial 50 + Advance 80
Note right of Leetcode: Conquer 150 problems
Note right of Leetcode: GOOGLE coding interview
Leetcode->Lintcode: Algorithm and Data Structure
Leetcode-->Google: Familiar with 500 problems up
```
> Read more about sequence-diagrams here: https://po-jen-lai.gitbook.io/coding-practice/sort
Project Timeline
---
```mermaid
gantt
title Gantt Diagram
axisFormat %m-%d
section Lintcode
Initial 50 :a1, 09-08, 3d
Advance 80 :a2, after a1, 7d
section Leetcode
Problems 150 :a3, after a2, 20d
Google 500 :after a3, 60d
```
> Read more about mermaid here: http://mermaid-js.github.io/mermaid/
## Appendix and FAQ
:::info
**Get some improvement everyday!** ...let's move on!
:::
###### tags: `Leetcode` `Lintcode`