728x90
반응형

문제풀이(Problem Solving) 326

Leetcode 26. Remove Duplicates from Sorted Array

https://leetcode.com/problems/remove-duplicates-from-sorted-array  정답 코드class Solution {public: int removeElement(vector& nums, int val) { int k = 0; for(int i = 0; i 생각의 흐름1. 이미 정렬되어 있다고?2. 중복만 제거하라고?3. 입출력 제한은 딱히 없고.. 뒤에 순서도 상관없다고?4. 같은 것은 묶고 다른게 나올 때 새로 시작하면 될 것 같은데?5. 첫 숫자를 기준으로 다른 숫자가 나올 때까지 지워야하겠는데?6. 다른 숫자가 나오면 그 숫자가 기준이 되어야 겠다.7. 엣지케이스(맨 앞 또는 맨 끝 케이스 있나 보자)

Leetcode 88. Merge Sorted Array (C++)

오랜만에 문제를 풀어봤다.머리가 잘 안돌어가더라import할 헤더도 기억이 잘 안나고.. 막 멤버 함수도 기억이 안나고 그래서 릿코드를 가져왔다. https://leetcode.com/problems/merge-sorted-array 정답 코드는 아래와 같다.class Solution {public: void merge(vector& nums1, int m, vector& nums2, int n) { // for two pointer for two arrays int i = m - 1, j = n - 1, k = m + n - 1; // evade null pointer while(i >= 0 && j >= 0) { ..

백준, BOJ, 5582번, 공통 부분 문자열 C++ [CPP] ★★★

이 문제는 알고리즘을 알 경우 쉽게 풀 수 있다. 쉽게 생각하기는 어려운데 알고 있다면 바로 풀 수 있다. https://www.acmicpc.net/problem/5582 5582번: 공통 부분 문자열 두 문자열이 주어졌을 때, 두 문자열에 모두 포함된 가장 긴 공통 부분 문자열을 찾는 프로그램을 작성하시오. 어떤 문자열 s의 부분 문자열 t란, s에 t가 연속으로 나타나는 것을 말한다. 예를 들 www.acmicpc.net #맞은 풀이 #include using namespace std; //LCS[i][j] => s1의 i번째 문자열과 s2의 j번째 문자열까지의 최장 공통 문자열수 int LCS[4001][4001]; int main(){ string s1,s2; cin >> s1 >> s2; in..

프로그래머스, 셔틀버스, C++ [CPP] ★★★★

어려우면서도 쉬운 문제다. 다시 말하자면 생각은 쉬운데 구현이 어렵다. https://school.programmers.co.kr/learn/courses/30/lessons/17678 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr #맞은 풀이 #include #include #include #include #include #include using namespace std; string func(int x){ if(x>= 10){ return to_string(x); }else{ return "0" + to_string(x); } } string sol..

프로그래머스, 방금그곡, C++ [CPP] ★★★★★

https://school.programmers.co.kr/learn/courses/30/lessons/17683 문자열 처리를 위한 문제다. C++로는 어렵지만 stringstream으로 헤쳐나가보자 중요하다. c++의 문자열 처리는 stringstream에게 맡기라고 그리고 #도 한글자로 쳐야하는 처리를 해주는게 까다롭다. #맞는 풀이 #include #include #include #include #include using namespace std; struct mus{ int playtime; int idx; string title; }; bool comp(mus a, mus b){ if(a.playtime == b.playtime){ return a.idx < b.idx; } return a.p..

프로그래머스, 파일명 정렬, C++ [CPP] ★★★★★

https://school.programmers.co.kr/learn/courses/30/lessons/17686# 어렵지 않다. 다만 까다로울 수 있다. 특히 number랑 head를 분리할 때 신경써주어야 한다. #맞은 풀이 #include #include #include #include using namespace std; struct File{ string head = ""; string number = ""; string totalName = ""; int idx = -1; }; bool cmp (const File &a, const File &b){ string head1 =""; string head2 =""; int number1 = -1; int number2 = -1; for(char ..

프로그래머스, 프렌즈4블록, C++ [CPP] ★★★★

https://school.programmers.co.kr/learn/courses/30/lessons/17679 이건 그냥 구현문제다. 블럭 게임은 그냥 말한대로 구현만 하면된다. 하지만 어떤 순서로 해야할지 내가 정해야 하는 그런 문제다. 어렵다. 생각하기는 어렵지만 생각만 하면 구현할 수 있다,. #맞은 풀이 #include #include #include #include using namespace std; int solution(int m, int n, vector board) { int answer = 0; //m = 행, row //n = 열, col auto temp = board; vector queue(n); //n개의 열을 큐로 만듦 //board에서 4개쌍을 찾아야함 while(1)..

728x90
반응형