[LeetCode/easy/Array] Pascal's triangle
·
알고리즘/LeetCode
https://leetcode.com/problems/pascals-triangle/ Pascal's Triangle - 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 vector generate(int numRows) { vector ans(numRows); ans[0].push_back(1); for(int i=1; i
[LeetCode/easy/Array] Merge Sorted Array
·
알고리즘/LeetCode
https://leetcode.com/problems/merge-sorted-array/ Merge Sorted Array - 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 void merge(vector& nums1, int m, vector& nums2, int n) { for(int i=0; i
[LeetCode/easy/Array] Plus One
·
알고리즘/LeetCode
https://leetcode.com/problems/plus-one/ Plus One - 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 올림 수 하나를 두고 진행한다. 만약 9라면 +1이 되면 10이 되어 자릿수가 늘어난다. 그러므로 이 값은 0으로 바꿔주고 올림수를 true로 변경해준다. 9가 아니면 그냥 +1을 진행하고 올림수는 false로 변경해준다. 모두 끝났는데, 만약 c(올림수) == true 라면 1을 붙여줘야 한다. 그런데 앞에 붙이는 것보단 뒤..
[LeetCode/easy/Array] Search Insert Position
·
알고리즘/LeetCode
https://leetcode.com/problems/search-insert-position/ Search Insert Position - 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 target value의 index 출력하기 여기서 문제는 만약 배열 안에 target value가 없을 경우다. target value가 있는지 없는지는 find 함수를 통해 iterator를 구하면 distance 함수를 사용해서 위치를 출력할 수 있다. 그런데 만약 tar..
[LeetCode/easy/Array] Remove Element
·
알고리즘/LeetCode
https://leetcode.com/problems/remove-element/ Remove Element - 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 배열 nums와 원소 val이 주어지는데, nums 내에서 val 값인 원소 모두 삭제 STL vector의 함수들을 사용하면 되는데, remove와 erase가 있다. 이 둘의 가장 큰 차이점은 remove는 값을 삭제하는 것이 아니다. 맨 뒤로 보내는 것이다. 그러고 뒤로 보내진 애들의 가장 첫 번째 ..
[LeetCode/easy/Array] Remove Duplicates from Sorted Array
·
알고리즘/LeetCode
https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Remove Duplicates from Sorted Array - 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 감소하지 않는 순서로 정렬된 nums 배열이 주어진다. 각 유니크한 요소들이 오직 한 번만 나타나도록 중복을 삭제해라. input array를 수정하는 방식으로 해야됨. 중복 수를 제거하고나서 몇 개가 나오는지 출력해라. STL v..
[LeetCode/easy] Ransom Note
·
알고리즘/LeetCode
https://leetcode.com/problems/ransom-note/ Ransom Note - 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 alpha 배열을 통해서 해당 알파벳 개수를 세고 magazine에서 해당 알파벳 개수를 감소시킨다. 만약 0보다 큰 게 있다면 ransomNote에 아직 알파벳이 남았는데 magazine이 끝난 것이므로 return false bool canConstruct(string ransomNote, string magaz..
[LeetCode/easy] Reverse Vowels of a String
·
알고리즘/LeetCode
https://leetcode.com/problems/reverse-vowels-of-a-string/ Reverse Vowels of a 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 투 포인터를 사용해서 앞 뒤로 l, r 포인터를 두고 - 둘 다 모음인 경우 swap, l++, r-- - 둘 다 자음인 경우 l++, r-- - 왼쪽만 자음 l++ - 오른쪽만 자음 r-- bool isVowel(char c){ if(c == 'a' || c =..
[LeetCode/easy] Word Pattern
·
알고리즘/LeetCode
https://leetcode.com/problems/word-pattern/ Word Pattern - 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 pattern과 v의 단어를 짝지어준다. (a-dog, b-cat) 이런 식으로. 1. 문자열 s을 공백 없이 단어들만 v에 넣어주기 (getline, sstream) 2. 문자열 v와 pattern의 길이 다르면 바로 return false 3. checkS()를 통해 현재 문자열 v와 다른 패턴이랑 짝지어진 ..
녕이
'알고리즘/LeetCode' 카테고리의 글 목록 (3 Page)