Thanks to visit codestin.com
Credit goes to github.com

Skip to content

세수의 합 질문 #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
dumi33 opened this issue Jan 26, 2022 · 4 comments
Open

세수의 합 질문 #145

dumi33 opened this issue Jan 26, 2022 · 4 comments

Comments

@dumi33
Copy link

dumi33 commented Jan 26, 2022

안녕하세요

class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        result = []
        nums.sort()
        for i in range (len(nums)-2) :
            if i > 0 and nums[i] == nums[i-1] : # 중복일 경우 pass
                continue
            left, right = i +1, len(nums)-1
            while left < right :
                sum = nums[left] + nums[right] + nums[i]
                if sum > 0 : right -=1
                if sum < 0 : left += 1 # if 면 틀린다 
                else : # sum == 0 일 때
                    result.append([nums[i],nums[left],nums[right]])
                    while left < right and nums[left] == nums[left+1] : left+=1 # if가 아니라 while
                    while left < right and nums[right] == nums[right-1] : right-=1
                    left += 1
                    right -= 1
                        
        return result
class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        result = []
        nums.sort()
        for i in range (len(nums)-2) :
            if i > 0 and nums[i] == nums[i-1] : # 중복일 경우 pass
                continue
            left, right = i +1, len(nums)-1
            while left < right :
                sum = nums[left] + nums[right] + nums[i]
                if sum > 0 : right -=1
                elif sum < 0 : left += 1 # if 면 틀린다 
                else : # sum == 0 일 때
                    result.append([nums[i],nums[left],nums[right]])
                    while left < right and nums[left] == nums[left+1] : left+=1 # if가 아니라 while
                    while left < right and nums[right] == nums[right-1] : right-=1
                    left += 1
                    right -= 1
                        
        return result

처음 코드 처럼 if를 두개 하면 틀리고 두번째 if를 elif로 바꾸면 맞습니다가 나옵니다.
그런데 이 둘이 차이가 나는 이유를 모르겠어서 여쭤보고 싶습니다!

@deopark
Copy link

deopark commented Jan 27, 2022

else 절은 sum == 0 일 때 일어나야 하는데...
위 코드는 sum > 0 때, 아래 if의 else 절이 실행될 것 같아요.

@dumi33
Copy link
Author

dumi33 commented Jan 28, 2022

sum <0 일 때, else 가 실행된다는 말씀이시죠?

@likejazz
Copy link
Collaborator

문제 풀이에 앞서 어떤 차이가 있는지 확실히 숙지하는게 좋을거 같습니다. https://www.w3resource.com/python/python-if-else-statements.php 여기에 설명이 아주 잘 나와 있네요. 이 설명을 확실히 이해하면 문제 풀이도 자연스럽게 이해될거 같습니다.

@dumi33
Copy link
Author

dumi33 commented Feb 2, 2022

넵 감사합니다!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants