Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 81335bc

Browse files
add some code
1 parent 4c42afb commit 81335bc

File tree

6 files changed

+145
-0
lines changed

6 files changed

+145
-0
lines changed

‎python/494_TargetSum.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# @Time : 2019年3月4日 2:41 PM
4+
# @Author : huxiaoman
5+
# @File : 494_TargetSum.py
6+
# @Package : LeetCode
7+
# @E-mail : charlotte77_hu@sina.com
8+
9+
class Solution(object):
10+
def findTargetSumWays(self, nums, S):
11+
"""
12+
:type nums: List[int]
13+
:type S: int
14+
:rtype: int
15+
"""
16+
if not nums:
17+
return 0
18+
dic = {nums[0]: 1, -nums[0]: 1} if nums[0] != 0 else {0: 2}
19+
print dic
20+
for i in range(1, len(nums)):
21+
tdic = {}
22+
for d in dic:
23+
tdic[d + nums[i]] = tdic.get(d + nums[i], 0) + dic.get(d, 0)
24+
tdic[d - nums[i]] = tdic.get(d - nums[i], 0) + dic.get(d, 0)
25+
dic = tdic
26+
return dic.get(S, 0)
27+
28+
if __name__=='__main__':
29+
s = Solution()
30+
print s.findTargetSumWays([1,1,1,1,1],3)

‎python/5_LongestPanlindromicSubstring.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,31 @@
55
# @File : 5_LongestPanlindromicSubstring.py
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
8+
9+
class Solution(object):
10+
"""
11+
:type s: str
12+
:rtype: str
13+
"""
14+
def longestPalindrome(self, s):
15+
res = ""
16+
for i in xrange(len(s)):
17+
tmp = self.helper(s, i, i)
18+
if len(tmp) > len(res):
19+
res = tmp
20+
tmp = self.helper(s, i, i+1)
21+
if len(tmp) > len(res):
22+
res = tmp
23+
return res
24+
25+
26+
def helper(self, s, l, r):
27+
while l >= 0 and r < len(s) and s[l] == s[r]:
28+
l -= 1; r += 1
29+
return s[l+1:r]
30+
31+
32+
if __name__ == '__main__':
33+
s = Solution()
34+
print s.longestPalindrome("babad")
35+

‎python/6_ZigZagConversion.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,31 @@
55
# @File : 6_ZigZagConversion.py
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
8+
9+
10+
class Solution(object):
11+
def convert(self, s, numRows):
12+
"""
13+
:type s: str
14+
:type numRows: int
15+
:rtype: str
16+
"""
17+
if numRows == 1 or numRows >= len(s):
18+
return s
19+
20+
L = [''] * numRows
21+
index, step = 0, 1
22+
23+
for x in s:
24+
L[index] += x
25+
if index == 0:
26+
step = 1
27+
elif index == numRows -1:
28+
step = -1
29+
index += step
30+
31+
return ''.join(L)
32+
33+
if __name__ == '__main__':
34+
s = Solution()
35+
print s.convert("PAYPALISHIRING",3)

‎python/7_ReverseInteger.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,22 @@
55
# @File : 7_ReverseInteger.py
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
8+
9+
10+
class Solution(object):
11+
def reverse(self, x):
12+
"""
13+
:type x: int
14+
:rtype: int
15+
"""
16+
flag = 1 if x>=0 else -1
17+
new ,x = 0, abs(x)
18+
while x:
19+
new = 10 *new + x % 10
20+
x /= 10
21+
new = flag * new
22+
return new if new < 2147483648 and new >= -2147483648 else 0
23+
24+
if __name__ == '__main__':
25+
s = Solution()
26+
print s.reverse(3213424)

‎python/8_StringToInteger(aoti).py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,25 @@
55
# @File : 8_StringToInteger(aoti).py
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
8+
9+
import re
10+
11+
class Solution(object):
12+
def myAtoi(self, str):
13+
"""
14+
:type str: str
15+
:rtype: int
16+
"""
17+
str = str.strip()
18+
try:
19+
res = re.search('(^[\+\-]?\d+)', str).group()
20+
res = int(res)
21+
res = res if res <= 2147483647 else 2147483647
22+
res = res if res >= -2147483648 else -2147483648
23+
except:
24+
res = 0
25+
return res
26+
27+
if __name__ == '__main__':
28+
s = Solution()
29+
print s.myAtoi("4232")

‎python/9_PanlindromeNumber.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,21 @@
55
# @File : 9_PanlindromeNumber.py
66
# @Package : LeetCode
77
# @E-mail : charlotte77_hu@sina.com
8+
9+
class Solution(object):
10+
def isPalindrome(self, x):
11+
"""
12+
:type x: int
13+
:rtype: bool
14+
"""
15+
if x<0:
16+
return False
17+
elif x>=0 :
18+
if str(x) == str(x)[::-1]:
19+
return True
20+
else:
21+
return False
22+
23+
if __name__ =='__main__':
24+
s = Solution()
25+
print s.isPalindrome(32123)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /