인터넷에 문제가 있길래 한번 해봄.
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<string> solution(vector<int> a, vector<int> b) {
int temp, bitTemp;
vector<string> ans;
//ans = new vector<string>[a.size()];
for (int i = 0; i < a.size(); ++i) {
temp = a[i] | b[i];
ans.push_back("");
while (temp) {
bitTemp = 0x01 & temp;
temp >>= 1;
if (bitTemp) ans[i].append("#");
else ans[i].append(" ");
}
std::reverse(ans[i].begin(), ans[i].end());
}
return ans;
}
};
input:
a = [9, 20, 28, 18, 11]
b = [30, 1, 21, 17, 28]
output:
#####
# # #
### #
# ##
#####
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Solution {
public:
int solution(string src) {
int ans = 0, *ansTemp;
ansTemp = new int[3];
int index = -1;
char ch;
for (int i = 0; i < src.length(); ++i) {
ch = src.at(i);
if ('0' <= ch && ch <= '9') {
++index;
if (ch == '1' && src.at(i+1) == '0') {
++i;
ansTemp[index] = 10;
}
else {
ansTemp[index] = ch - '0';
}
}
else if (ch == 'D') { // double
ansTemp[index] = pow(ansTemp[index], 2);
}
else if (ch == 'T') { // triple
ansTemp[index] = pow(ansTemp[index], 3);
}
else if (ch == '#') { //mistake
ansTemp[index] = -ansTemp[index];
}
else if (ch == '*') {
if (index > 0) ansTemp[index-1] *= 2;
ansTemp[index] *= 2;
}
}
for (int i = 0; i < 3; ++i) {
ans += ansTemp[i];
}
delete[] ansTemp;
return ans;
}
};
input :
src = ["1S2D*3T", "1D2S#10S","1D2S0T","1S*2T*3S","1D#2S*3S","1T2D3D#","1D2S3T*"]
output :
1S2D*3T: 37
1D2S#10S: 9
1D2S0T: 3
1S*2T*3S: 23
1D#2S*3S: 5
1T2D3D#: -4
1D2S3T*: 59
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
string ToUpper(string& str) {
char ch;
for (int i = 0; i < str.length(); ++i) {
ch = str[i];
if (97 <= ch) {
ch ^= 0x20;
str[i] = ch;
}
}
return str;
}
class Solution {
public:
size_t solution(size_t cacheSize, vector<string> cites) {
size_t time = 0;
vector<string> q;
int len = 0;
if (cacheSize < 1) return 5 * cites.size();
len = 1;
q.push_back(ToUpper(cites[0])), time += 5;
vector<string>::iterator iter;
for (int i = 1; i < cites.size(); ++i) {
ToUpper(cites[i]);
if ((iter = find(q.begin(), q.end(), cites[i])) != q.end()) {
time++;
q.erase(iter);
q.push_back(cites[i]);
}
else {
time += 5;
if (len <= cacheSize) {
len++;
q.push_back(cites[i]);
}
else {
q.erase(q.begin());
q.push_back(cites[i]);
}
}
}
return time;
}
};
input :
1 : 3, ["Jeju", "Pangyo", "Seoul", "NewYork", "LA", "Jeju", "Pangyo", "Seoul", "NewYork", "LA"]
2 : 3, ["Jeju", "Pangyo", "Seoul", "Jeju", "Pangyo", "Seoul", "Jeju", "Pangyo", "Seoul"]
3 : 2, ["Jeju", "Pangyo", "Seoul", "NewYork", "LA", "SanFrancisco", "Seoul", "Rome", "Paris", "Jeju", "NewYork", "Rome"]
4 : 5, ["Jeju", "Pangyo", "Seoul", "NewYork", "LA", "SanFrancisco", "Seoul", "Rome", "Paris", "Jeju", "NewYork", "Rome"]
5 : 2, ["Jeju", "Pangyo", "NewYork", "newyork"]
6 : 0, ["Jeju", "Pangyo", "Seoul", "NewYork", "LA"]
output :
1: 50
2: 21
3: 60
4: 52
5: 16
6: 25
그냥 무작정 만들어본 코드라서 비효율적일거고, 내 코드가 정확히 테스트에서 원하는 코드인지 아닌지도 모른다.