easy
-
LEETCODE - 1089. Duplicate Zeros릿코드(LEETCODE) 2020. 6. 7. 20:09
https://leetcode.com/problems/duplicate-zeros/ vector int 어레이에 원소의 값이 0 이면 한칸식 0으로 채우주고 shift 하는 문제 1101 -> 1100 12012001 -> 12001200 class Solution { public: void duplicateZeros(vector& arr) { // 임시 vector int 를 만들고 0 이면 한번더 넣어 준다. // 괜히 머리아프게 shift 하지 말자 쉽게 풀리면 쉽게 풀리는 방법으로 풀자 vector tmp; for (int i = 0; i < arr.size(); i++) { int value = arr[i]; tmp.push_back(value); if (value == 0) { tmp.push..
-
LEETCODE - 1470. Shuffle the Array릿코드(LEETCODE) 2020. 6. 7. 19:38
https://leetcode.com/problems/shuffle-the-array/ 어레이에 있는 요소를 shuffle 하는 문제 입력으로 들어오는 인자 n 위치와 current 위치에 넣어 주면 된다. class Solution { public: vector shuffle(vector& nums, int n) { vector arr; const int size = nums.size(); for(int i=0; i
-
485. Max Consecutive Ones릿코드(LEETCODE) 2020. 5. 2. 17:30
https://leetcode.com/problems/max-consecutive-ones/ 111100011111 글자가 있을때 연속되는 가장긴 1의 개수를 찾는 문제 111100011111 -> 1 연속으로 4 111100011111 -> 1 연속으로 5 class Solution { public: int findMaxConsecutiveOnes(vector& nums) { int sol = 0; int count = 0; for(auto a:nums) { if(a == 1) { count++; } else { count = 0; } sol = max(sol, count); } return sol; } };
-
jewels and stone릿코드(LEETCODE) 2020. 5. 2. 17:16
https://leetcode.com/problems/jewels-and-stones 문자열 J 는 보석을 의미하고, 문자열 S 는 돌과보석의 의미를 담은 문자열이다. 문자열 S 에서 J 의 보석이 몇 개 있는지 찾는 문제이다. 제약 사항 중에 Letters are case sensitive 라고 명시 되어 있으니 대소문자를 구별 한다는 점에 유의 하자. 예제의 문자열 J = 'aA', S = 'aAAbbbb' J 문자열 원소 개수 S 문자열에서 위치 a 1 aAAbbb A 2 aAAbbb 총합 3 // 2020-02-03 class Solution { public: int numJewelsInStones(string J, string S) { int sol = 0; const int size = J.s..
-
278. First Bad Version릿코드(LEETCODE) 2020. 5. 2. 16:58
https://leetcode.com/problems/first-bad-version/ 당신은 제품 관리자이며 현재 새 제품 개발 팀을 이끌고 있다. 불행히도 당신의 최근 제품은 품질 검사를 통과하지 못했다. 각 버전은 이전 버전 기반으로 개발되었음으로 불량 버전 이후의 모든 보전도 불량이다. // TODO 이문제는 첫 번째 불량이 발생하는 새제품을 찾는 문제인대 문제의 조건을 봐서는 어디 부터 접근해야 하는지 알 수가 없었다. Topic 에서 binary-search 나와서 접근해서 풀긴 했지만 문제 정의가 조금 아쉬운 문제다. // The API isBadVersion is defined for you. // bool isBadVersion(int version); class Solution { pu..
-
283. Move Zeroes릿코드(LEETCODE) 2020. 4. 9. 22:21
https://leetcode.com/problems/move-zeroes/ Input: [0,1,0,3,12] Output: [1,3,12,0,0] 벡터의 요소에서 0 인 수를 뒤로 shift 하는 문제 vector local 하나를 선언해서 0 채우고 nums 벡터의 0 이 아닌 숫자일때 arr 배열에 숫자를 넣어주고 offset 을 증가하는 식으로 풀었다. bruth-force 풀이 class Solution { public: void moveZeroes(vector& nums) { vector arr(nums.size()); fill(arr.begin(), arr.end(), 0); int pos = 0; for(int i=0;i
-
53. Maximum Subarray릿코드(LEETCODE) 2020. 4. 9. 22:09
https://leetcode.com/problems/maximum-subarray/ 부분합을 이용해서 전개함 Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: [4,-1,2,1] has the largest sum = 6. 연속되는 벡터에서 합이 가장큰 부분을 찾아서 출력함 bruth-force class Solution { public: int maxSubArray(vector& nums) { int sol = nums[0]; for(int i=0; i