Skip to content

Commit 705f520

Browse files
committed
0610
1 parent 6c1df67 commit 705f520

18 files changed

+695
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
3+
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
4+
5+
Example:
6+
Given a = 1 and b = 2, return 3.
7+
8+
Credits:
9+
Special thanks to @fujiaozhu for adding this problem and creating all test cases.
10+
11+
"""
12+
13+
"""
14+
思路:强大的位运算,不能用+ and -,考虑位运算,按位加
15+
那么 1+1=10相当于 按位与再左移1位,1+0=1相当于按位异或,所以 按照这个思路进行迭代
16+
17+
"""
18+
"""但是,这个对python不适用!,当输入负数时,会超时"""
19+
class Solution(object):
20+
def getSum(self, a, b):
21+
"""
22+
:type a: int
23+
:type b: int
24+
:rtype: int
25+
"""
26+
if a==0:
27+
return b
28+
if b==0:
29+
return a
30+
while b:
31+
carry = a & b
32+
a = a ^ b
33+
b = carry << 1
34+
return a
35+
36+
37+
"""python solution"""
38+
class Solution(object):
39+
def getSum(self, a, b):
40+
"""
41+
:type a: int
42+
:type b: int
43+
:rtype: int
44+
"""
45+
# 32 bits integer max
46+
MAX = 0x7FFFFFFF
47+
# 32 bits interger min
48+
MIN = 0x80000000
49+
# mask to get last 32 bits
50+
mask = 0xFFFFFFFF
51+
while b != 0:
52+
# ^ get different bits and & gets double 1s, << moves carry
53+
a, b = (a ^ b) & mask, ((a & b) << 1) & mask
54+
# if a is negative, get a's 32 bits complement positive first
55+
# then get 32-bit positive's Python complement negative
56+
"""有疑问,python把有符号数当作无符号数处理"""
57+
return a if a <= MAX else ~(a ^ mask)
202 KB
Binary file not shown.

easy/二进制/test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def getSum( a, b):
2+
"""
3+
:type a: int
4+
:type b: int
5+
:rtype: int
6+
"""
7+
# 32 bits integer max
8+
MAX = 0x7FFFFFFF
9+
# 32 bits interger min
10+
MIN = 0x80000000
11+
# mask to get last 32 bits
12+
mask = 0xFFFFFFFF
13+
while b != 0:
14+
# ^ get different bits and & gets double 1s, << moves carry
15+
a, b = (a ^ b) & mask, ((a & b) << 1) & mask
16+
# if a is negative, get a's 32 bits complement positive first
17+
# then get 32-bit positive's Python complement negative
18+
return a if a <= MAX else ~(a ^ mask)
19+
20+
mask = 0xFFFFFFFF
21+
print (getSum(-1,-2))
22+
print (~(4294967293^ mask))

easy/字符串/383_Ransom Note.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
3+
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
4+
5+
Each letter in the magazine string can only be used once in your ransom note.
6+
7+
Note:
8+
You may assume that both strings contain only lowercase letters.
9+
10+
canConstruct("a", "b") -> false
11+
canConstruct("aa", "ab") -> false
12+
canConstruct("aa", "aab") -> true
13+
Subscribe to see which companies asked this question.
14+
15+
"""
16+
"""
17+
solution:利用counter的计数功能
18+
"""
19+
20+
import collections
21+
22+
class Solution(object):
23+
def canConstruct(self, ransomNote, magazine):
24+
"""
25+
:type ransomNote: str
26+
:type magazine: str
27+
:rtype: bool
28+
"""
29+
return not collections.Counter(ransomNote) - collections.Counter(magazine)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
3+
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
4+
5+
Examples:
6+
7+
s = "leetcode"
8+
return 0.
9+
10+
s = "loveleetcode",
11+
return 2.
12+
Note: You may assume the string contain only lowercase letters.
13+
14+
"""
15+
import string
16+
17+
class Solution(object):
18+
def firstUniqChar(self, s):
19+
"""
20+
:type s: str
21+
:rtype: int
22+
"""
23+
return min([s.find(c) for c in string.ascii_lowercase if s.count(c)==1] or [-1])
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
3+
Given two strings s and t which consist of only lowercase letters.
4+
5+
String t is generated by random shuffling string s and then add one more letter at a random position.
6+
7+
Find the letter that was added in t.
8+
9+
Example:
10+
11+
Input:
12+
s = "abcd"
13+
t = "abcde"
14+
15+
Output:
16+
e
17+
18+
Explanation:
19+
'e' is the letter that was added.
20+
21+
"""
22+
23+
"""my solution:两个字符串只有一个值不同,类似于之前的一道题,一个数组中所有数字都出现了两次,只有一个数出现了一次,对数组中所有的数进行一次异或操作即可"""
24+
25+
class Solution(object):
26+
def findTheDifference(self, s, t):
27+
"""
28+
:type s: str
29+
:type t: str
30+
:rtype: str
31+
"""
32+
c = s+t
33+
res = 0
34+
for single_char in c:
35+
res = res ^ ord(single_char)
36+
return chr(res)
37+
38+
"""可以写的更精炼"""
39+
40+
41+
class Solution(object):
42+
def findTheDifference(self, s, t):
43+
"""
44+
:type s: str
45+
:type t: str
46+
:rtype: str
47+
"""
48+
return chr(reduce(operator.xor, map(ord, (s + t))))
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
3+
Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.
4+
5+
This is case sensitive, for example "Aa" is not considered a palindrome here.
6+
7+
Note:
8+
Assume the length of given string will not exceed 1,010.
9+
10+
Example:
11+
12+
Input:
13+
"abccccdd"
14+
15+
Output:
16+
7
17+
18+
Explanation:
19+
One longest palindrome that can be built is "dccaccd", whose length is 7.
20+
21+
"""
22+
23+
"""
24+
my solution:结果分为三部分,计数数量为正数的,随后计数为单数的-1,最后,如果有单数元素存在,长度肯定可以加1
25+
这里注意一个testcase "ccc",应该返回3,瑞后的max要注意,只要有计数为单数的字符,都要加上1,而不是只有计数为1的才行
26+
27+
"""
28+
29+
import collections
30+
class Solution(object):
31+
def longestPalindrome(self, s):
32+
"""
33+
:type s: str
34+
:rtype: int
35+
"""
36+
t = collections.Counter(s)
37+
return sum([t[x] for x in t if t[x] % 2 == 0]) + sum([t[x] - 1 for x in t if t[x] % 2 == 1]) + max(
38+
[1 for x in t if t[x] % 2 == 1] or [0])

easy/字符串/415_Add Strings.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
3+
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.
4+
5+
Note:
6+
7+
The length of both num1 and num2 is < 5100.
8+
Both num1 and num2 contains only digits 0-9.
9+
Both num1 and num2 does not contain any leading zero.
10+
You must not use any built-in BigInteger library or convert the inputs to integer directly.
11+
12+
"""
13+
14+
"""my solution
15+
从最后一位往前加,其实比较繁琐
16+
"""
17+
18+
19+
class Solution(object):
20+
def addStrings(self, num1, num2):
21+
"""
22+
:type num1: str
23+
:type num2: str
24+
:rtype: str
25+
"""
26+
extra = 0
27+
sum = ''
28+
for i in range(1,min(len(num1),len(num2))+1):
29+
sum = str((ord(num1[-i])+ord(num2[-i])-96+extra)%10)+sum
30+
extra = (ord(num1[-i])+ord(num2[-i])+extra-96)/10
31+
if len(num1) > i:
32+
for j in range(i+1,len(num1)+1):
33+
sum = str((ord(num1[-j])-48+extra)%10)+sum
34+
extra = (ord(num1[-j])+extra-48)/10
35+
if len(num2) > i:
36+
for j in range(i+1,len(num2)+1):
37+
sum = str((ord(num2[-j])-48+extra)%10)+sum
38+
extra = (ord(num2[-j])+extra-48)/10
39+
if extra>0:
40+
sum = str(extra)+sum
41+
return sum
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
3+
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.
4+
5+
Please note that the string does not contain any non-printable characters.
6+
7+
Example:
8+
9+
Input: "Hello, my name is John"
10+
Output: 5
11+
12+
13+
"""
14+
15+
"""要考虑连续空格的情形"""
16+
17+
18+
class Solution(object):
19+
def countSegments(self, s):
20+
"""
21+
:type s: str
22+
:rtype: int
23+
"""
24+
if not s:
25+
return 0
26+
ss = s.split(' ')
27+
return len([x for x in ss if x.strip() != ''])
28+
29+
"""其实一行就可以完成!不指定分隔符默认按照空格进行划分,包括连续的空格!"""
30+
class Solution(object):
31+
def countSegments(self, s):
32+
"""
33+
:type s: str
34+
:rtype: int
35+
"""
36+
return len(s.split())
37+
38+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
3+
Given a positive integer num, write a function which returns True if num is a perfect square else False.
4+
5+
Note: Do not use any built-in library function such as sqrt.
6+
7+
Example 1:
8+
9+
Input: 16
10+
Returns: True
11+
Example 2:
12+
13+
Input: 14
14+
Returns: False
15+
16+
"""
17+
18+
"""my solution
19+
二分搜索的方法
20+
"""
21+
22+
class Solution(object):
23+
def isPerfectSquare(self, num):
24+
"""
25+
:type num: int
26+
:rtype: bool
27+
"""
28+
if num < 0:
29+
return False
30+
left = 0
31+
right = num
32+
while left <= right:
33+
mid = (right-left)/2+left
34+
if mid * mid == num:
35+
return True
36+
elif mid * mid < num:
37+
left = mid + 1
38+
else:
39+
right = mid - 1
40+
return False
41+
42+
"""other solution
43+
使用牛顿方法解:http://blog.csdn.net/hyc__/article/details/41117009
44+
"""
45+
46+
class Solution(object):
47+
def isPerfectSquare(self, num):
48+
"""
49+
:type num: int
50+
:rtype: bool
51+
"""
52+
r = num
53+
while r*r > num:
54+
r = (r + num/r) / 2
55+
return r*r == num
56+

0 commit comments

Comments
 (0)