Algorithm
本周选择的算法题是:Walking Robot Simulation II。
规则
A width x height
grid is on an XY-plane with the bottom-left cell at (0, 0)
and the top-right cell at (width - 1, height - 1)
. The grid is aligned with the four cardinal directions ("North"
, "East"
, "South"
, and "West"
). A robot is initially at cell (0, 0)
facing direction "East"
.
The robot can be instructed to move for a specific number of steps. For each step, it does the following.
- Attempts to move forward one cell in the direction it is facing.
- If the cell the robot is moving to is out of bounds, the robot instead turns 90 degrees counterclockwise and retries the step.
After the robot finishes moving the number of steps required, it stops and awaits the next instruction.
Implement the Robot
class:
Robot(int width, int height)
Initializes thewidth x height
grid with the robot at(0, 0)
facing"East"
.void step(int num)
Instructs the robot to move forwardnum
steps.int[] getPos()
Returns the current cell the robot is at, as an array of length 2,[x, y]
.String getDir()
Returns the current direction of the robot,"North"
,"East"
,"South"
, or"West"
.
Example 1:
Input
["Robot", "step", "step", "getPos", "getDir", "step", "step", "step", "getPos", "getDir"]
[[6, 3], [2], [2], [], [], [2], [1], [4], [], []]
Output
[null, null, null, [4, 0], "East", null, null, null, [1, 2], "West"]
Explanation
Robot robot = new Robot(6, 3); // Initialize the grid and the robot at (0, 0) facing East.
robot.step(2); // It moves two steps East to (2, 0), and faces East.
robot.step(2); // It moves two steps East to (4, 0), and faces East.
robot.getPos(); // return [4, 0]
robot.getDir(); // return "East"
robot.step(2); // It moves one step East to (5, 0), and faces East.
// Moving the next step East would be out of bounds, so it turns and faces North.
// Then, it moves one step North to (5, 1), and faces North.
robot.step(1); // It moves one step North to (5, 2), and faces North (not West).
robot.step(4); // Moving the next step North would be out of bounds, so it turns and faces West.
// Then, it moves four steps West to (1, 2), and faces West.
robot.getPos(); // return [1, 2]
robot.getDir(); // return "West"
Constraints:
2 <= width, height <= 100
1 <= num <= 105
- At most
104
calls in total will be made tostep
,getPos
, andgetDir
.
Solution
class Robot:
__next_direction__ = {
'North': 'West',
'West': 'South',
'South': 'East',
'East': 'North'
}
def __init__(self, width: int, height: int):
self.width, self.height = width, height
self.direction = "East"
self.position = (0, 0)
self.perimeter = width * 2 + height * 2 - 4
def step(self, num: int) -> None:
num %= self.perimeter
if num == 0: num = self.perimeter
while num:
x, y = self.position
if self.direction == "North":
y = self.position[1] + num
overflow = y - (self.height-1)
elif self.direction == "East":
x = self.position[0] + num
overflow = x - (self.width-1)
elif self.direction == "South":
y = self.position[1] - num
overflow = 0 - y
else: # West
x = self.position[0] - num
overflow = 0 - x
num = 0
if x < 0 or x > self.width-1 or y < 0 or y > self.height-1:
self.direction = Robot.__next_direction__[self.direction]
num = overflow
x, y = max(0, min(self.width-1, x)), max(0, min(self.height-1, y))
self.position = x, y
def getPos(self) -> List[int]:
return list(self.position)
def getDir(self) -> str:
return self.direction
Review
介绍前端通过拆分任务减少页面卡顿的文章,深度一般,但胜在详实,侧重应用层的最佳实践。
Tip
一个代码库可视化工具:CodeSee。
Share
今年的圣诞节冷冷清清的,还不如疫情放开前的状态,不过也好,带着还没有阳的大宝出来看《一个黄黄旺旺玩玩的展览》,人挺少的/没有人,希望元旦过后能逐渐热闹起来吧。
圣诞节也别忘了打卡,分享几个学习英语的工具吧~
两个比较知名的,侧重点不同:
一个适合训练单词拼写的工具:Qwerty Learner,除了常见的 4/6 级、托福、雅思等词库,还内置了编程词库:
再推荐一个 macOS 下可用的美国传统词典 - American Heritage Dictionary,它有:
- 强大的英英解释
- 词性释义丰富
- 有语源,更容易理解它的词根
等特点,比如 love 这个词:
love [lʌv]
■n.(名词)
▪A deep, tender, ineffable feeling of affection and solicitude toward a person, such as that arising from kinship, recognition of attractive qualities, or a sense of underlying oneness.
爱:对某人的一种深切,温柔,无法形容的喜爱或牵挂之情,比如由亲戚关系引起的,对有魅力的品质的发现所引起的,或一种潜伏的同一性引起的
▪A feeling of intense desire and attraction toward a person with whom one is disposed to make a pair; the emotion of sex and romance.
情欲,爱欲:对愿意与之结为配偶的人的强烈地欲望和吸引力;性冲动和浪漫的情感
▪Sexual passion.
性欲
▪Sexual intercourse.
性交
▪A love affair.
风流韵事
▪An intense emotional attachment, as for a pet or treasured object.
嗜好:一种强烈的感情依赖,比如对一个宠物或珍贵的东西
▪A person who is the object of deep or intense affection or attraction; beloved. Often used as a term of endearment.
爱人,意中人:深切或强烈喜爱或吸引的对象;被爱者。经常用于昵称
▪An expression of one's affection:
爱意:喜爱的表现:
»Send him my love.
»给予他我的爱意
▪A strong predilection or enthusiasm:
爱,狂热:强烈的偏袒或热情:
»a love of language.
»对语言的狂热
▪The object of such an enthusiasm:
爱的东西或人:这样一种狂热的对象:
»The outdoors is her greatest love.
»她最热心户外运动
▪Love (Mythology) Eros or Cupid.
Love 【神话】 爱神,丘比特
▪Often Love (Theology) Charity.
常作 Love 【神学】 慈善
▪Love (Christian Science) God.
Love 【基督教科学派】 上帝
▪(Sports) A zero score in tennis.
【体育运动】 网球中的零分
■v.(动词)
loved,lov.ing,loves
■v.tr.(及物动词)
▪To have a deep, tender, ineffable feeling of affection and solicitude toward (a person):
爱,热爱,爱戴:对(某人)有一种深切,温柔,无法形容的喜爱和牵挂:
»We love our parents. I love my friends.
»我们爱我们的父母。我爱我的朋友
▪To have a feeling of intense desire and attraction toward (a person).
爱,抚爱:对(某人)有强烈的欲望和吸引力
▪To have an intense emotional attachment to:
爱,爱恋:对…有一种强烈的感情依赖:
»loves his house.
»依恋他和房子
▪To embrace or caress.
拥抱,爱抚
▪To have sexual intercourse with.
和…性交
▪To like or desire enthusiastically:
嗜好:狂热地喜欢或想得到:
»loves swimming.
»酷爱游泳
▪(Theology) To have charity for.
【神学】 对…的慈爱
▪To thrive on; need:
需要:依靠…生存;需要:
»The cactus loves hot, dry air.
»仙人掌需要干热的空气
■v.intr.(不及物动词)
▪To experience deep affection or intense desire for another.
爱:体验对某人的深切喜爱或强烈欲望
【习惯用语】
▬for love
▪Out of compassion; with no thought for a reward:
出于同情心;不求回报:
»She volunteers at the hospital for love.
»她出于爱心主动要求在医院工作
▬for love or money
▪Under any circumstances. Usually used in negative sentences:
在任何情况下。经常用在否定句中:
»I would not do that for love or money.
»在任何情况下我都不会这样做
▬for the love of
▪For the sake of; in consideration for:
为了,出于:为了…的缘故;出于对……的考虑:
»did it all for the love of praise.
»这样做是为了得到赞扬
▬no love lost
▪No affection; animosity:
没有好感;敌意:
»There's no love lost between them.
»他们之间只有敌意
【语源】
▪Middle English
中古英语
▪from Old English (lufu) * see leubh-
源自 古英语 (lufu) *参见 leubh-
【参考词汇】
love,affection,devotion,fondness,infatuation
▪These nouns denote feelings of warm personal attachment or strong attraction to another person.
这些名词表示对另外一个人的温暖的个人依恋或强烈的吸引力。
▪ (Love) suggests a more intense feeling than that associated with the other words of this group:
(Love) 比这类词中的相关词表明一种更强烈的感情:
»married for love.
»为爱而结婚。
▪Affection is a less ardent and more unvarying feeling of tender regard:
Affection 不那么热烈,但多一点永恒不变的温柔感:
»parental affection.
»父母的慈爱。
▪Devotion is earnest, affectionate dedication;it implies a more selfless, often more abiding feeling than(love) :
Devotion 表示热心的,充满情爱的奉献;它暗指比(love) 更无私,通常更执着的感情:
»The devotion of the aged couple is inspiring.
»老人的挚爱是催人奋进的。
▪Fondness is strong liking or affection:
Fondness 是一种强烈的喜欢或喜爱:
»showed their fondness for their grandchildren by financing their education.
»通过资助孙辈们的教育来表达对他们的喜爱。
▪Infatuation is foolish or extravagant attraction,often of short duration:
Infatuation 是一种愚蠢的爱或滥爱,通常持续的时间短:
»Their infatuation blinded them to the fundamental differences in their points of view. See also Synonyms at like ¹
»他们之间的迷恋使他们看不到他们之间意见的根本分歧 参见同义词 like¹
学习一个词不是记住它的发音就可以了,而是要把它和已知词汇、生活场景联系起来,才能真正理解它的释义。
词典使用方法:
- 下载 GitHub 上的文件: 下载地址
- 解压至
~/Library/Dictionaries
;如果这个目录不存在,可以通过 Dictionary 的 「File」->「Open Dictionaries Folder」创建 在 Dictionary 的设置面板里启用该词典:
最后如果你想测试一下你的英语词汇量的话,可以试试 Preply: