본문 바로가기

Algorithm/Implementation

(C++) - LeetCode (easy) 1365. How Many Numbers Are Smaller Than the Current Number

반응형

https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/description/

 

LeetCode - The World's Leading Online Programming Learning Platform

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 ans를 선언해줍니다.

📔 풀이과정

nums의 원소를 순회하며 자신보다 작은 원소들의 개수를 세줘 ans에 push_back해줍니다.

📔 정답 출력 | 반환

ans를 반환합니다.


📕 Code

📔 C++

class Solution {
public:
    vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
        vector <int> ans;
        for(int i = 0; i < nums.size(); i++) {
            int cnt = 0;
            for(int j = 0; j < nums.size(); j++) {
                if(i == j) continue;
                if(nums[i] > nums[j]) cnt++;
            }
            ans.push_back(cnt);
        }
        return ans;
    }
};

*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.