以前从来没做过什么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:

  ["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;}
};




leetcode day1 -- Reverse Words in a String Evaluate Reverse Polish Notation Max Points on a Li相关推荐

  1. leetcode - 150. Evaluate Reverse Polish Notation

    前言:记录一下leetcode上的一道题目: 题目网址:https://leetcode.com/problems/evaluate-reverse-polish-notation/descripti ...

  2. LeetCode 150. Evaluate Reverse Polish Notation

    LeetCode 150. Evaluate Reverse Polish Notation Solution1: 参考网址:http://www.cnblogs.com/grandyang/p/42 ...

  3. C#LeetCode刷题之#344-反转字符串​​​​​​​(Reverse String)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3933 访问. 编写一个函数,其作用是将输入的字符串反转过来. 输 ...

  4. leetcode 151 Reverse Words in a String (python)

    leetcode 151   Reverse Words in a String 题目描述: Given an input string, reverse the string word by wor ...

  5. leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String

    557. Reverse Words in a String III 最简单的把空白之间的词反转 class Solution { public:string reverseWords(string ...

  6. 345. Reverse Vowels of a String - LeetCode

    Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...

  7. LeetCode 151. Reverse Words in a String

    151. Reverse Words in a String Given an input string, reverse the string word by word. For example, ...

  8. Leetcode 345: Reverse Vowels of a String

    问题描述: Given a string s, reverse only all the vowels in the string and return it. The vowels are 'a', ...

  9. Reverse Vowels of a String (反转字符串中的母音)

    leetcode Reverse Vowels of a String 反转字符串中的母音 一.学习要点: 1.find_first_of:查找与字符串str中某个字符相同的位置,并返回他的第一个出现 ...

最新文章

  1. java中的foreach
  2. 面试官扎心一问:Tomcat 在 SpringBoot 中是如何启动的?
  3. 基于MNIST数据集的Batch Normalization(批标准化层)
  4. 在WinMain中嵌Console窗口
  5. img src请求后台值值能判断_图片src拼接后台返回ID
  6. 在数据库中存储层次型数据
  7. 搜狐畅游一面(c++)
  8. 重置浏览器的css,css重置浏览器默认样式
  9. 华为云认证题库哪里有?华为认证哪个级别好呢?
  10. 无线充qi协议c语言详解,QI无线充通信协议数据包格式解析
  11. matlab format rat,format rat
  12. 钟道隆逆向英语学习法—乐在英语中
  13. 观远数据完成2.8亿元C轮融资
  14. N-Tiers开发方式(ASP/ASP.NET、VB6/VB.NET呼叫使用COM+组件)
  15. Dom对象和java
  16. 风雨砥砺,岁月如歌——Ts之箭头函数
  17. 关于 Github.io 域名访问遭拒解决记录
  18. openlayers根据半径绘制圆形,多圆连线并标记距离
  19. pandas 数据结构--DataFrame
  20. 刷题第八天(贪心加前缀和,待更新)

热门文章

  1. numpy.tril详解
  2. 网上订票抢票攻略(亲测)
  3. 数据挖掘导论读书笔记6关联分析的高级概念
  4. Re:从0开始的微服务架构:(一)重识微服务架构--转
  5. Spring(AbstractRoutingDataSource)实现动态数据源切换--转载
  6. 元宇宙企业大比拼:云宇宙数据中台:iwemeta.com
  7. Transformer 在美团搜索排序中的实践
  8. 时隔 17 年,美科技股终于“收复失地”,但这次不是泡沫了
  9. 云白条,做有温度的金融,帮助有困难的人
  10. 任正非:进军高端市场的同时,华为要防范未来竞争者从低端崛起