以前从来没做过什么oj,发现做oj和在本地写代码或者纸上写差别还是很大的,觉得今天开始刷oj,特此记录一下。
1、Reverse Words in a String
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
- What constitutes a word?
A sequence of non-space characters constitutes a word. - Could the input string contain leading or trailing spaces?
Yes. However, your reversed string should not contain leading or trailing spaces. - How about multiple spaces between two words?
Reduce them to a single space in the reversed string.
此题在于看清题意,要去掉头尾的空格,单词中间多空格只保留一个,注意string类型的使用方法,查的http://www.cplusplus.com/reference/string/string/
去掉多连续空格时使用了stringstream。
运行成功的代码如下:
class Solution {
public:
void reverseWords(string &s) {
if(s.empty()){
return;
}
int start = 0;
int end = s.length()-1;
reverseWord(s,start,end);
removeSpaces(s);
int length = s.length();
for(int i=0; i <= length; ++i){
if(i == length || s.at(i) == ' ' && i>=1){
end = i-1;
reverseWord(s,start,end);
start = end = i+1;
}
}
}
private:
void reverseWord(string &s,int start, int end){
while( start < end ){
char temp = s.at(start);
s.at(start) = s.at(end);
s.at(end) = temp;
++start;
--end;
}
}
void removeSpaces(string& s){
stringstream ss;
ss << s;
string t;
s="";
while(ss >> t){
s += t+" ";
}
if(s.length()>1){
s.erase(s.end()-1);
}
}
};
2、Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +
, -
, *
, /
. Each operand may be an integer or another expression.
Some examples:
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
代码如下:用栈来实现:
注意易错点:(1)除0问题(2)输入一个数字时输出问题(3)错误处理问题,如果输入出错时输出结果为多少
class Solution {
public:
int evalRPN(vector<string> &tokens) {
int result = 0;
for(int i=0; i<tokens.size(); ++i){
int temp = operatorType(tokens.at(i));
if( temp == NUMBER){
result = atoi(tokens.at(i).c_str());
dataStack.push(result);
}else {
if(dataStack.size()<2)
{
return result;
}
int param2 = dataStack.top();
dataStack.pop();
int param1 = dataStack.top();
dataStack.pop();
switch(temp){
case ADD:
result = param1 + param2;
break;
case MINUS:
result = param1 - param2;
break;
case MULTIPLY:
result = param1 * param2;
break;
case DIV:
if(param2 == 0){
return 0;
}
result = param1 / param2;
break;
}
dataStack.push(result);
}
}
return result;
}
private:
stack<int> dataStack;
enum operat{NUMBER,ADD,MINUS,MULTIPLY,DIV};
int operatorType(string& s){
if(s.length()>1){
return NUMBER;
}else{
switch(s.at(0)){
case '+':
return ADD;
case '-':
return MINUS;
case '*':
return MULTIPLY;
case '/':
return DIV;
default:
return NUMBER;
}
}
}
};
3、Max Points on a Line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
分析:对每个点求和其他点的夹角的正切值和数量,建立map
易错点:(1)特殊情况下,点的个数为0,1,,2时 (2)相同的点,肯定在一条直线上;(3)横坐标相同的点,正切值无穷大,单独计算
在下面的代码中,两个点的正切值计算了两次,其实可以计算一次,但是实现比较复杂,如果用map来存储的话,存储point为键值时,需要自动排序,需要Point < 的定义,还是采用了下面的方法:
代码:
class Solution {
public:
int maxPoints(vector<Point> &points) {
if( points.size() <= 2){
return points.size();
}
map<float,int> tempHash;
float tanAngle = 0.0;
int maxPoints = 2;
map<float,int>::iterator iter;
for( int i=0; i< points.size(); ++i){
tempHash.clear();
int vertiLineNum = 1; //垂直线点数
int samePointNum = 0; //坐标相同的点数
for( int j=0; j<points.size(); ++j){
if( i == j){ //同一个点忽略
continue;
}
//如果x坐标相同
if(points.at(i).x == points.at(j).x){
//如果x,y坐标相同,则为同一个点
if(points.at(i).y == points.at(j).y){
++ samePointNum;
continue;
}
//否则,位于同一垂直线上
++ vertiLineNum;
}else{
//根据倾斜角的正切值来建立哈希表,正切值为键值,值为点数
tanAngle = (float)(points.at(j).y-points.at(i).y)/(points.at(j).x-points.at(i).x);
if(tempHash[tanAngle]){
++tempHash[tanAngle];
}else{
tempHash[tanAngle] = 2;
}
}
}
//更新最大点数
maxPoints = max(vertiLineNum + samePointNum,maxPoints);
for(iter = tempHash.begin(); iter!=tempHash.end(); ++iter){
if((*iter).second + samePointNum > maxPoints){
maxPoints = (*iter).second + samePointNum;
}
}
}
return maxPoints;
}
};