일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- omscs
- 위니펙
- machine learning
- cpp
- LAA
- 선형대수
- 머신러닝
- 딥러닝
- mpnp
- 주정부이민
- C++
- 개발자
- 조지아텍
- 프로그래머스
- EOI
- Plotting
- leetcode
- 코딩테스트
- 기본
- 컴퓨터과학과
- zeros
- 매트랩
- LV1
- Deep learning
- 온라인석사
- 방송통신대학교
- 캐나다 영주권
- 방통대
- MATLAB
- 알고리즘
Archives
- Today
- Total
Byte by Byte
821. Shortest Distance to a Character.cpp 본문
🔑 주어진 s의 index를 기준으로, 찾고자하는 c가, 왼쪽/오른쪽으로 얼마나 떨어져있는지 각각 계산하고, 그 거리중 짧은 거리를 채택한다. 단, 찾아도 없을 경우를 대비해서, flag를 만들어야 한다.
class Solution {
public:
vector<int> shortestToChar(string s, char c) {
vector<int>res;
for(int i =0; i< s.size(); i++){
int righttmp=-1;
int lefttmp=-1;
int ruse=0;
for(int j = i; j<s.size(); j++){
righttmp++;
if(s[j]==c){
ruse =1;
break;
}
}
int luse =0;
for(int k = i; k>=0; k--){
lefttmp++;
if(s[k]==c){
luse =1;
break;
}
}
if(luse == 0) lefttmp = INT_MAX;
if(ruse == 0) righttmp = INT_MAX;
res.push_back(min(righttmp,lefttmp));
}
return res;
}
};
'개발 로그 > 알고리즘' 카테고리의 다른 글
1002. Find Common Characters.cpp (0) | 2021.11.19 |
---|---|
1030. Matrix Cells in Distance Order.cpp (0) | 2021.11.19 |
1356. Sort Integers by The Number of 1 Bits.cpp (0) | 2021.11.07 |
1337. The K Weakest Rows in a Matrix.cpp (0) | 2021.10.30 |
1935. Maximum Number of Words You Can Type.cpp (0) | 2021.10.27 |