Search
Duplicate
๐Ÿ“’

Robot Bounded In Circle

์ฃผ์ฐจ
๋ฌธ์ œ๋ฒˆํ˜ธ
1041
์–ธ์–ด
ํ‹ฐ์–ด
Medium
์œ ํ˜•
๊ตฌํ˜„
์‹œ๋ฎฌ๋ ˆ์ด์…˜
์ˆ˜ํ•™
๋ฌธ์ž์—ด
nj_Blog
nj_์ƒํƒœ
์ดํ•ด๋„
66%
ํ’€์ด
์‚ฌ๋žŒ
์ดํ•ด๋„ 2
13 more properties

๋ฌธ์ œ์ ‘๊ทผ

โ€ข
G๋ฅผ ๋งŒ๋‚˜๋ฉด dir๋ณ€์ˆ˜์— ๋‹ด๊ฒจ์žˆ๋Š” ๋ฐฉํ–ฅ์ •๋ณด๋กœ ํ–ฅํ•จ
โ€ข
L์„ ๋งŒ๋‚˜๋ฉด ์Œ์ˆ˜ ๋ฐฉํ–ฅ์œผ๋กœ ๋ฐ”๊พธ๊ณ  dir์ด ์Œ์ˆ˜๊ฐ€ ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์–‘์ˆ˜๋กœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด +4
โ€ข
R์„ ๋งŒ๋‚˜๋ฉด L๊ณผ๋Š” ๋ฐ˜๋Œ€๋ฐฉํ–ฅ

๋†“์ณค๋˜ ๋ถ€๋ถ„

์ขŒํ‘œx,y์˜ ๊ฒฐ๊ณผ๊ฐ€ 0,0์ธ์ ๋ง๊ณ ๋„ dir โ‰  0์ผ๋•Œ๋„ ๋‹ต์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•จ
๊ทธ๋ž˜์•ผ ๋ฌดํ•œํžˆ ์›์„ ๊ทธ๋ฆฌ๋ฉด ์ด๋™ํ•˜๊ฒŒ๋จ

์ฝ”๋“œ

13.8 MB

4 ms

class Solution { func isRobotBounded(_ instructions: String) -> Bool { var dir = 0, x = 0, y = 0; for str in instructions { if (str == "G") { if (dir == 0) { y += 1; } else if (dir == 1) { x -= 1; } else if (dir == 2) { y -= 1; } else { x += 1; } } else if (str == "L") { dir -= 1; dir = (dir + 4) % 4; } else { dir += 1; dir %= 4; } } return (x == 0 && y == 0 || dir != 0) } }
Swift
๋ณต์‚ฌ