웅재의 코딩세상
[프로그래머스] 문자열, 사칙연산, 시뮬레이션, 2차원 배열 본문
- 숨어잇는 숫자의 덧셈 (2)
#include <string>
#include <vector>
using namespace std;
int solution(string my_string) {
int answer = 0, n=0;
for(int i=0; i< my_string.size(); i++){
if(isdigit(my_string[i])) n = n*10 + my_string[i]-'0';
else{
answer += n;
n =0;
}
}
answer += n;
return answer;
}
- 안전지대
#include <string>
#include <vector>
using namespace std;
vector<int> x={-1,0,1,-1,1,-1,0,1};
vector<int> y={1,1,1,0,0,-1,-1,-1};
int solution(vector<vector<int>> board) {
int answer = 0;
for(int i=0; i< board.size(); i++){
for(int j=0; j<board[i].size(); j++){
if(board[i][j]==1){
answer++;
board[i][j]++;
for(int k=0; k<8; k++){
int dx = i + x[k];
int dy = j + y[k];
if((-1 < dx && dx < board.size())
&&(-1 < dy && dy < board.size())
&&(board[dx][dy]==0)){
board[dx][dy] = 2;
answer++;
}
}
}
}
}
return board.size()*board.size() - answer;
}
- 삼각형의 완성 조건 (2)
#include <string>
#include <vector>
using namespace std;
int solution(vector<int> sides) {
if(sides[0] > sides[1]) swap(sides[0],sides[1]);
return sides[0] * 2 -1;
}
- 외계어 사전
#include <string>
#include <vector>
using namespace std;
int solution(vector<string> spell, vector<string> dic) {
int answer = 2;
int arr[26];
int check[26];
for(int i=0; i< spell.size(); i++){
arr[spell[i][0]-'a']=1;
}
for(int i=0; i<dic.size(); i++){
int num=0;
for(int j=0; j<dic[i].size(); j++){
if(arr[dic[i][j]-'a']==1 && check[dic[i][j]-'a']==0){
check[dic[i][j]-'a']=1;
num++;
}
else break;
}
if(num==spell.size()){
answer =1;
break;
}
for(int j=0; j<26; j++){
check[j]=0;
}
}
return answer;
}
반응형
'코딩테스트 > 프로그래머스 - LV 0' 카테고리의 다른 글
[프로그래머스] 배열, 정렬, 문자열 (0) | 2024.01.19 |
---|---|
[프로그래머스] dp, 수학, 조건문, 배열 (0) | 2024.01.19 |
[프로그래머스] 수학, 시뮬레이션, 문자열, 사칙연산 (0) | 2024.01.16 |
[프로그래머스] 문자열, 배열, 조건문 (1) | 2024.01.13 |
[프로그래머스] 문자열, 수학, 조건문, 정렬 (1) | 2024.01.13 |