or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing
xxxxxxxxxx
golang 鐵人賽 自我挑戰賽 leetcode 30 天 第6天 number-of-steps-to-reduce-a-number-to-zero
題目解讀:
題目來源:
number-of-steps-to-reduce-a-number-to-zero
原文:
Given a non-negative integer num, return the number of steps to reduce it to zero. If the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it.
解讀:
給定一個正整數 num
依據以下的法則num遞減到 0
1 如果 num 是偶數 則讓 num = num/2
2 如果 num 不是偶數 則讓 num = num -1
寫一個function 計算出所需要的步驟數目
初步解法:
初步觀察:
首先是需要把每個步驟利用一個counter記錄下來
每做一步 把 counter加一
初步設計:
Given an integer num, num > 0
step 1: set a integer count = 0
step 2: check if num == 0 go to step 6
step 3: check if num is even , num = num /2
step 4: check if num is not even, num = num - 1
step 5: count = count + 1 go to step 2
step 6: return count
遇到的困難
題目上理解的問題
因為英文不是筆者母語
所以在題意解讀上 容易被英文用詞解讀給搞模糊
pseudo code撰寫
一開始不習慣把pseudo code寫下來
因此 不太容易把自己的code做解析
golang table driven test不熟
對於table driven test還不太熟析
所以對於寫test還是耗費不少時間
我的github source code
numberOfSteps.go
測資的撰寫
參考文章
golang test