leetcode

leetcode

Day6 leetcode Fizz Buzz 字符转换游戏(实在不知道该怎么翻译这个)

python李魔佛 发表了文章 • 0 个评论 • 4075 次浏览 • 2017-03-07 11:06 • 来自相关话题

Fizz BuzzWrite a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

Example:
n = 15,

Return:
[
"1",
"2",
"Fizz",
"4",
"Buzz",
"Fizz",
"7",
"8",
"Fizz",
"Buzz",
"11",
"Fizz",
"13",
"14",
"FizzBuzz"
]
中文解释:
输入一个数字,返回从1到这个数字的所有数字的字符串,如果遇到3的倍数,就替换成Fizz, 遇到5的倍数,就tihu替换成Buzz,同时是3和5的倍数,就tihu替换成FizzBuzz。
 
我的代码:
def fizzBuzz(self, n):
"""
:type n: int
:rtype: List[str]
"""

result=[]
for i in range(1,n+1):
remainder1=i%3
remainder2=i%5
if remainder1 != 0 and remainder2!=0:
result.append(str(i))
elif remainder1==0 and remainder2!=0:
result.append('Fizz')
elif remainder1!=0 and remainder2 == 0:
result.append("Buzz")
else:
result.append("FizzBuzz")

return result 查看全部
Fizz BuzzWrite a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

Example:
n = 15,

Return:
[
"1",
"2",
"Fizz",
"4",
"Buzz",
"Fizz",
"7",
"8",
"Fizz",
"Buzz",
"11",
"Fizz",
"13",
"14",
"FizzBuzz"
]

中文解释:
输入一个数字,返回从1到这个数字的所有数字的字符串,如果遇到3的倍数,就替换成Fizz, 遇到5的倍数,就tihu替换成Buzz,同时是3和5的倍数,就tihu替换成FizzBuzz。
 
我的代码:
    def fizzBuzz(self, n):
"""
:type n: int
:rtype: List[str]
"""

result=[]
for i in range(1,n+1):
remainder1=i%3
remainder2=i%5
if remainder1 != 0 and remainder2!=0:
result.append(str(i))
elif remainder1==0 and remainder2!=0:
result.append('Fizz')
elif remainder1!=0 and remainder2 == 0:
result.append("Buzz")
else:
result.append("FizzBuzz")

return result

Day5 leetcode Next Greater Element 下一个更大的元素

python李魔佛 发表了文章 • 0 个评论 • 4112 次浏览 • 2017-03-03 09:30 • 来自相关话题

You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.

The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.
 
Example 1:
Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
Explanation:
For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
For number 1 in the first array, the next greater number for it in the second array is 3.
For number 2 in the first array, there is no next greater number for it in the second array, so output -1.Example 2:
Input: nums1 = [2,4], nums2 = [1,2,3,4].
Output: [3,-1]
Explanation:
For number 2 in the first array, the next greater number for it in the second array is 3.
For number 4 in the first array, there is no next greater number for it in the second array, so
Note:


All elements in nums1 and nums2 are unique.
The length of both nums1 and nums2 would not exceed 1000.
 
中文解释下:
有2个数组(列表) num1,和num2,

nums1 = [4,1,2], nums2 = [1,3,4,2].
 
nums1是num2的子集
 
然后在nums1中每个元素,在num2中找到第一个比它大的元素,比如nums1中第一个是4,在nums2中没有比4更大的,所以返回的是-1,nums第二个是1,在nums2中第一个比1大的是3,所以返回的是3,第三个的是2,nums2中第一个比2大的数是3,所以返回的是3
所以上面的结果需要返回:
[-1,3,3] 查看全部
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.

The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.
 
Example 1:
Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
Explanation:
For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
For number 1 in the first array, the next greater number for it in the second array is 3.
For number 2 in the first array, there is no next greater number for it in the second array, so output -1.
Example 2:
Input: nums1 = [2,4], nums2 = [1,2,3,4].
Output: [3,-1]
Explanation:
For number 2 in the first array, the next greater number for it in the second array is 3.
For number 4 in the first array, there is no next greater number for it in the second array, so

Note:


All elements in nums1 and nums2 are unique.
The length of both nums1 and nums2 would not exceed 1000.

 
中文解释下:
有2个数组(列表) num1,和num2,

