首先补上昨天问题的Java实现,不多说,直接上代码
class Solution {
public int[] twoSum(int[] nums, int target) {
int arr[] = new int[2];
for (int i=0; i<nums.length-1; i++){
for (int j=i+1; j<nums.length; j++){
if ((nums[i] + nums[j]) == target){
arr[0] = i;
arr[1] = j;
return arr;
}
}
}
return null;
}
}
今天也是做了一个简单的问题,从简单到困难,一步一步来,好了,上题目:

import math
class Solution(object):
def reverse(self, x):
shuzi = str(x) # 将数字转为字符串
fuhao = ''
if shuzi[:1] == '-': # 判断是正数还是负数,负数将第一个的符号截取下来
fuhao = '-'
shuzi = shuzi[1:]
shuzi = shuzi[::-1] # 字符串反转
shuzi = fuhao + shuzi
if math.pow(-2,31) <= int(shuzi) < math.pow(2,31): # 判断是否溢出
return int(shuzi)
else:
return 0
简单的实现了,运行花费时间是36 ms,还算过关,不知道有没有更好的方法去实现,找找吧。