简单模拟题。分情况讨论边界奇偶数即可。
class Solution {
public:
int countOdds(int low, int high) {
int ans = 0;
if(low % 2 == 0) {
if(high % 2 == 0)
ans = (high - low) / 2;
else
ans = (high - low) / 2 + 1;
} else {
if(high % 2 == 0)
ans = (high - low) / 2 + 1;
else
ans = (high - low) / 2 + 1;
}
return ans;
}
};
简单模拟题。简单模拟路径即可。
class Solution {
public:
bool isPathCrossing(string path) {
int len1 = path.length();
bool flag = false;
int startx = 0, starty = 0;
map<pair<int,int>,int> p;
pair<int,int> z;
z.first = startx, z.second = starty;
p[z] += 1;
for(int i = 0; i < len1; i++) {
if(path[i] == 'N')
starty += 1;
if(path[i] == 'E')
startx += 1;
if(path[i] == 'S')
starty -= 1;
if(path[i] == 'W')
startx -= 1;
pair<int,int> temp;
temp.first = startx,temp.second = starty;
if(p[temp] >= 1) {
flag = true;
break;
} else
p[temp] += 1;
}
return flag;
}
};
简单模拟题。直接遍历寻找即可。
class Solution {
public:
int findKthPositive(vector<int>& arr, int k) {
int missCount = 0, lastMiss = -1, current = 1, ptr = 0;
for (missCount = 0; missCount < k; ++current) {
if (current == arr[ptr]) {
ptr = (ptr + 1 < arr.size()) ? ptr + 1 : ptr;
} else {
++missCount;
lastMiss = current;
}
}
return lastMiss;
}
};
简单模拟题。先统计所有1的个数,之后遍历数组,同时记录0的个数,1的个数则用总数减去遇见的1的个数即可。
class Solution {
public:
int maxScore(string s) {
int num0 = 0, num1 = 0, len1 = s.length();
for(int i = 0;i < len1; i++) {
if(s[i] == '1')
num1 += 1;
}
int max1 = -1;
for(int i = 0;i < len1 - 1; i++) {
if(s[i] == '0')
num0 += 1;
if(s[i] == '1')
num1 -= 1;
max1 = max(max1, num0 + num1);
}
return max1;
}
};
简单模拟题。直接用哈希表记录排序后的数组位置即可,相同的数由于哈希表之前肯定记录过,或者没记录过,取较小即可。
class Solution {
public:
vector<int> arrayRankTransform(vector<int>& arr) {
vector<int> ans;
vector<int> sum = arr;
sort(sum.begin(),sum.end());
map<int,int> p;
int len1 = arr.size(), start = 1;
for(int i = 0; i < len1; i++) {
if(p[sum[i]] != 0)
continue;
else
p[sum[i]] = start;
start += 1;
}
for(int i = 0; i < len1; i++)
ans.push_back(p[arr[i]]);
return ans;
}
};
简单模拟题。先统计所有数字的个数和字母的个数,判定二者差值是否大于1,大于1则不行,小于等于1则行,之后较大的放在第一个字符,然后穿插即可得到答案。
class Solution {
public:
string reformat(string s) {
string ans = "", tmp1 = "", tmp2 = "";
for (int i = 0; i < s.size(); ++i) {
if (isdigit(s[i]))
tmp1 += s[i];
else
tmp2 += s[i];
}
int n1 = tmp1.size(), n2 = tmp2.size();
if (abs(n1 - n2) > 1)
return ans;
int n = min(n1, n2);
for (int i = 0; i < n; ++i)
ans = ans + tmp1[i] + tmp2[i];
if (n1 > n2)
ans += tmp1.back();
else {
if (n1 < n2)
ans = tmp2.back() + ans;
}
return ans;
}
};
简单模拟题。按照题目意思直接模拟完成即可。
class Solution {
public:
string makeGood(string s) {
for(int i = 0; i < s.length() - 1; i++) {
if(s[i] >= 'a' && s[i] <= 'z') {
if(s[i+1] >= 'A' && s[i+1] <= 'Z') {
int temp = s[i] - s[i+1];
if(temp == 32) {
s.erase(i,2);
i = -1;
if(s.length() == 0)
break;
}
}
} else {
if(s[i] >= 'A' && s[i] <= 'Z') {
if(s[i+1] >= 'a' && s[i+1] <= 'z') {
int temp = s[i+1] - s[i];
if(temp == 32) {
s.erase(i,2);
i = -1;
if(s.length() == 0)
break;
}
}
}
}
}
return s;
}
};
简单模拟题。按照题目意思生成所有数值然后找出最大值即可。
class Solution {
public:
int getMaximumGenerated(int n) {
int num[211] = {0};
num[0] = 0, num[1] = 1;
for(int i = 1; i <= n; i++) {
if((2*i) >= 2 && (2*i) <= n)
num[2*i] = num[i];
if((2*i) + 1 >= 2 && (2*i) + 1 <= n)
num[2*i + 1] = num[i] + num[i + 1];
}
int max1 = -1;
for(int i = 0;i <= n; i++)
max1 = max(max1, num[i]);
return max1;
}
};
简单模拟题。求取前后二者相减的最大差值即可。
class Solution {
public:
char slowestKey(vector<int>& releaseTimes, string keysPressed) {
map<char,int> hash_map;
for(int i = 0; i < releaseTimes.size(); i++) {
if(i == 0)
hash_map[keysPressed[i]] = releaseTimes[i];
else {
int value=releaseTimes[i] - releaseTimes[i-1];
if(value > hash_map[keysPressed[i]])
hash_map[keysPressed[i]] = value;
}
}
char res;
int max_value=0;
for(map<char,int>::iterator it1 = hash_map.begin();it1 != hash_map.end(); it1++) {
if(it1->second >= max_value) {
max_value = it1->second;
res = it1->first;
}
}
return res;
}
};
简单模拟题。直接计算二者与1971年1月1日的差值,然后求出差值即可。
class Solution {
bool leap_year(int year) {
return ((year % 400 == 0) || (year % 100 != 0 && year % 4 == 0));
}
int date_to_int(string date) {
int year, month, day;
sscanf(date.c_str(), "%d-%d-%d", &year, &month, &day);
int month_length[] = {31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30};
int ans = 0;
while (year != 1971 or month != 1 or day != 1) {
++ans;
if (--day == 0)
if (--month == 0)
--year;
if (day == 0) {
day = month_length[month];
if (month == 2 && leap_year(year))
++day;
}
if (month == 0)
month = 12;
}
return ans;
}
public:
int daysBetweenDates(string date1, string date2) {
return abs(date_to_int(date1) - date_to_int(date2));
}
};