본문 바로가기

IT/알고리즘90

6] Leetcode 771. Jewels and Stones Jewels and Stones - 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 보석 이름이 주어졌을 때 가지고 있는 돌 중에 보석인 것의 개수를 반환하는 문제. class Solution { public: int numJewelsInStones(string J, string S) { int ans = 0; for(int i = 0;i 2020. 6. 21.
5] Leetcode 1342. Number of Steps to Reduce a Number to Zero Number of Steps to Reduce a Number to Zero - 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 주어진 숫자를 짝수면 반으로 나누고, 홀수면 -1해서 0으로 만드는데 걸리는 연산 횟수를 반환하는 문제다. class Solution { public: int numberOfSteps (int num) { int ans = 0; while(num !=0){ if(num %2 == 0){ num /=2; ans++; } else{ num-.. 2020. 6. 21.
4] Leetcode 1108. Defanging an IP Address 숫자와 .으로 구성된 문자열이 주어졌을 때 . 앞뒤로 []를 넣어주는 문제. Defanging an IP Address - 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 주어진 문자열 처음부터 보면서 숫자면 그냥 추가하고 . 을 만나면 앞뒤로 대괄호를 넣어주면 된다. class Solution { public: string defangIPaddr(string address) { string ans; for(int i = 0;i 2020. 6. 21.
3] Leetcode 1431. Kids With the Greatest Number of Candies candies 배열과 extra 숫자가 주어질 때 배열의 각 숫자들이 extra 값을 더했을 때 배열에서 가장 큰 값이 될 수 있으면 true, 아니면 false를 반환하는 문제. 배열에서 최댓값 구하고 처음부터 돌면서 extra 더했을 때 최댓값과 비교한다. class Solution { public: vector kidsWithCandies(vector& candies, int extraCandies) { vector ans; int maxNum = 0; for(int i = 0;i maxNum) maxNum = candies[i]; for(int i = 0;i= maxNum) ans.push_back(true); else ans.push_back(false); return ans; } }; 2020. 6. 18.