nums1 = [4,1,2], nums2 = [1,3,4,2].
 
nums1是num2的子集
 
然后在nums1中每个元素,在num2中找到第一个比它大的元素,比如nums1中第一个是4,在nums2中没有比4更大的,所以返回的是-1,nums第二个是1,在nums2中第一个比1大的是3,所以返回的是3,第三个的是2,nums2中第一个比2大的数是3,所以返回的是3
所以上面的结果需要返回:
[-1,3,3]

leetcode Day4 Keyboard Row 键盘中的行

python李魔佛 发表了文章 • 0 个评论 • 4700 次浏览 • 2017-02-28 22:21 • 来自相关话题

Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.





 
Example 1:
Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]Note:


You may use one character in the keyboard more than once.
You may assume the input string will only contain letters of alphabet.
 
中文解释:
输入一个字符串列表,如果这个字符串的字母在键盘上的位置为同一行,就输出这个字符串,否则不输出。
 
def findWords( words):
"""
:type words: List[str]
:rtype: List[str]
"""

kb={'q':['q','w','e','r','t','y','u','i','o','p'],
'a':['a','s','d','f','g','h','j','k','l'],
'z':['z','x','c','v','b','n','m']}

rList=[]
qRow= kb['q']
aRow= kb['a']
zRow= kb['z']
for wi in words:
w=wi.lower()
i=0
l=len(w)

if w[0] in qRow:
row=qRow
if w[0] in aRow:
row=aRow
if w[0] in zRow:
row=zRow

#row=kb[w[0]]
for i in range(len(w)):
if w[i] not in row:
break
else:
if i==l-1:
rList.append(wi)
return rList
解释:
首先将字母转换为统一的小写字母,然后根据首字母确定该字符串会属于键盘上的哪一列,因为键盘上只有3列,分别为q,a,z行,如果确定属于q行,接下来将剩下的字符一直在q行内循环,一旦遇到不在q行,就退出这一次的循环,进行下一个字符串的判断。
  查看全部
Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.

keyboard.png

 
Example 1:
Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]
Note:


You may use one character in the keyboard more than once.
You may assume the input string will only contain letters of alphabet.
 
中文解释:
输入一个字符串列表,如果这个字符串的字母在键盘上的位置为同一行,就输出这个字符串,否则不输出。
 
    def findWords( words):
"""
:type words: List[str]
:rtype: List[str]
"""

kb={'q':['q','w','e','r','t','y','u','i','o','p'],
'a':['a','s','d','f','g','h','j','k','l'],
'z':['z','x','c','v','b','n','m']}

rList=[]
qRow= kb['q']
aRow= kb['a']
zRow= kb['z']
for wi in words:
w=wi.lower()
i=0
l=len(w)

if w[0] in qRow:
row=qRow
if w[0] in aRow:
row=aRow
if w[0] in zRow:
row=zRow

#row=kb[w[0]]
for i in range(len(w)):
if w[i] not in row:
break
else:
if i==l-1:
rList.append(wi)
return rList

解释:
首先将字母转换为统一的小写字母,然后根据首字母确定该字符串会属于键盘上的哪一列,因为键盘上只有3列,分别为q,a,z行,如果确定属于q行,接下来将剩下的字符一直在q行内循环,一旦遇到不在q行,就退出这一次的循环,进行下一个字符串的判断。
 

leetcode Day3 complement number 【补码】

python李魔佛 发表了文章 • 0 个评论 • 3304 次浏览 • 2017-02-27 21:47 • 来自相关话题

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

Note:


The given integer is guaranteed to fit within the range of a 32-bit signed integer.
You could assume no leading zero bit in the integer’s binary representation.

Example 1:
 
Input: 5
Output: 2
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
Input: 1
Output: 0
Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
解决方法:
class Solution(object):
def findComplement(self, num):
"""
:type num: int
:rtype: int
"""
result=[]
while num!=0:
remainder=num%2
num=num/2
result.append(remainder)
l=len(result)

for i in range(l/2):
temp=result[i]
result[i]=result[l-i-1]
result[l-i-1]=temp
lam=lambda x:abs(x-1)

for i in range(l):
result[i]=lam(result[i])

