LeetCode专题-模拟

栏目: 编程工具 · 发布时间: 6年前

内容简介:EasyOn an infinite plane, a robot initially stands at (0, 0) and faces north. The robot can receive one of three instructions:The robot performs the instructions given in order, and repeats them forever.

1041. Robot Bounded In Circle

Easy

On an infinite plane, a robot initially stands at (0, 0) and faces north. The robot can receive one of three instructions:

"G": go straight 1 unit;
"L": turn 90 degrees to the left;
"R": turn 90 degress to the right.

The robot performs the instructions given in order, and repeats them forever.

Return true if and only if there exists a circle in the plane such that the robot never leaves the circle.

Example 1:

Input: "GGLLGG"

Output: true

Explanation:

The robot moves from (0,0) to (0,2), turns 180 degrees, and then returns to (0,0).

When repeating these instructions, the robot remains in the circle of radius 2 centered at the origin.

题目大意:对一个机器人发送一系列重复的指令,要求判断机器人会不会回到原点。

解题思路:指令是重复的,因此只要机器人不朝向北方,就有机会回到原点

class Solution:
    def isRobotBounded(self, instructions: str) -> bool:
        x = 0
        y = 0
        dir = 0 #direction - N W S E
        #offset for each direction
        dx = [0, -1, 0, 1]
        dy = [1, 0, -1, 0]
        for c in instructions:
            if c == 'G': #go ahead by dir
                x += dx[dir]
                y += dy[dir]
            elif c == 'L': #turn left
                dir = (dir + 3)%4
            elif c == 'R': #turn right
                dir = (dir + 1)%4
        return (x == 0 and y == 0) or dir != 0 #if go back to origin or not facing north

测试一下

Success
[Details]
Runtime: 32 ms, faster than 86.36% of Python3 online submissions for Robot Bounded In Circle.
Memory Usage: 13.2 MB, less than 100.00% of Python3 online submissions for Robot Bounded In Circle.

838. Push Dominoes

Medium

There are N dominoes in a line, and we place each domino vertically upright.

In the beginning, we simultaneously push some of the dominoes either to the left or to the right.

LeetCode专题-模拟

image

After each second, each domino that is falling to the left pushes the adjacent domino on the left.

Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right.

When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces.

For the purposes of this question, we will consider that a falling domino expends no additional force to a falling or already fallen domino.

Given a string "S" representing the initial state. S[i] = 'L' , if the i-th domino has been pushed to the left; S[i] = 'R' , if the i-th domino has been pushed to the right; S[i] = '.' , if the i -th domino has not been pushed.

Return a string representing the final state.

Example 1:

Input:".L.R...LR..L.."

Output:"LL.RR.LLRRLL.."

Example 2:

Input:"RR.L"

Output:"RR.L"

Explanation:The first domino expends no additional force on the second domino.

题目大意:对于一组多米诺骨牌,给定一个初始化推的指令,求出最终多米诺骨牌的状态。

解题思路:通过推的指令,计算每一个骨牌的状态。

class Solution(object):
    def pushDominoes(self, D):
        """
        :type dominoes: str
        :rtype: str
        """
        max_int = 9999999
        #record the distance from current dominoe to nearest push down one(left and right)
        left_steps = [max_int for x in range(len(D))]
        right_steps = [max_int for x in range(len(D))]

        for i in range(len(D)):
            if D[i] == 'L':
                #if occur one push down domine
                left_steps[i] = 0
                #for all following domine that influenced
                for j in range(i-1, -1, -1):
                    if D[j] != '.': #only influence '.'
                        break
                    left_steps[j] = left_steps[j+1]+1
            elif D[i] == 'R':
                right_steps[i] = 0
                for j in range(i+1, len(D)):
                    if D[j] != '.':
                        break
                    right_steps[j] = right_steps[j-1]+1

        #simulate the push work
        ND = ''
        for i in range(len(D)):
            if left_steps[i] < right_steps[i]:
                ND += 'L'
            elif left_steps[i] > right_steps[i]:
                ND += 'R'
            else:
                ND += '.'

        return ND

测试一下,算法应该是对的,但是用 python 实现会超时,用golang或者cpp可以通过所有测试用例。

Time Limit Exceeded
[Details]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据结构

数据结构

殷人昆 / 清华大学 / 2007-6 / 39.00元

《数据结构》(第2版)“数据结构”是计算机专业的核心课程,是从事计算机软件开发和应用人员必备的专业基础。随着计算机的日益普及,“数据结构”课程也在不断地发展。《数据结构》(第2版)按照清华大学计算机系本科“数据结构”大纲的要求,从面向对象的概念、对象类设计的风格和数据结构的层次开始,从线性结构到非线性结构,从简单到复杂,深入地讨论了各种数据结构内在的逻辑关系及其在计算机中的实现方式和使用。此外,对......一起来看看 《数据结构》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具