Skip to content

Commit

Permalink
更新LeetCode bytedance题目
Browse files Browse the repository at this point in the history
  • Loading branch information
qixiangyang committed Nov 15, 2019
1 parent 3a8b32f commit 751b46d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
62 changes: 62 additions & 0 deletions Leetcode/BD/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
Description:
Author:qxy
Date: 2019/11/15 5:15 下午
File: 1
"""

"""
我们定制了一款特殊的力扣键盘,所有的键都排列在一行上。
我们可以按从左到右的顺序,用一个长度为 26 的字符串 keyboard (索引从 0 开始,到 25 结束)来表示该键盘的键位布局。
现在需要测试这个键盘是否能够有效工作,那么我们就需要个机械手来测试这个键盘。
最初的时候,机械手位于左边起第一个键(也就是索引为 0 的键)的上方。当机械手移动到某一字符所在的键位时,就会在终端上输出该字符。
机械手从索引 i 移动到索引 j 所需要的时间是 |i - j|。
当前测试需要你使用机械手输出指定的单词 word,请你编写一个函数来计算机械手输出该单词所需的时间。
示例 1:
输入:keyboard = "abcdefghijklmnopqrstuvwxyz", word = "cba"
输出:4
解释:
机械手从 0 号键移动到 2 号键来输出 'c',又移动到 1 号键来输出 'b',接着移动到 0 号键来输出 'a'。
总用时 = 2 + 1 + 1 = 4.
示例 2:
输入:keyboard = "pqrstuvwxyzabcdefghijklmno", word = "leetcode"
输出:73
提示:
keyboard.length == 26
keyboard 按某种特定顺序排列,并包含每个小写英文字母一次。
1 <= word.length <= 10^4
word[i] 是一个小写英文字母
"""


class Solution:

def calculateTime(self, keyboard: str, word: str) -> int:
keyboard_dict = {x[1]:x[0] for x in enumerate(keyboard)}
count = 0
position = 0
for s in word:
now_position = keyboard_dict[s]
x = abs(now_position - position)
count += x
position = now_position

return count


a = Solution()
res = a.calculateTime('pqrstuvwxyzabcdefghijklmno', word="leetcode")
print(res)
25 changes: 25 additions & 0 deletions Leetcode/BD/2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
Description:
Author:qxy
Date: 2019/11/15 5:25 下午
File: 2
"""

from typing import List

"""
给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
示例:
输入: [-2,1,-3,4,-1,2,1,-5,4],
输出: 6
解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。
进阶:
如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。
"""

class Solution:
def maxSubArray(self, nums: List[int]) -> int:
pass

0 comments on commit 751b46d

Please sign in to comment.