iT邦幫忙

2021 iThome 鐵人賽

DAY 1
0
自我挑戰組

開學之前....系列 第 1

Day1-介紹與開始

嗨大家好~我是凱西!接下來是我開學的三十天實力增進計畫的紀錄

規劃上會刷leetcode加強我的python、學習一些機器學習的知識
另外再分享一些其他我學到的東西!

先上第一天的暖身題目:

7. Reverse Integer(EASY)

Example 1:
Input: x = 123
Output: 321

Example 2:
Input: x = -123
Output: -321

Example 3:
Input: x = 120
Output: 21

Example 4:
Input: x = 0
Output: 0

constraints:
-231 <= x <= 231 - 1

以下是我的code:

def reverse(x):
    out = []
    negative = 0
    num = str(x)
    length = len(num)

    for i in range(length):

        if ord(num[i]) == 45:
            negative = 1
        else:
            out.append(num[i])

    out.reverse()
    out = "".join(out)

    #is negative and start with 0
    if (negative ==1) and (out[0]=='0'):
        out = -int(out[1:])
    elif (negative ==1) and (out[0]!='0'):
        out = -int(out)
    # start with 0 but not zero
    elif (out != '0') and (out[0] == '0'):
        out = int(out[1:])
    else:
        out = int(out)

    print(out)

那就先這樣!
明天再寫些厲害的!!


下一篇
Day2-LeetCode 118. Pascal's Triangle
系列文
開學之前....20
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 則留言

1
阿瑜
iT邦研究生 4 級 ‧ 2021-09-17 00:01:13

為什這個人連寫文章都很有趣?!

0
阿瑜
iT邦研究生 4 級 ‧ 2021-09-17 00:03:09
#我示範個程式碼標記功能
print("凱西最後幾分鐘的發文的簽到")

我要留言

立即登入留言