Leetcode Contest 33

class Solution(object):
def findLHS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
cnt = {}
for n in nums:
cnt[n] = cnt.get(n, 0) + 1
ans = 0
for k, v in cnt.items():
if k-1 in cnt:
ans = max(ans, v+cnt[k-1])
if k+1 in cnt:
ans = max(ans, v+cnt[k+1])
return ans


Leetcode Contest 32

class Solution(object):
def findUnsortedSubarray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
sn = sorted(nums)
i = 0
while i < len(nums) and nums[i] == sn[i]:
i += 1
if i == len(nums):
return 0
j = len(nums) - 1
while j >= 0 and nums[j] == sn[j]:
j -= 1
return j-i+1


编写Hexo插件让主页显示特定的文章

(I) 让主页只显示特定文章（或者说过滤掉特定文章）。

(II) 建立一个页面只显示特定文章。

Leetcode Contest 31

class Solution(object):
def distributeCandies(self, candies):
"""
:type candies: List[int]
:rtype: int
"""
kind = set()
for c in candies:
n = len(candies)/2
if (n<=len(kind)):
return n
else:
return len(kind)


Leetcode Contest 30

class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
int n = nums.size();
if (n==0) return nums;
int m = nums[0].size();
if (m==0) return nums;
if (n*m != r*c) return nums;
vector<vector<int>> ans(r, vector<int>(c, 0));
int a = 0, b = 0;
for (auto row: nums)
for (auto n: row) {
ans[a][b] = n;
b ++;
if (b == c) {
b = 0;
a ++;
}
}
return ans;
}
};


Leetcode Contest 29

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
def findTilt(self, root):
"""
:type root: TreeNode
:rtype: int
"""
ans = [0]

def dfs(root):
if not root:
return 0
left = dfs(root.left)
right = dfs(root.right)
ans[0] += abs(left-right)
return left+right+root.val

dfs(root)
return ans[0]


Leetcode Contest 28

class Solution(object):
def checkRecord(self, s):
"""
:type s: str
:rtype: bool
"""
a = 0
for c in s:
if c == 'A':
a += 1
if a > 1:
return False
if s.find('LLL') != -1:
return False
return True


Leetcode Contest 27

class Solution(object):
def reverseWords(self, s):
"""
:type s: str
:rtype: str
"""
lst = s.split(' ')
for i in xrange(len(lst)):
if lst[i] == '':
continue
lst[i] = ''.join(reversed(lst[i]))
return ' '.join(lst)


Cabaret

Cabaret是一部音乐剧，一部充满黄段子的音乐剧。Cabaret一开幕我就明白朋友路上说的少儿不宜是什么意思了。虽然我并不能完全明白剧中的对话（估计错过了好多黄段子 😛 ），但是剧中的大致剧情还是明白的。这里我就不剧透了，有兴趣的朋友也很容易在网上找到简介。一开始根据剧中反映的性泛滥以及对金钱的崇拜，我还以为是说战后经济大低谷时期的事情。后来剧中出现了纳粹红袖章我才意识到自己搞错了。原来剧中的人们正活着1928～1930期间，正是纳粹主义开始猖獗的时候。

Leetcode Contest 25

class Solution(object):
def checkPerfectNumber(self, num):
"""
:type num: int
:rtype: bool
"""
if num <= 1:
return False

s = 1
for i in xrange(2, int(num**(0.5))+1):
if num % i == 0:
s += i
if i != num/i:
s += num/i
return s == num