==================================

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)的更多相关文章

  1. Manacher算法学习笔记 | LeetCode#5

    Manacher算法学习笔记 DECLARATION 引用来源:https://www.cnblogs.com/grandyang/p/4475985.html CONTENT 用途:寻找一个字符串的 ...

  2. C / C++算法学习笔记(8)-SHELL排序

    原始地址:C / C++算法学习笔记(8)-SHELL排序 基本思想 先取一个小于n的整数d1作为第一个增量(gap),把文件的全部记录分成d1个组.所有距离为dl的倍数的记录放在同一个组中.先在各组 ...

  3. Johnson算法学习笔记

    \(Johnson\)算法学习笔记. 在最短路的学习中,我们曾学习了三种最短路的算法,\(Bellman-Ford\)算法及其队列优化\(SPFA\)算法,\(Dijkstra\)算法.这些算法可以快 ...

  4. 某科学的PID算法学习笔记

    最近,在某社团的要求下,自学了PID算法.学完后,深切地感受到PID算法之强大.PID算法应用广泛,比如加热器.平衡车.无人机等等,是自动控制理论中比较容易理解但十分重要的算法. 下面是博主学习过程中 ...

  5. Johnson 全源最短路径算法学习笔记

    Johnson 全源最短路径算法学习笔记 如果你希望得到带互动的极简文字体验,请点这里 我们来学习johnson Johnson 算法是一种在边加权有向图中找到所有顶点对之间最短路径的方法.它允许一些 ...

  6. 算法学习笔记——sort 和 qsort 提供的快速排序

    这里存放的是笔者在学习算法和数据结构时相关的学习笔记,记录了笔者通过网络和书籍资料中学习到的知识点和技巧,在供自己学习和反思的同时为有需要的人提供一定的思路和帮助. 从排序开始 基本的排序算法包括冒泡 ...

  7. R语言实现关联规则与推荐算法(学习笔记)

    R语言实现关联规则 笔者前言:以前在网上遇到很多很好的关联规则的案例,最近看到一个更好的,于是便学习一下,写个学习笔记. 1 1 0 0 2 1 1 0 0 3 1 1 0 1 4 0 0 0 0 5 ...

  8. 二次剩余Cipolla算法学习笔记

    对于同余式 \[x^2 \equiv n \pmod p\] 若对于给定的\(n, P\),存在\(x\)满足上面的式子,则乘\(n\)在模\(p\)意义下是二次剩余,否则为非二次剩余 我们需要计算的 ...

  9. 【算法学习笔记】Meissel-Lehmer 算法 (亚线性时间找出素数个数)

    「Meissel-Lehmer 算法」是一种能在亚线性时间复杂度内求出 \(1\sim n\) 内质数个数的一种算法. 在看素数相关论文时发现了这个算法,论文链接:Here. 算法的细节来自 OI w ...

随机推荐

  1. 在eclipse中怎么安装插件

    1.方法1是help中安装新软件,这个一般要你真到软件的url,如果不知道呢?那么就要用到marketpalce,这个一般也在help中的,

  2. html系列教程--nav noscript option optgroup object

    <nav> 标签:html5用来定义导航部分的标签,与div无异,html5推荐用nav. <noframes> 标签:定义当浏览器不支持frameset时显示的提示性语言 d ...

  3. Virtualbox 启动虚拟机报错以及扩展、显卡驱动安装

    一.Virtualbox虚拟机启动报错,如图 预先估计是BIOS中的cpu Virtualtion虚拟化支持是disable,结果一看是enable. 接下来只好Google,找到了这么一个帖子:ht ...

  4. Oracle中使用透明网关链接到Sqlserver[Z]

    Oracle中使用透明网关链接到Sqlserver 在最近项目中需要从Oracle中访问SQL Server数据库, 自然想到了透明网关. 因为Oracle数据库是Linux上的, 而Linux上的O ...

  5. Mac浏览器全屏设置

    在 mac 升级之后,以往点击放大的按钮,现在显示的效果是全屏:

  6. DEV控件自定义排序实现

    一般的控件或者组件都支持按照某一列进行排序.但是,这种排序是根据数据源里的数据默认按照降序或升序排序的,同时这样的排序与字段的类型有关. 假设现在字段的类型是字符串类型 ,但是,存储的数据时数字加一些 ...

  7. js——cookie

    cookie:存储数据,当用户访问了某个网站(网页)的时候,我们就可以通过cookie来向访问者电脑上存储数据 1.不同的浏览器存放的cookie位置不一样,也是不能通用的 2. cookie的存储是 ...

  8. C/C++基本数据类型

    基本类型 C/C++语言有一组基本类型,对应于计算机的基本存储单元和使用这些单元去保存数据的一些常用方式: 基本数据类型如下: Name Description Size* Range* char C ...

  9. Oracle学习之start with...connect by子句的用法

    转自:http://www.blogjava.net/xzclog/archive/2010/03/05/314642.html,多谢博主分享 Oracle中start with…connect by ...

  10. eclipse 异常Unhandled event loop exception

    出了这一类的异常问题,大都是一些图像优化软件插件等等. 出现的问题大都是,一些eclipse模块不显示,或者点击不反应,出现最多的次数是点击断点的时候. 我这里是Catalyst Control Ce ...