美文网首页大数据 爬虫Python AI SqlPython小哥哥
Python解答力扣网站题库简单版!

Python解答力扣网站题库简单版!

作者: 14e61d025165 | 来源:发表于2019-05-19 15:24 被阅读0次

    1041. 困于环中的机器人

    题库链接: 1041. 困于环中的机器人 .

    题干

    在无限的平面上,机器人最初位于 (0, 0) 处,面朝北方。机器人可以接受下列三条指令之一:

    • "G":直走 1 个单位
    • "L":左转 90 度
    • "R":右转 90 度
    • 机器人按顺序执行指令 instructions,并一直重复它们。

    只有在平面中存在环使得机器人永远无法离开时,返回 true。否则,返回 false。

    示例

    Python学习交流群:1004391443,这里有资源共享,技术解答,还有小编从最基础的Python资料到项目实战的学习资料都有整理,希望能帮助你更了解python,学习python。

    示例 1

    输入:"GGLLGG" 输出:true 解释:机器人从 (0,0) 移动到 (0,2),转 180 度,然后回到 (0,0)。 重复这些指令,机器人将保持在以原点为中心,2 为半径的环中进行移动。

    示例 2

    输入:"GG" 输出:false 解释:机器人无限向北移动。

    示例3

    输入:"GL" 输出:true 解释: 机器人按 (0, 0) -> (0, 1) -> (-1, 1) -> (-1, 0) -> (0, 0) -> ... 进行移动。

    提示

    • 1 <= instructions.length <= 100
    • instructions[i] 在 {'G', 'L', 'R'} 中

    思路

    • 只要循环一次之后方向不是在向北或一次之后就在原点,
    • 那么循环四次或者四次之内之后就可以回到初始位置
    • 使用两个变量记录点的位置,使用两个变量记录机器人的方向。
    • 遍历指令,如果为‘L’, 执行dx, dy = -dy, dx(横坐标为纵坐标的相反数,纵坐标为横坐标),如果为‘R’,执行dx, dy = dy, -dx(横坐标为纵坐标,纵坐标为横坐标的相反数)。
    • 最后只要方向不向北或已经在原点机器人就可以离开。

    代码解析

    class Solution(object): def isBoomerang(self, points): """ :type points: List[List[int]] :rtype: bool """ A,B,C=points return (A[0]-B[0])(B[1]-C[1])-(A[1]-B[1])(B[0]-C[0])!=0 运行结果

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1558250592208" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    237. 删除链表中的节点

    题库链接: 237. 删除链表中的节点 .

    题干

    请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。

    现有一个链表 -- head = [4,5,1,9],它可以表示为:

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1558250592212" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    示例

    示例 1

    输入: head = [4,5,1,9], node = 5 输出: [4,1,9] 解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.

    示例 2

    输入: head = [4,5,1,9], node = 1 输出: [4,5,9] 解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9 .

    说明

    • 链表至少包含两个节点。
    • 链表中所有节点的值都是唯一的。
    • 给定的节点为非末尾节点并且一定是链表中的一个有效节点。
    • 不要从你的函数中返回任何结果。

    思路:

    • 使本节点为删除节点的下一个点,删除节点的下一个点为删除节点的下下一个节点。

    代码解析

    Definition for singly-linked list. # class ListNode(object): # def init(self, x): # self.val = x # self.next = None class Solution(object): def deleteNode(self, node): """ :type node: ListNode :rtype: void Do not return anything, modify node in-place instead. """ node.val, node.next = node.next.val, node.next.next 运行结果

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1558250592217" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    1. 两数之和

    题库链接: 1. 两数之和 .

    题干

    给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

    你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

    示例

    给定 nums = [2, 7, 11, 15], target = 9

    因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1]

    解法1

    思路:

    • 暴力破解,遍历每一个元素,并查找是否存在两个数和为target

    代码解析

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">class Solution(object):
    def twoSum(self, nums, target):
    """
    :type nums: List[int]
    :type target: int
    :rtype: List[int]
    """
    for i in range(len(nums)):
    for k in range(i + 1, len(nums)):
    if nums[i] + nums[k] == target:
    return [i, k]
    break
    </pre>

    运行结果

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1558250592223" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    当然解法一是非常暴力的会消耗很长时间和内存空间,接下来介绍一个更好的解法。

    解法2

    • 使用一个字典记录list里面的值和索引,遍历list,如果target-num在字典里面返回两个索引值,否则返回None

    代码解析

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
    hashmap = {}
    for index, num in enumerate(nums):
    another_num = target - num
    if another_num in hashmap:
    return [hashmap[another_num], index]
    hashmap[num] = index
    return None
    </pre>

    结尾

    • 鸡汤:人的一生,既不是想象中的那么好,也不是想象中的那么坏。每个人的背后都会有心酸,都会有无法言说的艰难。每个人都会有自己的泪要擦,都会有自己的路要走。

    相关文章

      网友评论

        本文标题:Python解答力扣网站题库简单版!

        本文链接:https://www.haomeiwen.com/subject/jhhqzqtx.html