日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

leetcode day1 -- Reverse Words in a String Evaluate Reverse Polish Notation Max Points on a Li

發(fā)布時間:2025/3/21 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 leetcode day1 -- Reverse Words in a String Evaluate Reverse Polish Notation Max Points on a Li 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

以前從來沒做過什么oj,發(fā)現(xiàn)做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/

去掉多連續(xù)空格時使用了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

代碼如下:用棧來實現(xiàn):

注意易錯點:(1)除0問題(2)輸入一個數(shù)字時輸出問題(3)錯誤處理問題,如果輸入出錯時輸出結(jié)果為多少

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.

分析:對每個點求和其他點的夾角的正切值和數(shù)量,建立map

易錯點:(1)特殊情況下,點的個數(shù)為0,1,,2時 (2)相同的點,肯定在一條直線上;(3)橫坐標(biāo)相同的點,正切值無窮大,單獨計算

在下面的代碼中,兩個點的正切值計算了兩次,其實可以計算一次,但是實現(xiàn)比較復(fù)雜,如果用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; //垂直線點數(shù)int samePointNum = 0; //坐標(biāo)相同的點數(shù)for( int j=0; j<points.size(); ++j){if( i == j){ //同一個點忽略continue;}//如果x坐標(biāo)相同if(points.at(i).x == points.at(j).x){//如果x,y坐標(biāo)相同,則為同一個點if(points.at(i).y == points.at(j).y){++ samePointNum;continue;}//否則,位于同一垂直線上++ vertiLineNum;}else{//根據(jù)傾斜角的正切值來建立哈希表,正切值為鍵值,值為點數(shù)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;}}}//更新最大點數(shù)maxPoints = max(vertiLineNum + samePointNum,maxPoints);for(iter = tempHash.begin(); iter!=tempHash.end(); ++iter){if((*iter).second + samePointNum > maxPoints){maxPoints = (*iter).second + samePointNum;}} }return maxPoints;} };

  

總結(jié)

以上是生活随笔為你收集整理的leetcode day1 -- Reverse Words in a String Evaluate Reverse Polish Notation Max Points on a Li的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。