第一题
class Solution { public: /** * * @param arr int整型vector * @return int整型 */ int Answerofjudge(vector<int>& arr) { // write code here typedef long long ll; double sum=0; double ave; sort(arr.begin(),arr.end()); for(int i=0;i<arr.size();i++){ sum+=arr[i]; } ave=(double)(sum/arr.size()); double mid; int n=arr.size(); if(n%2==0){ mid=(double)(arr[(n-1)/2]+arr[(n+1)/2])/2; } else{ mid=arr[n/2]; } if(mid>ave) return 1; if(mid==ave) return 0; else return -1; } };第二题:
class Solution { public: int Maximumlength(string x) { int n = 0, p = 0, y = 0, j = 0; int res = 0; for (int i = 0; i < x.size(); i++) { if (x[i] == 'n') n++; else if (x[i] == 'p') p++; else if (x[i] == 'y') y++; while (n >= 1 && p >= 1 && y >= 1) { res = max(res, i - j); if (x[j] == 'n') n--; else if (x[j] == 'p') p--; else if (x[j] == 'y') y--; j++; } res = max(res, i - j + 1); } return res; } };
第三场:
class Solution { public: long long solve(string str) { typedef long long ll; if (str.size() == 1) { return (str[0] - '0'); } stack<ll>num; int temp = 0; for (int i = 0; i < str.size(); i++) { if (str[i] >= '0' && str[i] <= '9') temp = temp * 10 + str[i] - '0'; if (str[i] == '#') { num.push(temp); temp = 0; } else if (str[i] == '+') { ll a = num.top(); num.pop(); ll b = num.top(); num.pop(); num.push(a + b); } else if (str[i] == '-') { ll a = num.top(); num.pop(); ll b = num.top(); num.pop(); num.push(b - a); } else if (str[i] == '*') { ll a = num.top(); num.pop(); ll b = num.top(); num.pop(); num.push(a * b); } } ll res = num.top(); return num.top(); } };
全部评论
(0) 回帖