算法练习之leetcode系列1-3
public class Solution {
public String reverseWords(String s) {
String result[] = s.trim().split(" +");
String reverseResult[] = new String[result.length];
String reverse = "";
if(result.length>0){
for(int i = 0;i<result.length;i++){
reverseResult[i] = result[result.length-1-i].trim();
reverse += reverseResult[i].trim()+" ";
}
}
return reverse.trim();
}
}
2.Evaluate Reverse Polish Notation
public class Solution {
public int evalRPN(String[] tokens) {
List<String> numList = new ArrayList<String>();
for(int i = 0;i<tokens.length;i++){
if(tokens[i].equals("+")){
String newNum = String.valueOf(Integer.valueOf(numList.get(numList.size()-2))+Integer.valueOf(numList.get(numList.size()-1)));
numList.remove(numList.size()-2);
numList.remove(numList.size()-1);
numList.add(newNum);
}else if(tokens[i].equals("-")){
String newNum = String.valueOf(Integer.valueOf(numList.get(numList.size()-2))-Integer.valueOf(numList.get(numList.size()-1)));
numList.remove(numList.size()-2);
numList.remove(numList.size()-1);
numList.add(newNum);
}else if(tokens[i].equals("*")){
String newNum = String.valueOf(Integer.valueOf(numList.get(numList.size()-2))*Integer.valueOf(numList.get(numList.size()-1)));
numList.remove(numList.size()-2);
numList.remove(numList.size()-1);
numList.add(newNum);
}else if(tokens[i].equals("/")){
String newNum = String.valueOf(Integer.valueOf(numList.get(numList.size()-2))/Integer.valueOf(numList.get(numList.size()-1)));
numList.remove(numList.size()-2);
numList.remove(numList.size()-1);
numList.add(newNum);
}else{
numList.add(tokens[i]);
}
}
return Integer.valueOf(numList.get(0));
}
}
3.Max Points on a Line(这个题真是很蛋疼啊,开始时真没想到还会包括出现重复点的情况,导致后来写的有点晕了=.=)
/**
* Definition for a point.
* class Point {
* int x;
* int y;
* Point() { x = 0; y = 0; }
* Point(int a, int b) { x = a; y = b; }
* }
*/
public class Solution {
public int maxPoints(Point[] points) { List<Integer> numlist = new ArrayList<Integer>();
HashMap<String, Integer> map;
HashMap<String, Integer> mapPoint;
Integer max = 0;
if (points.length == 1) {
max = 1;
}
for (int i = 0; i < points.length; i++) {
map = new HashMap<String, Integer>();
mapPoint = new HashMap<String, Integer>();
for (int j = i + 1; j < points.length; j++) {
String kString = "";
if ((points[i].x - points[j].x) != 0) {
if (points[i].y - points[j].y == 0) {
kString = String.valueOf(0);
} else {
float k = (float) (points[i].y - points[j].y)
/ (float) (points[i].x - points[j].x);
kString = String.valueOf(k);
}
if (map.containsKey(kString)) {
int num = map.get(kString) + 1;
map.remove(kString);
map.put(kString, num);
} else {
map.put(kString, 2);
}
} else if ((points[i].y - points[j].y) != 0
&& (points[i].x - points[j].x) == 0) {
kString = "no";
if (map.containsKey(kString)) {
int num = map.get(kString) + 1;
map.remove(kString);
map.put(kString, num);
} else {
map.put(kString, 2);
}
} else {
kString = "re";
if (mapPoint.containsKey(kString)) {
int num = mapPoint.get(kString) + 1;
mapPoint.remove(kString);
mapPoint.put(kString, num);
} else {
mapPoint.put(kString, 2);
}
} }
if (!map.isEmpty()) {
for (String key : map.keySet()) {
if (mapPoint.containsKey("re")
&& map.get(key) + mapPoint.get("re")
- 1 > max){
max = map.get(key) + mapPoint.get("re")
- 1;
} else if (map.get(key) > max) {
max = map.get(key);
} }
}
else {
if (mapPoint.containsKey("re")
&& mapPoint.get("re") > max) {
max = mapPoint.get("re");
}
}
} return max; }
}
算法练习之leetcode系列1-3的更多相关文章
- Leetcode系列之两数之和
Leetcode系列之两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一个答案.但是,你 ...
- 【算法题 14 LeetCode 147 链表的插入排序】
算法题 14 LeetCode 147 链表的插入排序: 解题代码: # Definition for singly-linked list. # class ListNode(object): # ...
- leetcode 系列文章目录
leetcode 系列文章目录 0. 两数之和1. 两数相加 2. 无重复字符的最长子串 3. 寻找两个有序数组的中位数 4. 最长回文子串 5. Z 字形变换 6. 整数反转 7. 字符串转换整数 ...
- C#刷遍Leetcode系列连载 索引
C#刷遍Leetcode系列文章 索引 索引(陆续发布中,请保持关注) C#刷遍Leetcode面试题系列连载(1) - 入门与工具简介 C#刷遍Leetcode面试题系列连载(2): No.38 - ...
- Leetcode系列-Search in Rotated Sorted Array
做Leetcode题有一段时间了,但都是断断续续的,到现在才做了30题左右,感觉对自己来说还是有点难度的.希望自己能继续坚持下去,在校招前能解决超过一百题吧. 其实这些题就是用来训练你的解题思路的,做 ...
- Manacher算法学习笔记 | LeetCode#5
Manacher算法学习笔记 DECLARATION 引用来源:https://www.cnblogs.com/grandyang/p/4475985.html CONTENT 用途:寻找一个字符串的 ...
- 程序员进阶之算法练习:LeetCode专场
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由落影发表 前言 LeetCode上的题目是大公司面试常见的算法题,今天的目标是拿下5道算法题: 题目1是基于链表的大数加法,既考察基本 ...
- 最长回文子串 C++实现 java实现 leetcode系列(五)
给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &qu ...
- 【算法题目】Leetcode算法题思路:两数相加
在LeetCode上刷了一题比较基础的算法题,一开始也能解出来,不过在解题过程中用了比较多的if判断,看起来代码比较差,经过思考和改进把原来的算法优化了. 题目: 给出两个 非空 的链表用来表示两个非 ...
随机推荐
- Cheatsheet: 2016 08.01 ~ 08.31
.NET Getting Started with ASP.NET Core and VS Code Coding Standard Best Practices In C# Wire – Writi ...
- 【转载】推荐5款超实用的.NET性能分析工具
来源:http://www.csdn.net/article/2012-11-23/2812174-5-Good-and-useful-.NET-Profilers 虽然.NET框架号称永远不会发生内 ...
- 部署网站出现System.ServiceModel.Activation.HttpModule错误
1. 部署网站到IIS7.5,Window 2008的时候出现这个错误 2. 错误信息 Server Error in '/' Application. Could not load type 'Sy ...
- C#泛型文章汇总
http://www.cnblogs.com/kid-li/archive/2006/11/29/577045.html http://www.blogjava.net/Jack2007/archiv ...
- 去繁从简--简化Message和Signal设置
以往的版本中如果添加了消息或者信号类型的事件需要先在流程属性(设置流程ID的界面)中添加Message和Signal,之后再选择对应的事件在属性中的下拉框中选择已经定义的Message或Signal. ...
- 基础笔记12(socket,url网络通信)
进一步深入socket 1.网络通信条件: .IP地址,可用主机名. .传输数据时将不用的应用程序通过数字标识区分开来,这种标识称为逻辑端口,也称端口.(0-65535端口,一般系统预留0-1024) ...
- golang os/exec 执行外部命令
exec包执行外部命令,它将os.StartProcess进行包装使得它更容易映射到stdin和stdout,并且利用pipe连接i/o. func LookPath(file string) (st ...
- adb shell使用
adb shell可以用来操纵数据库 1.在cmd界面 输入adb shell 进入adb shell 2.使用cd 切换到工作目录:使用ls查看文件 3.使用sqlite3+数据库名字 打开某个数据 ...
- mysql:SQL语句操作数据库中表和字段的COMMENT值
转载:http://blog.163.com/inflexible_simple/blog/static/167694684201182601221758/ 参考文档不太给力啊,表注释和字段注释的资料 ...
- KEGG数据库
参考:KEGG数据库中文教程 - 博奥 &[学习笔记]KEGG数据库 - 微信 学习一个技能最主要的事情你必须知道,那就是能通过它来做什么? KEGG数据库里面有什么? 如何查询某一特定的代 ...