leetcode day1 -- Reverse Words in a String Evaluate Reverse Polish Notation Max Points on a Li
以前從來沒做過什么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".
click to show clarification.
Clarification:- 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:
代碼如下:用棧來實現:
注意易錯點:(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;} };
總結
以上是生活随笔為你收集整理的leetcode day1 -- Reverse Words in a String Evaluate Reverse Polish Notation Max Points on a Li的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 轻松搞定面试中的红黑树问题
- 下一篇: leetcode day2 -- Sor