算法学习笔记(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 ...
随机推荐
- spring加载jar包中多个配置文件
转自:http://www.cnblogs.com/GarfieldTom/p/3723915.html <import resource="classpath*:applicatio ...
- eclipse序列化生成serialVersionUID
serialVersionUID作用: 序列化时为了保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性. 如果你修改代码重新部署后出现序列化错误,可以考虑给相应的类增加serialVersio ...
- PHP fopen和fwrite函数实现创建html页面
思路 用fopen函数和fread函数得到模板,然后用str_replace函数替换模板标签为变量,最后用fwrite函数输出新的HTML页面 index.html模板页面 <!DOCTYPE ...
- 15.java.lang.InstantiationException
java.lang.InstantiationException 实例化异常 当试图通过Class的newInstance()方法创建某个类的实例,但程序无法通过该构造器来创建该对象时引发 Class ...
- JS中特殊句子-for in
for(var i=0;i<len;i++)这样的用法一般都可以用for in 来替代. 例如: var a = ["a","b","c&quo ...
- J2SE知识点摘记(十三)
1. 字节流 InputStream(输入字节流)是一个定义了java流式字节流输入模式的抽象类.该类的所有方法在出错时都会引发一个IOExcepiton异常. Void close() ...
- Apache开启expires响应头,优化缓存
apache开始expires响应头输出 expires是什么 指示资源什么时候过期的时间值(GMT时间),在指定的过期时间前,浏览器可以直接使用自身缓存的版本,而不用向服务器发请求,大大减轻服务器压 ...
- startup_LPC17XX.s 启动文件分析
工程中startup_LPC17XX.s是M3的启动文件,启动文件由汇编语言写的,它的作用一般是下面这几个: 1)堆和栈的初始化 2)中断向量表定义 3)地址重映射及中断向量表的转移 4)设置系统时钟 ...
- TraceView简单使用方法
一.TraceView简介 TraceView是AndroidSDK里面自带的工具,用于对Android的应用程序以及Framework层的代码进行性能分析. TraceView是图形化的工具,最终它 ...
- spring mvc 使用jsr-303进行表单验证的方法介绍
源代码来源:http://howtodoinjava.com/spring/spring-mvc/spring-bean-validation-example-with-jsr-303-annotat ...