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

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. 查询最小未使用ID的SQL查询

    --每个都加一,以此来找出最小的未用ID SELECT Min(T1.ID)+1 FROM dbo.TestTable T1 -- 不用查询已经存在的ID WHERE (T1.ID+1) NOT IN ...

  2. C/C++ 结构体成员在内存中的对齐规则(转载)

    这几天在看王艳平的<windows 程序设计>,第5章讲解了MFC框架是怎么管理窗口句柄到窗口实例之间的映射,用到了两个类CPlex和CMapPtrToPtr,用于管理内存分配的类(避免因 ...

  3. Java提高学习之Object(2)

    Equality 问:euqals()函数是用来做什么的? 答:equals()函数可以用来检查一个对象与调用这个equals()的这个对象是否相等. 问:为什么不用“==”运算符来判断两个对象是否相 ...

  4. CodeForces 379 D. New Year Letter

    枚举开头结尾的字母,枚举ac的个数,总AC个数就是两个Fibonacci数列的和..... D. New Year Letter time limit per test 1 second memory ...

  5. HTML5 Placeholder实现input背景文字提示效果

    这篇文章我们来看看什么是input输入框背景文字提示效果,如下图所示: 这种效果现在网上非常的普遍流行,但大部分是使用JavaScript实现的.但HTML5给我们提供了新的纯HTML的实现方式,不需 ...

  6. vb6.0 时间日期

    使用year(now)可以得到4位数的年    你还可以用Format来得到, 还有FormatDateTime 下面两种都是一样的结果:  FormatDateTime(now,vbLongDate ...

  7. TCP/IP详解学习笔记 这位仁兄写得太好了

      TCP/IP详解学习笔记(1)-基本概念 为什么会有TCP/IP协议 在世界上各地,各种各样的电脑运行着各自不同的操作系统为大家服务,这些电脑在表达同一种信息的时候所使用的方法是千差万别.就好像圣 ...

  8. Python核心编程笔记---- print@2

    print 的输出从定向问题 print 可以用’>>‘来重定向输出,下面是例子 f = open('D:/python.txt','w+') print >> f," ...

  9. easyui datagrid 列的内容超出所定义的列宽时,自动换行

    定义表单  nowrap="false"可以使得列中的内容超出所定义的列宽是就会自动换行pagination : true, // 当true时在DataGrid底部显示一个分页工 ...

  10. Django里面的自定义tag和filter

    Django的文档里面有这么一句 The app that contains the custom tags must be in INSTALLED_APPS  in order for the { ...