嗨大家好~我是凱西!接下來是我開學的三十天實力增進計畫的紀錄
規劃上會刷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)
那就先這樣!
明天再寫些厲害的!!