일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- camera permission
- ELECTRON
- github lfs
- 이미지 데이터 타입
- react-native
- device in use
- camera access
- github pdf
- Can't resolve
- Recoil
- github 100mb
- silent printing
- html
- rolldown
- animation
- nextjs
- Each child in a list should have a unique "key" prop.
- npm package
- Git
- custom printing
- ffi-napi
- Failed to compiled
- augmentedDevice
- dvh
- 티스토리 성능
- react-native-dotenv
- adb connect
- vercel git lfs
- adb pair
- electron-packager
- Today
- Total
Bleeding edge
[LeetCode]796. Rotate String - 자바스크립트 0609 본문
https://leetcode.com/problems/rotate-string/
Rotate String - LeetCode
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
goal의 첫번째 문자를 빼서 맨 뒤에 push한 후에, s와 비교를 하는 것을 s.length만큼 반복한 뒤 둘이 같지 않다면, false를, 중간에 같은 값이 있다면 true를 반환하는 문제이다.
1; shift
문제를 풀이하면서 알게된건, 문자열의 경우 shift가 안된다는 것이 었다.
shift is not a function!
shift는 Array.prototype.shift 즉, 정렬에서만 사용할 수 있기 때문에, 주어진 s를 split("")을 하면 shift가 가능하다. 그러나 split하고 join하고 push하고 하는 것은 매우 번거로우니, 다른 방법을 선택했다.
2. [...goal.slice(1), goal[0]]
getCombination에서 자주 보이는 모양새이다. 이 역시 정렬로 나오기때문에 push의 과정은 없더라도 join을 매번해줘야 하기때문에 다른 방법으로..
3. goal.slice(1) + goal[0]
문자열로 반환되기 때문에 이로 진행하였다.
전체 풀이
var rotateString = function (s, goal) {
if(s.length!==s.length)return false
for (let i = 0; i < s.length; i++) {
if (s === goal) return true
goal = goal.slice(1) + goal[0]
}
return false
};
문제 풀이 자체는 심플했으나, xxx is not a function이 왜 뜨는지 공부를 한 것에 대해서 왜 떴고, 이를 어떻게 해결할 것인지에 대해서 고민을 해볼 수 있었다.
'코딩테스트 공부' 카테고리의 다른 글
[LeetCode] 2293. Min Max Game - 자바스크립트 0610 (0) | 2022.06.10 |
---|---|
[LeetCode] 43. Multiply Strings - 자바스크립트 0609 (0) | 2022.06.09 |
[leetcode] 34. Find First and Last Position of Element in Sorted Array - 자바스크립트 0607 (0) | 2022.06.07 |
[leetcode] 28. Implement strStr() - 자바스크립트 0607 (0) | 2022.06.07 |
[leetcode] 14. Longest Common Prefix - 자바스크립트 0607 (0) | 2022.06.07 |