본문 바로가기
python-algorithm

leetcode 3028. Ant on the Boundary

by 무적김두칠 2024. 2. 6.

https://leetcode.com/problems/ant-on-the-boundary/description/

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

1
2
3
4
5
6
7
8
class Solution:
    def returnToBoundaryCount(self, nums: List[int]) -> int:
        location = answer = 0        
        for num in nums:
            location += num
            if location == 0:
                answer += 1
        return answer
cs

처음에는 원점(원문에는 boundary 라고 표현합니다)으로 돌아올수 있는지 아닌지 를
묻는 문제인줄알고 누적합을 사용했는데

그게 아니라 돌아올수있을때마다 answer 에다가 더하기 1을 해줘야되네요

반응형

댓글