算法学习笔记(LeetCode OJ)
==================================
LeetCode的一些算法题,都是自己做的,欢迎提出改进~~
LeetCode:http://oj.leetcode.com
==================================
<Reverse Words in a String>-20140328
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.
public class Solution {
public String reverseWords(String s) {
s = s.trim();
String[] str = s.split("\\s{1,}");
String tmp;
int len = str.length;
int halfLen = len/2;
for(int i=0;i<halfLen;i++){
tmp = str[i];
str[i] = str[len-i-1];
str[len-i-1] = tmp;
}
StringBuffer sb = new StringBuffer();
String result;
if(len==1){
result = str[0];
}else{
for(String string:str){
sb.append(string+" ");
}
result = sb.toString().trim();
}
return result;
}
}
Java Code - 404ms - AC1/7
C++: http://blog.csdn.net/feliciafay/article/details/20884583
一个空格的情况
多个空格在一起的情况
空串的情况
首尾空格的情况
一些测试数据:
“ ”
“a ”
" a"
" a "
" a b "
" a b "
" a b"
"a b "
Hint
<Evaluate Reverse Polish Notation>-20140329
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:
["", "", "+", "", "*"] -> (( + ) * ) ->
["", "", "", "/", "+"] -> ( + ( / )) ->
public class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack();
for(String str:tokens){
if("+-*/".contains(str)){
int b = stack.pop();
int a = stack.pop();
switch(str){
case "+":
stack.add(a+b);
break;
case "-":
stack.add(a-b);
break;
case "*":
stack.add(a*b);
break;
case "/":
stack.add(a/b);
break;
}
}else{
stack.add(Integer.parseInt(str));
}
}
return stack.pop();
}
}
Java Code - 484ms - AC1/1
<Max Points on a Line>-20140330
Given n points on a 2D plane, find the maximum number of points that lie on the same straight 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 {
private boolean judgment(Point a, Point b, Point third){
return (a.y-third.y)*(b.x-third.x)==(b.y-third.y)*(a.x-third.x);
} public int maxPoints(Point[] points) {
int len = points.length;
if(len<3){
return len;
}
int[] mark = new int[len];
for(int m=0;m<len;m++){
mark[m] = 0;
}
int count = 2;
for(int i=0;i<len;i++){
int tmpCount = 2;
for(int j=i+1;j<len;j++){
if(mark[j]==1){
continue;
}
for(int k=j+1;k<len;k++){
if(mark[k]==1){
continue;
}
boolean judge = judgment(points[i],points[j],points[k]);
if(judge){
mark[k] = 1;
tmpCount++;
}
}
}
if(tmpCount>count){
count = tmpCount;
}
}
return count;
}
}
Java Code - Wrong
后来发现被坑了,它是有相同点出现这种情况的!!!开始我还傻傻地想为什么<5,6,18>三点共线而<0,5,6>却不共线,后来自己费了好大力气才明白过来。
然后,还有其他的问题,对此题无力了,还是先放放过几天头脑清醒再来弄吧T-T。
看看人家的思想先吧,不过我还是想先自己搞掂再看。
C++: http://blog.csdn.net/feliciafay/article/details/20704629
<Single Number>-20140414
Given an array of integers, every element appears twice except for one. Find that single one. Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
public class Solution {
public int singleNumber(int[] A) {
int num = 0;
for(int i : A){
num ^= i;
}
return num;
}
}
Java Code
算法学习笔记(LeetCode OJ)的更多相关文章
- Manacher算法学习笔记 | LeetCode#5
Manacher算法学习笔记 DECLARATION 引用来源:https://www.cnblogs.com/grandyang/p/4475985.html CONTENT 用途:寻找一个字符串的 ...
- C / C++算法学习笔记(8)-SHELL排序
原始地址:C / C++算法学习笔记(8)-SHELL排序 基本思想 先取一个小于n的整数d1作为第一个增量(gap),把文件的全部记录分成d1个组.所有距离为dl的倍数的记录放在同一个组中.先在各组 ...
- Johnson算法学习笔记
\(Johnson\)算法学习笔记. 在最短路的学习中,我们曾学习了三种最短路的算法,\(Bellman-Ford\)算法及其队列优化\(SPFA\)算法,\(Dijkstra\)算法.这些算法可以快 ...
- 某科学的PID算法学习笔记
最近,在某社团的要求下,自学了PID算法.学完后,深切地感受到PID算法之强大.PID算法应用广泛,比如加热器.平衡车.无人机等等,是自动控制理论中比较容易理解但十分重要的算法. 下面是博主学习过程中 ...
- Johnson 全源最短路径算法学习笔记
Johnson 全源最短路径算法学习笔记 如果你希望得到带互动的极简文字体验,请点这里 我们来学习johnson Johnson 算法是一种在边加权有向图中找到所有顶点对之间最短路径的方法.它允许一些 ...
- 算法学习笔记——sort 和 qsort 提供的快速排序
这里存放的是笔者在学习算法和数据结构时相关的学习笔记,记录了笔者通过网络和书籍资料中学习到的知识点和技巧,在供自己学习和反思的同时为有需要的人提供一定的思路和帮助. 从排序开始 基本的排序算法包括冒泡 ...
- R语言实现关联规则与推荐算法(学习笔记)
R语言实现关联规则 笔者前言:以前在网上遇到很多很好的关联规则的案例,最近看到一个更好的,于是便学习一下,写个学习笔记. 1 1 0 0 2 1 1 0 0 3 1 1 0 1 4 0 0 0 0 5 ...
- 二次剩余Cipolla算法学习笔记
对于同余式 \[x^2 \equiv n \pmod p\] 若对于给定的\(n, P\),存在\(x\)满足上面的式子,则乘\(n\)在模\(p\)意义下是二次剩余,否则为非二次剩余 我们需要计算的 ...
- 【算法学习笔记】Meissel-Lehmer 算法 (亚线性时间找出素数个数)
「Meissel-Lehmer 算法」是一种能在亚线性时间复杂度内求出 \(1\sim n\) 内质数个数的一种算法. 在看素数相关论文时发现了这个算法,论文链接:Here. 算法的细节来自 OI w ...
随机推荐
- 【转】 LESS CSS 框架简介
简介 CSS(层叠样式表)是一门历史悠久的标记性语言,同 HTML 一道,被广泛应用于万维网(World Wide Web)中.HTML 主要负责文档结构的定义,CSS 负责文档表现形式或样式的定义. ...
- Javaweb整合mongo和kettle6.0的环境配置
为了编译能通过,maven需要加入仓库地址以及一些必须要的包的依赖情况: pentaho中央仓库: 在properties里面配置版本号: <kettle.version>6.0.0.0- ...
- poj 2001 Shortest Prefixes
字典树的简单应用. #include<stdio.h> #include<string.h> ][]; struct node{ int cnt; node *next[]; ...
- CCF计算机认证——字符串匹配问题(运行都正确,为什么提交后只给50分?)
我的程序: #include<iostream> #include<cctype> #include<string> #include<vector> ...
- Sublime Text3 插件安装教程
链接地址:http://jingyan.baidu.com/article/4d58d541caeeaa9dd4e9c093.html
- php配置redis支持
在php.ini里面添加下面两行,注意这两行的顺序一定不要颠倒(扩展库下载网址https://github.com/phpredis/phpredis/downloads),同时注意这2个文件的版本一 ...
- 基于Web的系统测试方法
基于Web的系统测试与传统的软件测试既有相同之处,也有不同的地方,对软件测试提出了新的挑战.基于Web的系统测试不但需要检查和验证是否按照设计的要求运行,而且还要评价系统在不同用户的浏览器端的显示是否 ...
- MySQLdb callproc 方法
MySQLdb执行存储过程时就要调用 callproc 方法.它返回的是调用时的参数列表. MySQL 中存储过程的定如下: delimiter // create procedure proc_in ...
- logrotate 清理tomcat日志
rsyslog tomcat 服务器: 192.168.32.215 input(type="imfile" File="/usr/local/apache-tomcat ...
- maintenance ShellScripts
1.Linux挂载Winodws共享文件夹 2.查看http的并发请求数及其TCP连接状态: 3.用tcpdump嗅探80端口的访问看看谁最高 4.统计/var/log/下文件个数 5.查看当前系统每 ...