#print result
sum=0
for i in range(l):
sum=sum+result[l-i-1]*pow(2,i)
return sum
PS: 上面的方法是第一次开始做的最原始的方法,巨傻无比。 完全就是一个没学过计算机原理或者微机原理的人的代码哈。 连一个数的补码这种计算机第一节课的内容都还给老师了。。
 
附上最简便的一个解法:
class Solution(object):
def findComplement(self, num):
i = 1
while i <= num:
i = i << 1
return (i - 1) ^ num
以为其实一个补码就是一个数与另外一个全1的数进行异或处理哈。
鄙视自己!!!
 
  查看全部
Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

Note:


The given integer is guaranteed to fit within the range of a 32-bit signed integer.
You could assume no leading zero bit in the integer’s binary representation.

Example 1:
 
Input: 5
Output: 2
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.

Input: 1
Output: 0
Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.

解决方法:
class Solution(object):
def findComplement(self, num):
"""
:type num: int
:rtype: int
"""
result=[]
while num!=0:
remainder=num%2
num=num/2
result.append(remainder)
l=len(result)

for i in range(l/2):
temp=result[i]
result[i]=result[l-i-1]
result[l-i-1]=temp
lam=lambda x:abs(x-1)

for i in range(l):
result[i]=lam(result[i])

#print result
sum=0
for i in range(l):
sum=sum+result[l-i-1]*pow(2,i)
return sum

PS: 上面的方法是第一次开始做的最原始的方法,巨傻无比。 完全就是一个没学过计算机原理或者微机原理的人的代码哈。 连一个数的补码这种计算机第一节课的内容都还给老师了。。
 
附上最简便的一个解法:
class Solution(object):
def findComplement(self, num):
i = 1
while i <= num:
i = i << 1
return (i - 1) ^ num

以为其实一个补码就是一个数与另外一个全1的数进行异或处理哈。
鄙视自己!!!
 
 

leetcode Day2 Hamming Distance 海明距离

python李魔佛 发表了文章 • 0 个评论 • 3756 次浏览 • 2017-02-26 21:57 • 来自相关话题

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Given two integers x and y, calculate the Hamming distance.

Note:
0 ≤ x, y < 231.

Example:
 
Input: x = 1, y = 4

Output: 2

Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑

The above arrows point to positions where the corresponding bits are different.

中文的大概意思是,两个数取异或,然后看异或后,1的个数,也就是二进制数中不同位的个数。 这编码常用于信道编码。
 
python中异或用符号^, 然后python自带一个函数bin(),可以把一个数转换为二进制。
转为为二进制后,再用一个循环计算1出现的个数。
 
上代码:
 
x=10
y=20
z=x^y
#solution 1
s=bin(z)
print type(s)
distance=0
for i in s[2:]:
if i=='1':
distance+=1
print "distance is %d" %distance 查看全部
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Given two integers x and y, calculate the Hamming distance.

Note:
0 ≤ x, y < 231.

Example:
 
Input: x = 1, y = 4

Output: 2

Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑

The above arrows point to positions where the corresponding bits are different.

中文的大概意思是,两个数取异或,然后看异或后,1的个数,也就是二进制数中不同位的个数。 这编码常用于信道编码。
 
python中异或用符号^, 然后python自带一个函数bin(),可以把一个数转换为二进制。
转为为二进制后,再用一个循环计算1出现的个数。
 
上代码:
 
x=10
y=20
z=x^y
#solution 1
s=bin(z)
print type(s)
distance=0
for i in s[2:]:
if i=='1':
distance+=1
print "distance is %d" %distance

leetcode Day1 Two Sum 两数之和

python李魔佛 发表了文章 • 0 个评论 • 3737 次浏览 • 2017-02-25 18:39 • 来自相关话题

题目:
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].
 
中文意思就是,给定输入一个列表和一个目标数, 输出的是两个数的下标,这个数的对应的值相加,等于目标数。
 
上代码:

def twoSum(nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
indics=[]
for i in range(len(nums)-1):
#使用两次循环,类似于冒泡算法,尝试每个数和另外一个数的和,枚举
for j in range(i+1,len(nums)):
if target== nums[i]+nums[j]:
indics.append(i)
indics.append(j)
return indics
  查看全部
题目:
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].
 
中文意思就是,给定输入一个列表和一个目标数, 输出的是两个数的下标,这个数的对应的值相加,等于目标数。
 
