@dyk
2015-11-04T00:45:25.000000Z
字数 4382
阅读 442
Algorithm Workspace
LeetCode
大意:给定一个整数array,给定一个target,要求找出两个array中的数a,b,要a+b=target
解法:rbtree,hash map
class Solution {public:// 其实二分查找也可以,使用RB树也可以,另外题目的对于output的要求并不是十分明确vector<int> twoSum(vector<int>& nums, int target) {int query,count;vector<int> v(2);map<int,int> mymap;vector<int>::iterator it;map<int,int>::iterator fit;it = nums.begin();for(count = 1; it != nums.end(); it++, count++){mymap.insert(pair<int,int>(*it,count));}for(count = 1, it = nums.begin() ; it != nums.end(); it++, count++){//cout<<"Now query "<<*it<<endl;query = target - *it;fit = mymap.find(query);if(fit != mymap.end()){//cout<<"in"<<endl;if(count < fit->second){v[0] = count;v[1] = fit->second;break;}else if(count > fit->second){v[1] = count;v[0] = fit->second;break;}}}return v;}};
hinocoder , 2015-09-16年网易游戏开发第三题
建立操作符栈,以及操作数栈,并且记录每个括号内的操作数个数。
Input Examples:
5
(+ 1 (* 2 3)))
(2 3)
(- 3 2 1)
(+ (+ 1 2) (* 2 3) (- 2 1))
(- 2)
Output Examples:
invalid expression
invalid expression
invalid expression
10
-2
#include <iostream>#include <cstdio>#include <stack>#include <fstream>using namespace std;int main(){int casenm;stack<char> op_stack;stack<int> opd_stack;stack<int> opdn_stack;char op;int opd , opdn, ans;char exp[1000001];int flag = 1;ifstream ifs;ifs.open("./input.txt");if(!ifs){return 0;}ifs>>casenm;ifs.getline(exp,1000000);//cin>>casenm;//cin.getline(exp,1000000);for(int k = 0; k < casenm ; k++){// cin.getline(exp,1000000);ifs.getline(exp,1000000);flag = 1;opd = -1;opdn = 0;while(!op_stack.empty()){op_stack.pop();}while(!opd_stack.empty()){opd_stack.pop();}while(!opdn_stack.empty()){opdn_stack.pop();}for(int i = 0; exp[i] != '\0' ; i++){if( exp[i] == '(' || exp[i] == '+' || exp[i] == '-' || exp[i] == '*' ){op_stack.push(exp[i]);if(exp[i] == '('){opdn_stack.push(opdn);opdn = 0;if(opd != -1){// 操作数连着左括号的情况opd_stack.push(opd);opdn++;opd = -1;}}}else if(exp[i] == ')'){//开始退站计算, 减号只能有2以下个参数 ,加号至少有 1个参数 , 乘号至少有 2 个if(op_stack.empty()){// 如果符号栈空则错误flag = 0;break;}else{op = op_stack.top();op_stack.pop();}//如果存在操作数先将操作数进栈if(opd != -1){opd_stack.push(opd);opd = -1;opdn++;}if(op == '-'){//if(opdn == 2){ans = opd_stack.top();opd_stack.pop();ans = opd_stack.top() - ans;opd_stack.pop();}else if(opdn == 1){ans = -1 * opd_stack.top();opd_stack.pop();}else if(opdn > 2){flag = 0;break;}}else if(op == '+'){ans = 0;for(int i = 0; i < opdn ; i++){ans = ans + opd_stack.top();opd_stack.pop();}}else if(op == '*'){if(opdn < 2){flag = 0;break;}else{ans = 1;for(int i = 0; i < opdn ; i++){ans = ans * opd_stack.top();opd_stack.pop();}}}else if(op == '('){//没有操作符的情况flag = 0;break;}if( !op_stack.empty() && op_stack.top() == '('){op_stack.pop();}else if(!op_stack.empty() && op_stack.top() != '('){// 反向括号不对应的情况 对应了其他符号flag = 0;break;}else if(op_stack.empty()){// 反向括号不对应的情况 没有对应了其他符号flag = 0;break;}// 一个括号的运算结果入栈,这个结果可能被上一层的括号使用opd_stack.push(ans);if(!opdn_stack.empty()){// 将上一层opdn的数量弹出opdn = opdn_stack.top();opdn_stack.pop();}else{flag = 0;break;}opdn++; // 上一层的opd数量+ 1opd = -1;}else if(exp[i] >= '0' && exp[i] <= '9'){if(opd == -1){// opd 开始情况opd = 0;}opd = opd * 10 + exp[i] - '0';}else if(exp[i] == ' '){if(opd != -1){// 空格情况,如果存在opd则将其入栈opd_stack.push(opd);opdn++;opd = -1;}}// else if(exp[i] == '\0' || 0){// if(opd != -1){// opd_stack.push(opd);// opdn++;// opd = -1;// }// }}if(op_stack.empty()){if( opdn == 1 && flag != 0){printf("%d\n", opd_stack.top() );}else{printf("invalid expression\n");}}else{printf("invalid expression\n");}}return 0;}
给一个字符串,以及row数(行数)。摆出N行的,之后在一行一行的输入。
根据N型的特点计算每一个位置的字符。
class Solution {public:string convert(string s, int numRows) {int len = s.length();int tmp, dis;dis = 2 * numRows - 2;if(dis == 0)// 处理字符长度为1的情况dis = 1;char ans[len+1];ans[len] = '\0';for(int i = 0, cnt = 0 ; i < numRows ; i++){if( i == 0 ){tmp = 0;while(tmp < len){ans[cnt++] = s[tmp];tmp = tmp + dis;}}else if(i == numRows - 1){tmp = numRows - 1;while(tmp < len){ans[cnt++] = s[tmp];tmp = tmp + dis;}}else{tmp = 0;while(tmp - i < len){if(tmp - i > 0){ans[cnt++] = s[tmp-i];}if(tmp + i < len){ans[cnt++] = s[tmp+i];}tmp = tmp + dis;}}}return ans;}};
将给定的int反转后输出,例如123输出321。符号相同。
唯一需要注意的一点就是反转之后数字可能会溢出int。
判断两个int溢出的方法可以反向验证,例如
A=B+C,是否溢出只需要验证A-B=C?。其他计算式同理。
class Solution {public:int reverse(int x) {int ans, flag;ans = 0;if(x < 0){flag = -1;x = x * flag;}else flag = 1;while(x){ans = ans*10 + x%10;if(ans - x%10 != (ans/10)*10)return 0;x = x/10;}return flag*ans;}};
可以在计算之前先试用INT_MAX或者INT_MIN对计算进行判断。
例如
if(flag == 1){if(ans > INT_MAX / 10){return INT_MAX;}else if(ans > (INT_MAX - (str[k] - '0') * flag )/10 )return INT_MAX;}else if(flag == -1){if(ans < INT_MIN / 10){return INT_MIN;}else if(ans < (INT_MIN - (str[k] - '0') * flag)/10)
水仙花字符串,可以采用遍历所有的可能字串进行判断,当然字串需要划分是偶数串还是奇数串进行判断。
水仙花数,在没有额外空间的情况下判断一个数是不是水仙花数。