1 Star 0 Fork 0

yuhang2__2/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
array-partition-i.py 1.05 KB
一键复制 编辑 原始数据 按行查看 历史
Allen Liu 提交于 7年前 . remove unused code
# Time: O(r), r is the range size of the integers
# Space: O(r)
class Solution(object):
def arrayPairSum(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
LEFT, RIGHT = -10000, 10000
lookup = [0] * (RIGHT-LEFT+1)
for num in nums:
lookup[num-LEFT] += 1
r, result = 0, 0
for i in xrange(LEFT, RIGHT+1):
result += (lookup[i-LEFT] + 1 - r) / 2 * i
r = (lookup[i-LEFT] + r) % 2
return result
# Time: O(nlogn)
# Space: O(1)
class Solution2(object):
def arrayPairSum(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums.sort()
result = 0
for i in xrange(0, len(nums), 2):
result += nums[i]
return result
# Time: O(nlogn)
# Space: O(n)
class Solution3(object):
def arrayPairSum(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums = sorted(nums)
return sum([nums[i] for i in range(0, len(nums), 2)])
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yuhang2__2/LeetCode-Solutions.git
git@gitee.com:yuhang2__2/LeetCode-Solutions.git
yuhang2__2
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助