mirror of
https://github.com/d0zingcat/infloop.life.git
synced 2026-05-16 15:09:59 +00:00
drafting some posts
This commit is contained in:
@@ -1,93 +0,0 @@
|
||||
---
|
||||
title: leetcode-1-two-sum
|
||||
tags: ["leetcode", "solution"]
|
||||
date: 2019-11-24 21:06:37
|
||||
---
|
||||
|
||||
> 嗯,虽然以前就就在Leetcode上面刷过题,但是都没有坚持下去。这次准备重操旧业,希望能起码做到一天一道题吧。
|
||||
|
||||
首先是A+B问题,按理说是所有题目的开始。但是很遗憾的是我还是提交了三次才AC,感觉自己弱爆了。废话不多说,进入正题。
|
||||
|
||||
<!--more-->
|
||||
|
||||
```
|
||||
1. Two Sum
|
||||
|
||||
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
|
||||
|
||||
You may assume that each input would have exactly one solution, and you may not use the same element twice.
|
||||
|
||||
Example:
|
||||
|
||||
Given nums = [2, 7, 11, 15], target = 9,
|
||||
|
||||
Because nums[0] + nums[1] = 2 + 7 = 9,
|
||||
return [0, 1].
|
||||
```
|
||||
|
||||
题意很简单,就是给一个list,然后给定一个target,需要从list中找到两个数的和是target,然后输出这两个数的下标。需要注意的是每个元素只能用一次,另外输出的list中元素的顺序不影响结果。
|
||||
|
||||
我很快就完成了,但是第一版代码是错的,代码如:
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def twoSum(self, nums: List[int], target: int) -> List[int]:
|
||||
m = dict()
|
||||
for i in range(len(nums)):
|
||||
if i not in m:
|
||||
m[nums[i]] = i
|
||||
if nums[i] in m and target-nums[i] in m:
|
||||
return [m[nums[i]], m[target - nums[i]]]
|
||||
return []
|
||||
```
|
||||
|
||||
致命的问题在于如果test case是[3,2,1] 6,那么输出是 [0,0],也就是说第一个元素用了两次,嗯,这么简单的边界都翻车。
|
||||
|
||||
然后很快又改好了,加了判断条件,代码如:
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def twoSum(self, nums: List[int], target: int) -> List[int]:
|
||||
m = dict()
|
||||
for i in range(len(nums)):
|
||||
if nums[i] not in m:
|
||||
m[nums[i]] = i
|
||||
if nums[i] in m and target-nums[i] in m and m[nums[i]] != m[target - nums[i]]:
|
||||
return [m[nums[i]], m[target - nums[i]]]
|
||||
return []
|
||||
```
|
||||
|
||||
然后又WA了。。。嗯,仔细分析了一下,发现test case是[3,3] 6的时候,输出是空。嗯,分支条件写错了。不应该判断两个数字都在字典里面,因为如果有两个相同的元素的话只有一个会加入到字典中。为了解决这个问题,嗯,直接换个写法就好,如:
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def twoSum(self, nums: List[int], target: int) -> List[int]:
|
||||
m = dict()
|
||||
for i in range(len(nums)):
|
||||
if nums[i] not in m:
|
||||
m[nums[i]] = i
|
||||
if target-nums[i] in m and i != m[target - nums[i]]:
|
||||
return [i, m[target - nums[i]]]
|
||||
```
|
||||
|
||||
嗯,总算AC了。我感觉每天一题的目标估计是要GG了。
|
||||
|
||||
稍微看了下题解和别人的代码,我发现这么写更加优雅:
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def twoSum(self, nums: List[int], target: int) -> List[int]:
|
||||
m = dict()
|
||||
for i, n in enumerate(nums):
|
||||
if target-n in m:
|
||||
return [m[target - n], i]
|
||||
if n not in m:
|
||||
m[n] = i
|
||||
```
|
||||
|
||||
# Refer
|
||||
|
||||
[1. Two Sum](https://leetcode.com/problems/two-sum/)
|
||||
|
||||
[1. Two Sum](https://leetcode.com/articles/two-sum/)
|
||||
# Appendix
|
||||
@@ -1,67 +0,0 @@
|
||||
---
|
||||
title: leetcode-7-reverse-integer.md
|
||||
tags: ["leetcode", "solution"]
|
||||
date: 2019-11-26 20:56:46
|
||||
---
|
||||
|
||||
7. Reverse Integer
|
||||
Given a 32-bit signed integer, reverse digits of an integer.
|
||||
Example 1:
|
||||
|
||||
Input: 123
|
||||
Output: 321
|
||||
Example 2:
|
||||
|
||||
Input: -123
|
||||
Output: -321
|
||||
Example 3:
|
||||
|
||||
Input: 120
|
||||
Output: 21
|
||||
|
||||
Note:
|
||||
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
|
||||
|
||||
<!--more-->
|
||||
|
||||
题意很简单,就是数字反转,例如123-\>321。解法也比较简单,就是每次取除10的模数(然后除10取整),原数乘10累加。例如123就是1+10*((0+3)*10+2)=321。需要注意有两个点,一个是可能数字是120000,那么就不能简单地通过字符串反转来做(算数方法做是没问题的);另一个就是有边界,边界是4字节的有符号整型数字的范围,也就是 $$[-2^{-31}, 2^{31}-1]$$。
|
||||
|
||||
我的做法比较粗暴,判断还有一位的时候用边界去减了除10,和当前数比较大小即可(其实这么做有问题,如果test case长度不是2的31次方位,比如100位的数字,那也会最后一步才会判断是否溢出,对于python这种科学计算型语言不会有问题,因为底层已经处理了大数运算。对于C、Java等传统语言,这么写就会有问题。当然,也可以上来就手动判一下数字长度,如果大于10就直接返回0,不过我没写。),也没想到居然这么写过了,代码如:
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def reverse(self, x: int) -> int:
|
||||
n = 0
|
||||
flag = 1
|
||||
if x < 0:
|
||||
flag = -1
|
||||
x = -x
|
||||
while x > 0:
|
||||
if x < 10:
|
||||
if flag == 1 and n > ((1<<31)-1-x)/10 or flag == -1 and n > ((1<<31)-1)/10:
|
||||
return 0
|
||||
n = n * 10 + x % 10
|
||||
x = x // 10
|
||||
return flag * n
|
||||
```
|
||||
|
||||
|
||||
|
||||
然后就是参考题解了。发现有两个地方可以改进代码。一个其实不用一开始就把负数转换为正数,因为带符号数累加之后符号还是在的;还有一个是判断条件可以做修改为判断为当前数是不是大于MAXINT/10,因为如果大于的话那么下一次x10就必然溢出;如果等于那么判断尾数是不是大于等于8(正数时)、小于等于-9(负数时)。
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# Refer
|
||||
|
||||
[7. Reverse Integer](https://leetcode.com/problems/reverse-integer/)
|
||||
|
||||
[7. Reverse Integer](https://leetcode.com/articles/reverse-integer/)
|
||||
|
||||
# Appendix
|
||||
Reference in New Issue
Block a user