上代码:

def twoSum(nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
indics=[]
for i in range(len(nums)-1):
#使用两次循环,类似于冒泡算法,尝试每个数和另外一个数的和,枚举
for j in range(i+1,len(nums)):
if target== nums[i]+nums[j]:
indics.append(i)
indics.append(j)
return indics

 

每天一道leetcode题目 30天尝试新事情

30天新尝试李魔佛 发表了文章 • 0 个评论 • 4455 次浏览 • 2017-02-24 12:14 • 来自相关话题

忽然觉得自己基础知识忘了不少。所以打算做做传说中的刷题神器--leetcode。 题目在官网: https://leetcode.com
 
从接受度排序,先挑战接受度高的题目,然后循序渐进。
 
每天更新在这个帖子里面。 (最近发现另外一个在线编程练习网站lintcode,题目都免费,而且适合国内的公司,也开始在做了,这些题目后面会在这个贴子里更新吧)
 
Day 1 2017-2-25
leetcode Day1 Two Sum 两数之和
http://30daydo.com/article/145
 
Day 2 2017-2-26
leetcode Day2 Hamming Distance 海明距离​
http://30daydo.com/article/146

Day3 2017-2-27
leetcode Day3 complement number 【补码】
http://30daydo.com/article/147 

Day4 2017-2-28
leetcode Day4 Keyboard Row 键盘中的行
http://30daydo.com/article/148
 
Day4 2017-3-1
Day5 leetcode Next Greater Element 下一个更大的元素​
http://30daydo.com/article/150
 
Day4 2017-3-7
Day6 leetcode Fizz Buzz 字符转换游戏(实在不知道该怎么翻译这个)​
http://30daydo.com/article/151
 
 
  查看全部
忽然觉得自己基础知识忘了不少。所以打算做做传说中的刷题神器--leetcode。 题目在官网: https://leetcode.com
 
从接受度排序,先挑战接受度高的题目,然后循序渐进。
 
每天更新在这个帖子里面。 (最近发现另外一个在线编程练习网站lintcode,题目都免费,而且适合国内的公司,也开始在做了,这些题目后面会在这个贴子里更新吧)
 
Day 1 2017-2-25
leetcode Day1 Two Sum 两数之和
http://30daydo.com/article/145
 
Day 2 2017-2-26
leetcode Day2 Hamming Distance 海明距离​
http://30daydo.com/article/146

Day3 2017-2-27
leetcode Day3 complement number 【补码】
http://30daydo.com/article/147 

Day4 2017-2-28
leetcode Day4 Keyboard Row 键盘中的行
http://30daydo.com/article/148
 
Day4 2017-3-1
Day5 leetcode Next Greater Element 下一个更大的元素​
http://30daydo.com/article/150
 
Day4 2017-3-7
Day6 leetcode Fizz Buzz 字符转换游戏(实在不知道该怎么翻译这个)​
http://30daydo.com/article/151
 
 
 

每天一道leetcode题目 30天尝试新事情

30天新尝试李魔佛 发表了文章 • 0 个评论 • 4455 次浏览 • 2017-02-24 12:14 • 来自相关话题

忽然觉得自己基础知识忘了不少。所以打算做做传说中的刷题神器--leetcode。 题目在官网: https://leetcode.com
 
从接受度排序,先挑战接受度高的题目,然后循序渐进。
 
每天更新在这个帖子里面。 (最近发现另外一个在线编程练习网站lintcode,题目都免费,而且适合国内的公司,也开始在做了,这些题目后面会在这个贴子里更新吧)
 
Day 1 2017-2-25
leetcode Day1 Two Sum 两数之和
http://30daydo.com/article/145
 
Day 2 2017-2-26
leetcode Day2 Hamming Distance 海明距离​
http://30daydo.com/article/146

Day3 2017-2-27
leetcode Day3 complement number 【补码】
http://30daydo.com/article/147 

Day4 2017-2-28
leetcode Day4 Keyboard Row 键盘中的行
http://30daydo.com/article/148
 
Day4 2017-3-1
Day5 leetcode Next Greater Element 下一个更大的元素​
http://30daydo.com/article/150
 
Day4 2017-3-7
Day6 leetcode Fizz Buzz 字符转换游戏(实在不知道该怎么翻译这个)​
http://30daydo.com/article/151
 
 
  查看全部
忽然觉得自己基础知识忘了不少。所以打算做做传说中的刷题神器--leetcode。 题目在官网: https://leetcode.com
 
从接受度排序,先挑战接受度高的题目,然后循序渐进。
 
每天更新在这个帖子里面。 (最近发现另外一个在线编程练习网站lintcode,题目都免费,而且适合国内的公司,也开始在做了,这些题目后面会在这个贴子里更新吧)
 
Day 1 2017-2-25
leetcode Day1 Two Sum 两数之和
http://30daydo.com/article/145
 
Day 2 2017-2-26
leetcode Day2 Hamming Distance 海明距离​
http://30daydo.com/article/146

Day3 2017-2-27
leetcode Day3 complement number 【补码】
http://30daydo.com/article/147 

Day4 2017-2-28
leetcode Day4 Keyboard Row 键盘中的行
http://30daydo.com/article/148
 
Day4 2017-3-1
Day5 leetcode Next Greater Element 下一个更大的元素​
http://30daydo.com/article/150
 
Day4 2017-3-7
Day6 leetcode Fizz Buzz 字符转换游戏(实在不知道该怎么翻译这个)​
http://30daydo.com/article/151
 
 
 

Day6 leetcode Fizz Buzz 字符转换游戏(实在不知道该怎么翻译这个)

python李魔佛 发表了文章 • 0 个评论 • 4075 次浏览 • 2017-03-07 11:06 • 来自相关话题

Fizz BuzzWrite a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

Example:
n = 15,

Return:
[
"1",
"2",
"Fizz",
"4",
"Buzz",
"Fizz",
"7",
"8",
"Fizz",
"Buzz",
"11",
"Fizz",
"13",
"14",
"FizzBuzz"
]
中文解释:
输入一个数字,返回从1到这个数字的所有数字的字符串,如果遇到3的倍数,就替换成Fizz, 遇到5的倍数,就tihu替换成Buzz,同时是3和5的倍数,就tihu替换成FizzBuzz。
 
我的代码:
def fizzBuzz(self, n):
"""
:type n: int
:rtype: List[str]
"""

result=[]
for i in range(1,n+1):
remainder1=i%3
remainder2=i%5
if remainder1 != 0 and remainder2!=0:
result.append(str(i))
elif remainder1==0 and remainder2!=0:
result.append('Fizz')
elif remainder1!=0 and remainder2 == 0:
result.append("Buzz")
else:
result.append("FizzBuzz")

return result 查看全部
Fizz BuzzWrite a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

Example:
n = 15,

Return:
[
"1",
"2",
"Fizz",
"4",
"Buzz",
"Fizz",
"7",
"8",
"Fizz",
"Buzz",
"11",
"Fizz",
"13",
"14",
"FizzBuzz"
]

中文解释:
输入一个数字,返回从1到这个数字的所有数字的字符串,如果遇到3的倍数,就替换成Fizz, 遇到5的倍数,就tihu替换成Buzz,同时是3和5的倍数,就tihu替换成FizzBuzz。
 
我的代码:
    def fizzBuzz(self, n):
"""
:type n: int
:rtype: List[str]
"""

result=[]
for i in range(1,n+1):
remainder1=i%3
remainder2=i%5
if remainder1 != 0 and remainder2!=0:
result.append(str(i))
elif remainder1==0 and remainder2!=0:
result.append('Fizz')
elif remainder1!=0 and remainder2 == 0:
result.append("Buzz")
else:
result.append("FizzBuzz")

return result

Day5 leetcode Next Greater Element 下一个更大的元素

python李魔佛 发表了文章 • 0 个评论 • 4112 次浏览 • 2017-03-03 09:30 • 来自相关话题

You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.

The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.
 
Example 1:
Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
Explanation:
For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
For number 1 in the first array, the next greater number for it in the second array is 3.
For number 2 in the first array, there is no next greater number for it in the second array, so output -1.Example 2:
Input: nums1 = [2,4], nums2 = [1,2,3,4].
Output: [3,-1]
Explanation:
For number 2 in the first array, the next greater number for it in the second array is 3.
For number 4 in the first array, there is no next greater number for it in the second array, so
Note:


All elements in nums1 and nums2 are unique.
The length of both nums1 and nums2 would not exceed 1000.
 
中文解释下:
有2个数组(列表) num1,和num2,

nums1 = [4,1,2], nums2 = [1,3,4,2].
 
nums1是num2的子集
 
然后在nums1中每个元素,在num2中找到第一个比它大的元素,比如nums1中第一个是4,在nums2中没有比4更大的,所以返回的是-1,nums第二个是1,在nums2中第一个比1大的是3,所以返回的是3,第三个的是2,nums2中第一个比2大的数是3,所以返回的是3
所以上面的结果需要返回:
[-1,3,3] 查看全部
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.

The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.
 
Example 1:
Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
Explanation:
For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
For number 1 in the first array, the next greater number for it in the second array is 3.
For number 2 in the first array, there is no next greater number for it in the second array, so output -1.
Example 2:
Input: nums1 = [2,4], nums2 = [1,2,3,4].
Output: [3,-1]
Explanation:
For number 2 in the first array, the next greater number for it in the second array is 3.
For number 4 in the first array, there is no next greater number for it in the second array, so

Note:


All elements in nums1 and nums2 are unique.
The length of both nums1 and nums2 would not exceed 1000.

 
中文解释下:
有2个数组(列表) num1,和num2,

nums1 = [4,1,2], nums2 = [1,3,4,2].
 
nums1是num2的子集
 
然后在nums1中每个元素,在num2中找到第一个比它大的元素,比如nums1中第一个是4,在nums2中没有比4更大的,所以返回的是-1,nums第二个是1,在nums2中第一个比1大的是3,所以返回的是3,第三个的是2,nums2中第一个比2大的数是3,所以返回的是3
所以上面的结果需要返回:
[-1,3,3]

leetcode Day4 Keyboard Row 键盘中的行

python李魔佛 发表了文章 • 0 个评论 • 4700 次浏览 • 2017-02-28 22:21 • 来自相关话题

Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.





 
Example 1:
Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]Note:


