diff --git a/source/_posts/leetcode-1-two-sum-md.md b/source/_posts/leetcode-1-two-sum-md.md new file mode 100644 index 0000000..26dfcee --- /dev/null +++ b/source/_posts/leetcode-1-two-sum-md.md @@ -0,0 +1,78 @@ +--- +title: leetcode-1-two-sum +tags: ["leetcode"] +date: 2019-11-24 21:06:37 +--- + +> 嗯,虽然以前就就在Leetcode上面刷过题,但是都没有坚持下去。这次准备重操旧业,希望能起码做到一天一道题吧。 + +首先是A+B问题,按理说是所有题目的开始。但是很遗憾的是我还是提交了三次才AC,感觉自己弱爆了。废话不多说,进入正题。 + + + +``` +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,然后输出这两个数的下标。需要注意的是每个元素只能用一次。 + +我很快就完成了,但是第一版代码是错的,代码如: + +```python3 +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],也就是说第一个元素用了两次,嗯,这么简单的边界都翻车。 + +然后很快又改好了,加了判断条件,代码如: + +```python3 +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的时候,输出是空。嗯,分支条件写错了。不应该判断两个数字都在字典里面,因为如果有两个相同的元素的话只有一个会加入到字典中。为了解决这个问题,嗯,直接换个写法就好,如: + +```python3 +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了。 + +# Refer + +[1. Two Sum](https://leetcode.com/problems/two-sum/) +# Appendix diff --git a/source/_posts/migrating-to-hexo-from-hugo-md.md b/source/_posts/migrating-to-hexo-from-hugo-md.md index 380831f..893b95a 100644 --- a/source/_posts/migrating-to-hexo-from-hugo-md.md +++ b/source/_posts/migrating-to-hexo-from-hugo-md.md @@ -43,6 +43,7 @@ Gohugo是个非常高效的静态博客生成器,而且是用我当时最痴 [Makefile git add commit push github All in One command](https://medium.com/@panjeh/makefile-git-add-commit-push-github-all-in-one-command-9dcf76220f48) +[Cross-reference (named anchor) in markdown](https://stackoverflow.com/questions/5319754/cross-reference-named-anchor-in-markdown) # Appendix ## Makefile diff --git a/source/about/index.md b/source/about/index.md index 87b7c79..ff7b7dd 100644 --- a/source/about/index.md +++ b/source/about/index.md @@ -8,4 +8,4 @@ > 面向阳光,做一个开朗自信的人。 -你好👋我的名字是d0zingcat,目前就职于[莉莉丝游戏](https://www.lilithgames.com/cn/?nlr=1),是个搬砖的码农,欢迎小伙伴拿简历来砸我。我会使用Python,Golang,Erlang进行编程,目前沉醉于Elixir无法自拔,也有计划学习一下Rust、Lua、Js。也许在未来的某天我会试着更新这个"关于我"页面。 +你好👋我的名字是d0zingcat,目前就职于[莉莉丝游戏](https://www.lilithgames.com/cn/?nlr=1),是个搬砖的码农,欢迎小伙伴拿简历来砸我。我会使用Python,Golang,Erlang, Elixir, Bash Shell 进行编程,也有计划学习一下Rust、Lua、JS。也许在未来的某天我会试着更新这个"关于我"页面。