Skip to content

Conversation

deipanema
Copy link
Member

구현 방법 1

  1. 전개 연산자를 이용해서 매개변수 문자열을 배열 변수에 할당해줍니다.
  2. result 변수를 선언합니다. 나중에 결과로 반환할 거예요.
  3. currnet 변수 값을 배열 길이를 할당합니다.
  4. while 반복문을 돌려요.
    4-1. 조건식으로 current가 0보다 클때까지 돌립니다.
    4-2. current를 -1 합니다.
    4-3. current 인덱스에 맞는 배열 요소를 result에 저장합니다.
  5. result와 String(x)가 동일한지 확인합니다. 이것이 Output이에요.

아이디어

  • 문자열을 받아오고 마지막 인덱스부터 차례대로 0번째까지 문자열을 저장해서 매개변수 문자열과 동일값인지 비교합니다.

구현 방법 2

  1. 구현 방법 1의 1~3까지 동일
  2. for문 조건식이 배열 길이의 반입니다.
    2-1. 맨 앞 인덱스와 맨 뒤에서 i를 뺀만큼의 인덱스를 확인하고 같지 않을 때 return false를 반환합니다.

아이디어

  • 문자열을 반으로 잘라서 맨앞과 맨뒤에서 가운데로 포인터를 움직이며 확인합니다. O(N)만큼 돌리는 것 보다 반으로 줄어 듭니다.
  • 근데 구현 방법 1보다 시간 복잡도가 심각하게 좋아지지는 않음... 10ms 좋아지더라고😂

@deipanema deipanema self-assigned this Mar 19, 2023
@deipanema deipanema linked an issue Mar 19, 2023 that may be closed by this pull request
}

return true;
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'Explanation: ' 부분은 구현 안하셨나요??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 처음 이 문제 받았을 때는 구현을 했었는데요~!
리트코드에서 그냥 넣어주는 거였더라고요🤣

}

return true;
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

바뀐 구현 방법이 코드가 간결하고 더 쉽게 구현이 되는것 같습니다.
10ms 라도 빨라진게 많이 바뀐것일 겁니다.
👍 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제 PR 글 내용도 읽어보시는군여😏

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

당근 다봐야죠 ㅎㅎㅎㅎ

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

Successfully merging this pull request may close these issues.

[LeetCode] 9. Palindrome Number
2 participants