You may use one character in the keyboard more than once.
You may assume the input string will only contain letters of alphabet.
 
中文解释:
输入一个字符串列表,如果这个字符串的字母在键盘上的位置为同一行,就输出这个字符串,否则不输出。
 
def findWords( words):
"""
:type words: List[str]
:rtype: List[str]
"""

kb={'q':['q','w','e','r','t','y','u','i','o','p'],
'a':['a','s','d','f','g','h','j','k','l'],
'z':['z','x','c','v','b','n','m']}

rList=[]
qRow= kb['q']
aRow= kb['a']
zRow= kb['z']
for wi in words:
w=wi.lower()
i=0
l=len(w)

if w[0] in qRow:
row=qRow
if w[0] in aRow:
row=aRow
if w[0] in zRow:
row=zRow

#row=kb[w[0]]
for i in range(len(w)):
if w[i] not in row:
break
else:
if i==l-1:
rList.append(wi)
return rList
解释:
首先将字母转换为统一的小写字母,然后根据首字母确定该字符串会属于键盘上的哪一列,因为键盘上只有3列,分别为q,a,z行,如果确定属于q行,接下来将剩下的字符一直在q行内循环,一旦遇到不在q行,就退出这一次的循环,进行下一个字符串的判断。
  查看全部
Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.

