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

LeetCode的一些算法题,都是自己做的,欢迎提出改进~~

LeetCode:http://oj.leetcode.com

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

<Reverse Words in a String>-20140328

  1. Given an input string, reverse the string word by word.
  2. For example,
  3. Given s = "the sky is blue",
  4. return "blue is sky the".
  5. click to show clarification.
  1. public class Solution {
  2. public String reverseWords(String s) {
  3. s = s.trim();
  4. String[] str = s.split("\\s{1,}");
  5. String tmp;
  6. int len = str.length;
  7. int halfLen = len/2;
  8. for(int i=0;i<halfLen;i++){
  9. tmp = str[i];
  10. str[i] = str[len-i-1];
  11. str[len-i-1] = tmp;
  12. }
  13. StringBuffer sb = new StringBuffer();
  14. String result;
  15. if(len==1){
  16. result = str[0];
  17. }else{
  18. for(String string:str){
  19. sb.append(string+" ");
  20. }
  21. result = sb.toString().trim();
  22. }
  23. return result;
  24. }
  25. }

Java Code - 404ms - AC1/7

C++: http://blog.csdn.net/feliciafay/article/details/20884583

  1. 一个空格的情况
  2. 多个空格在一起的情况
  3. 空串的情况
  4. 首尾空格的情况
  5. 一些测试数据:

  6. a
  7. " a"
  8. " a "
  9. " a b "
  10. " a b "
  11. " a b"
  12. "a b "

Hint

<Evaluate Reverse Polish Notation>-20140329

  1. Evaluate the value of an arithmetic expression in Reverse Polish Notation.(逆波兰表达式/后缀表达式)
  2. Valid operators are +, -, *, /. Each operand may be an integer or another expression.
  3. Some examples:
  4. ["", "", "+", "", "*"] -> (( + ) * ) ->
  5. ["", "", "", "/", "+"] -> ( + ( / )) ->
  1. public class Solution {
  2. public int evalRPN(String[] tokens) {
  3. Stack<Integer> stack = new Stack();
  4. for(String str:tokens){
  5. if("+-*/".contains(str)){
  6. int b = stack.pop();
  7. int a = stack.pop();
  8. switch(str){
  9. case "+":
  10. stack.add(a+b);
  11. break;
  12. case "-":
  13. stack.add(a-b);
  14. break;
  15. case "*":
  16. stack.add(a*b);
  17. break;
  18. case "/":
  19. stack.add(a/b);
  20. break;
  21. }
  22. }else{
  23. stack.add(Integer.parseInt(str));
  24. }
  25. }
  26. return stack.pop();
  27. }
  28. }

Java Code - 484ms - AC1/1

 <Max Points on a Line>-20140330

  1. Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

开始的实现:

  1. /**
  2. * Definition for a point.
  3. * class Point {
  4. * int x;
  5. * int y;
  6. * Point() { x = 0; y = 0; }
  7. * Point(int a, int b) { x = a; y = b; }
  8. * }
  9. */
  10. public class Solution {
  11. private boolean judgment(Point a, Point b, Point third){
  12. return (a.y-third.y)*(b.x-third.x)==(b.y-third.y)*(a.x-third.x);
  13. }
  14.  
  15. public int maxPoints(Point[] points) {
  16. int len = points.length;
  17. if(len<3){
  18. return len;
  19. }
  20. int[] mark = new int[len];
  21. for(int m=0;m<len;m++){
  22. mark[m] = 0;
  23. }
  24. int count = 2;
  25. for(int i=0;i<len;i++){
  26. int tmpCount = 2;
  27. for(int j=i+1;j<len;j++){
  28. if(mark[j]==1){
  29. continue;
  30. }
  31. for(int k=j+1;k<len;k++){
  32. if(mark[k]==1){
  33. continue;
  34. }
  35. boolean judge = judgment(points[i],points[j],points[k]);
  36. if(judge){
  37. mark[k] = 1;
  38. tmpCount++;
  39. }
  40. }
  41. }
  42. if(tmpCount>count){
  43. count = tmpCount;
  44. }
  45. }
  46. return count;
  47. }
  48. }

Java Code - Wrong

后来发现被坑了,它是有相同点出现这种情况的!!!开始我还傻傻地想为什么<5,6,18>三点共线而<0,5,6>却不共线,后来自己费了好大力气才明白过来。

然后,还有其他的问题,对此题无力了,还是先放放过几天头脑清醒再来弄吧T-T。

看看人家的思想先吧,不过我还是想先自己搞掂再看。

C++: http://blog.csdn.net/feliciafay/article/details/20704629

<Single Number>-20140414

  1. Given an array of integers, every element appears twice except for one. Find that single one.
  2.  
  3. Note:
  4. Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
  1. public class Solution {
  2. public int singleNumber(int[] A) {
  3. int num = 0;
  4. for(int i : A){
  5. num ^= i;
  6. }
  7. return num;
  8. }
  9. }

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. spring加载jar包中多个配置文件

    转自:http://www.cnblogs.com/GarfieldTom/p/3723915.html <import resource="classpath*:applicatio ...

  2. eclipse序列化生成serialVersionUID

    serialVersionUID作用: 序列化时为了保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性. 如果你修改代码重新部署后出现序列化错误,可以考虑给相应的类增加serialVersio ...

  3. PHP fopen和fwrite函数实现创建html页面

    思路 用fopen函数和fread函数得到模板,然后用str_replace函数替换模板标签为变量,最后用fwrite函数输出新的HTML页面 index.html模板页面 <!DOCTYPE ...

  4. 15.java.lang.InstantiationException

    java.lang.InstantiationException 实例化异常 当试图通过Class的newInstance()方法创建某个类的实例,但程序无法通过该构造器来创建该对象时引发 Class ...

  5. JS中特殊句子-for in

    for(var i=0;i<len;i++)这样的用法一般都可以用for in 来替代. 例如: var a = ["a","b","c&quo ...

  6. J2SE知识点摘记(十三)

    1.        字节流 InputStream(输入字节流)是一个定义了java流式字节流输入模式的抽象类.该类的所有方法在出错时都会引发一个IOExcepiton异常. Void close() ...

  7. Apache开启expires响应头,优化缓存

    apache开始expires响应头输出 expires是什么 指示资源什么时候过期的时间值(GMT时间),在指定的过期时间前,浏览器可以直接使用自身缓存的版本,而不用向服务器发请求,大大减轻服务器压 ...

  8. startup_LPC17XX.s 启动文件分析

    工程中startup_LPC17XX.s是M3的启动文件,启动文件由汇编语言写的,它的作用一般是下面这几个: 1)堆和栈的初始化 2)中断向量表定义 3)地址重映射及中断向量表的转移 4)设置系统时钟 ...

  9. TraceView简单使用方法

    一.TraceView简介 TraceView是AndroidSDK里面自带的工具,用于对Android的应用程序以及Framework层的代码进行性能分析. TraceView是图形化的工具,最终它 ...

  10. spring mvc 使用jsr-303进行表单验证的方法介绍

    源代码来源:http://howtodoinjava.com/spring/spring-mvc/spring-bean-validation-example-with-jsr-303-annotat ...