keyboard.png

 
Example 1:
Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]
Note:


You may use one character in the keyboard more than once.
You may assume the input string will only contain letters of alphabet.
 
中文解释:
输入一个字符串列表,如果这个字符串的字母在键盘上的位置为同一行,就输出这个字符串,否则不输出。
 
    def findWords( words):
"""
:type words: List[str]
:rtype: List[str]
"""

kb={'q':['q','w','e','r','t','y','u','i','o','p'],
'a':['a','s','d','f','g','h','j','k','l'],
'z':['z','x','c','v','b','n','m']}

rList=[]
qRow= kb['q']
aRow= kb['a']
zRow= kb['z']
for wi in words:
w=wi.lower()
i=0
l=len(w)

if w[0] in qRow:
row=qRow
if w[0] in aRow:
row=aRow
if w[0] in zRow:
row=zRow

#row=kb[w[0]]
for i in range(len(w)):
if w[i] not in row:
break
else:
if i==l-1:
rList.append(wi)
return rList

解释:
首先将字母转换为统一的小写字母,然后根据首字母确定该字符串会属于键盘上的哪一列,因为键盘上只有3列,分别为q,a,z行,如果确定属于q行,接下来将剩下的字符一直在q行内循环,一旦遇到不在q行,就退出这一次的循环,进行下一个字符串的判断。
 

leetcode Day3 complement number 【补码】

python李魔佛 发表了文章 • 0 个评论 • 3304 次浏览 • 2017-02-27 21:47 • 来自相关话题

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

Note:


The given integer is guaranteed to fit within the range of a 32-bit signed integer.
You could assume no leading zero bit in the integer’s binary representation.

Example 1:
 
Input: 5
Output: 2
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
Input: 1
Output: 0
Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
解决方法:
class Solution(object):
def findComplement(self, num):
"""
:type num: int
:rtype: int
"""
result=[]
while num!=0:
remainder=num%2
num=num/2
result.append(remainder)
l=len(result)

for i in range(l/2):
temp=result[i]
result[i]=result[l-i-1]
result[l-i-1]=temp
lam=lambda x:abs(x-1)

for i in range(l):
result[i]=lam(result[i])

#print result
sum=0
for i in range(l):
sum=sum+result[l-i-1]*pow(2,i)
return sum
PS: 上面的方法是第一次开始做的最原始的方法,巨傻无比。 完全就是一个没学过计算机原理或者微机原理的人的代码哈。 连一个数的补码这种计算机第一节课的内容都还给老师了。。
 
附上最简便的一个解法:
class Solution(object):
def findComplement(self, num):
i = 1
while i <= num:
i = i << 1
return (i - 1) ^ num
以为其实一个补码就是一个数与另外一个全1的数进行异或处理哈。
鄙视自己!!!
 
  查看全部
Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

Note:


The given integer is guaranteed to fit within the range of a 32-bit signed integer.
You could assume no leading zero bit in the integer’s binary representation.

Example 1:
 
Input: 5
Output: 2
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.

Input: 1
Output: 0
Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.

解决方法:
class Solution(object):
def findComplement(self, num):
"""
:type num: int
:rtype: int
"""
result=[]
while num!=0:
remainder=num%2
num=num/2
result.append(remainder)
l=len(result)

for i in range(l/2):
temp=result[i]
result[i]=result[l-i-1]
result[l-i-1]=temp
lam=lambda x:abs(x-1)

for i in range(l):
result[i]=lam(result[i])

#print result
sum=0
for i in range(l):
sum=sum+result[l-i-1]*pow(2,i)
return sum

PS: 上面的方法是第一次开始做的最原始的方法,巨傻无比。 完全就是一个没学过计算机原理或者微机原理的人的代码哈。 连一个数的补码这种计算机第一节课的内容都还给老师了。。
 
附上最简便的一个解法:
class Solution(object):
def findComplement(self, num):
i = 1
while i <= num:
i = i << 1
return (i - 1) ^ num

以为其实一个补码就是一个数与另外一个全1的数进行异或处理哈。
鄙视自己!!!
 
 

leetcode Day2 Hamming Distance 海明距离

python李魔佛 发表了文章 • 0 个评论 • 3756 次浏览 • 2017-02-26 21:57 • 来自相关话题

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Given two integers x and y, calculate the Hamming distance.

Note:
0 ≤ x, y < 231.

Example:
 
Input: x = 1, y = 4

Output: 2

Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑

The above arrows point to positions where the corresponding bits are different.

中文的大概意思是,两个数取异或,然后看异或后,1的个数,也就是二进制数中不同位的个数。 这编码常用于信道编码。
 
python中异或用符号^, 然后python自带一个函数bin(),可以把一个数转换为二进制。
转为为二进制后,再用一个循环计算1出现的个数。
 
上代码:
 
x=10
y=20
z=x^y
#solution 1
s=bin(z)
print type(s)
distance=0
for i in s[2:]:
if i=='1':
distance+=1
print "distance is %d" %distance 查看全部
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Given two integers x and y, calculate the Hamming distance.

Note:
0 ≤ x, y < 231.

Example:
 
Input: x = 1, y = 4

Output: 2

Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑

The above arrows point to positions where the corresponding bits are different.

中文的大概意思是,两个数取异或,然后看异或后,1的个数,也就是二进制数中不同位的个数。 这编码常用于信道编码。
 
python中异或用符号^, 然后python自带一个函数bin(),可以把一个数转换为二进制。
转为为二进制后,再用一个循环计算1出现的个数。
 
上代码:
 
x=10
y=20
z=x^y
#solution 1
s=bin(z)
print type(s)
distance=0
for i in s[2:]:
if i=='1':
distance+=1
print "distance is %d" %distance

leetcode Day1 Two Sum 两数之和

python李魔佛 发表了文章 • 0 个评论 • 3737 次浏览 • 2017-02-25 18:39 • 来自相关话题

题目:
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].
 
中文意思就是,给定输入一个列表和一个目标数, 输出的是两个数的下标,这个数的对应的值相加,等于目标数。
 
上代码:

def twoSum(nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
indics=[]
for i in range(len(nums)-1):
#使用两次循环,类似于冒泡算法,尝试每个数和另外一个数的和,枚举
for j in range(i+1,len(nums)):
if target== nums[i]+nums[j]:
indics.append(i)
indics.append(j)
return indics
  查看全部
题目:
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].
 
中文意思就是,给定输入一个列表和一个目标数, 输出的是两个数的下标,这个数的对应的值相加,等于目标数。
 
上代码:

def twoSum(nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
indics=[]
for i in range(len(nums)-1):
#使用两次循环,类似于冒泡算法,尝试每个数和另外一个数的和,枚举
for j in range(i+1,len(nums)):
if target== nums[i]+nums[j]:
indics.append(i)
indics.append(j)
return indics

 

每天一道leetcode题目 30天尝试新事情

30天新尝试李魔佛 发表了文章 • 0 个评论 • 4455 次浏览 • 2017-02-24 12:14 • 来自相关话题

忽然觉得自己基础知识忘了不少。所以打算做做传说中的刷题神器--leetcode。 题目在官网: https://leetcode.com
 
从接受度排序,先挑战接受度高的题目,然后循序渐进。
 
每天更新在这个帖子里面。 (最近发现另外一个在线编程练习网站lintcode,题目都免费,而且适合国内的公司,也开始在做了,这些题目后面会在这个贴子里更新吧)
 
Day 1 2017-2-25
leetcode Day1 Two Sum 两数之和
http://30daydo.com/article/145
 
Day 2 2017-2-26
leetcode Day2 Hamming Distance 海明距离​
http://30daydo.com/article/146

Day3 2017-2-27
leetcode Day3 complement number 【补码】
http://30daydo.com/article/147 

Day4 2017-2-28
leetcode Day4 Keyboard Row 键盘中的行
http://30daydo.com/article/148
 
Day4 2017-3-1
Day5 leetcode Next Greater Element 下一个更大的元素​
http://30daydo.com/article/150
 
Day4 2017-3-7
Day6 leetcode Fizz Buzz 字符转换游戏(实在不知道该怎么翻译这个)​
http://30daydo.com/article/151
 
 
  查看全部
忽然觉得自己基础知识忘了不少。所以打算做做传说中的刷题神器--leetcode。 题目在官网: https://leetcode.com
 
从接受度排序,先挑战接受度高的题目,然后循序渐进。
 
每天更新在这个帖子里面。 (最近发现另外一个在线编程练习网站lintcode,题目都免费,而且适合国内的公司,也开始在做了,这些题目后面会在这个贴子里更新吧)
 
Day 1 2017-2-25
leetcode Day1 Two Sum 两数之和
http://30daydo.com/article/145
 
Day 2 2017-2-26
leetcode Day2 Hamming Distance 海明距离​
http://30daydo.com/article/146

Day3 2017-2-27
leetcode Day3 complement number 【补码】
http://30daydo.com/article/147 

Day4 2017-2-28
leetcode Day4 Keyboard Row 键盘中的行
http://30daydo.com/article/148
 
Day4 2017-3-1
Day5 leetcode Next Greater Element 下一个更大的元素​
http://30daydo.com/article/150
 
Day4 2017-3-7
Day6 leetcode Fizz Buzz 字符转换游戏(实在不知道该怎么翻译这个)​
http://30daydo.com/article/151