
java
Garvin Li
Dancing with data
展开
-
java的main函数为什么是public static void main(String[] args)
这个问题困扰我好久了,今天就一查究竟,毕竟我好奇心比较重1. why “public” 因为java程序是通过jvm虚拟机调用的,所以main()函数要是想被调用,必须是public2.why “static” 在java中,没有static的变量或函数,如果想被调用的话,是要先新建一个对象才可以。而main函数作为程序的入口,需要在其它函数实例化之前就启动,这也就是为什么要加一个stat原创 2015-01-09 15:36:49 · 10370 阅读 · 1 评论 -
单链表问题(反转、是否有环、删除结尾第N个节点、合并两个sortlist、找到交点)
1.时间复杂度O(N),内存O(1)的效率下实现单链表的翻转public static TreeNode revers(TreeNode head){ TreeNode temp,first,second; first=head; second=head.next; while(second!=null){ temp=second.next; second.next=原创 2015-03-26 15:21:00 · 2264 阅读 · 0 评论 -
【LeetCode从零单排】No 114 Flatten Binary Tree to Linked List
题目Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1 \ 2原创 2015-03-16 14:57:08 · 1432 阅读 · 0 评论 -
【LeetCode从零单排】No112 Path Sum
题目Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree and sum =原创 2015-03-04 16:48:47 · 1665 阅读 · 0 评论 -
【LeetCode从零单排】No104 Maximum Depth of Binary Tree
题目Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.代码/** * Definition for binary tree * p原创 2015-03-04 14:51:02 · 1652 阅读 · 0 评论 -
【LeetCode从零单排】No102 Binary Tree Level Order Traversal
题目 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 /原创 2015-03-03 16:22:03 · 1572 阅读 · 2 评论 -
【LeetCode从零单排】No100 Same Tree && No101 Symmetric Tree
题目1.same treeGiven two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.2.sym原创 2015-03-02 16:10:29 · 1515 阅读 · 0 评论 -
【LeetCode从零单排】No88.Merge Sorted Array
题目Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. T原创 2015-03-02 09:54:12 · 1400 阅读 · 0 评论 -
【LeetCode从零单排】No83 Remove Duplicates from Sorted List
题目Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.代码 public static ListNode deleteD原创 2015-03-02 11:18:50 · 1464 阅读 · 0 评论 -
【LeetCode从零单排】No58.Length of Last Word
题目Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is defined原创 2015-02-15 10:19:37 · 1479 阅读 · 0 评论 -
【LeetCode从零单排】No36 Valid Sudoku
题目 判断数独是否成立的一道题,看的是某大神的答案,写的太漂亮了。Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the charact原创 2015-02-13 16:50:03 · 1306 阅读 · 0 评论 -
【LeetCode从零单排】No15 3Sum
题目Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a triplet (a,b,c) must be原创 2015-03-20 16:39:36 · 1227 阅读 · 0 评论 -
【LeetCode从零单排】No118 Pascal's Triangle
题目Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]代码public class Solution { public原创 2015-03-09 15:10:31 · 1523 阅读 · 0 评论 -
【LeetCode从零单排】No221.Maximal Square
题目Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area.For example, given the following matrix:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0Retur原创 2015-07-02 09:58:17 · 1741 阅读 · 0 评论 -
【LeetCode从零单排】No133. clon graph (BFS广度优先搜索)
背景(以下背景资料转载自:http://www.cnblogs.com/springfor/p/3874591.html?utm_source=tuicool)DFS(Dpeth-first Search)顾名思义,就是深度搜索,一条路走到黑,再选新的路。记得上Algorithm的时候,教授举得例子就是说,DFS很像好奇的小孩,你给这个小孩几个盒子套盒子,好奇的小孩肯定会一个盒子打开后继续再在这个原创 2015-04-06 16:57:23 · 1783 阅读 · 0 评论 -
【LeetCode从零单排】No121 Best Time to Buy and Sell Stock
题目Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), d原创 2015-04-01 09:16:04 · 1567 阅读 · 0 评论 -
【LeetCode从零单排】No96 Unique Binary Search Trees
题目Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \原创 2015-03-24 17:09:02 · 1604 阅读 · 0 评论 -
【LeetCode从零单排】No129 Sum Root to Leaf Numbers
题目Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the total sum of原创 2015-03-23 15:16:54 · 1301 阅读 · 0 评论 -
【LeetCode从零单排】No 191.Number of 1 Bits(考察位运算)
题目Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11' has binary representation 0000000000原创 2015-03-13 15:50:15 · 4620 阅读 · 2 评论 -
【LeetCode从零单排】No.169 Majority Element(hashmap用法)
题目Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element alwa原创 2015-03-12 11:26:57 · 1504 阅读 · 0 评论 -
【LeetCode从零单排】No189 .Rotate Array
题目Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].Note:Try to come up as many solutions as you can, the原创 2015-03-12 16:12:35 · 1448 阅读 · 0 评论 -
【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters
题目Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. Fo原创 2015-03-17 16:33:47 · 1449 阅读 · 0 评论 -
【LeetCode从零单排】No.160 Intersection of Two Linked Lists
题目Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2 ↘原创 2015-03-10 14:28:15 · 1320 阅读 · 0 评论 -
【LeetCode从零单排】No38.CountAndSay
题目The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, then原创 2015-02-14 18:13:40 · 1415 阅读 · 0 评论 -
【LeetCode从零单排】No67.AddBinary
题目Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".代码public class Solution { public String addBinary(String a, String b) { S原创 2015-02-15 16:44:53 · 1322 阅读 · 0 评论 -
【LeetCode从零单排】No28 Implement strStr()
题目Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.代码public class Solution { public int strStr(String haystack, String ne原创 2015-02-12 16:36:11 · 1400 阅读 · 0 评论 -
【算法数据结构Java实现】Java实现单链表
1.背景 单链表是最基本的数据结构,仔细看了很久终于搞明白了,差不每个部分,每个链都是node的一个对象。需要两个参数定位:一个是index,表示对象的方位。另一个是node的对象。2.代码node类public class Node { protected Node next; protected int data; public Node(int data){原创 2014-12-02 17:02:19 · 1638 阅读 · 2 评论 -
【算法数据结构Java实现】折半查找
1.背景 以一个题目为例,一个整数x是一组按大小顺序排列好的数列中的一个数,我们要找到x在数列中的索引位置。比如按从小到大排列的数列:-3,-2,0,4,5,7,12,64我们要找到数字7的位置,如果是线性查找,时间复杂度是O(n),如果用折半查找的话,时间复杂度是O(log(n)),因为每次折半,计算量少一半,所以取对数。2.代码package Algorithm_analysis;原创 2014-11-24 22:07:21 · 2005 阅读 · 0 评论 -
【算法数据结构Java实现】欧几里得算法
1.背景 欧几里得算法是一个求最大因子的快速算法。如果m,n存在最大因子k,假设m=x*n+r,那么m和n可以整出k的话,r也肯定可以整除k 因为定理:如果M>N,则M mod N2.代码 package Algorithm_analysis;public class Euclid { public static void mai原创 2014-11-25 13:23:57 · 3628 阅读 · 0 评论 -
【算法数据结构Java实现】时间复杂度为O(n)的最大和序列
1.背景 最大序列和问题一直以来是一个比较经典的算法题,看到这个问题,有很多解题的办法。今天看到了一种时间复杂度只为O(n)的解题算法,在这里记录下。 思路很简单,比方说有P1,P2,P3,P4.....这样一个序列,我们从P1开始求和,比如说在P5时求和数小于零,就可以断定。第一种情况,最大序列在P1~P5之间,或者说在P6~Pn之间。因为如果P1原创 2014-11-21 20:11:30 · 3221 阅读 · 0 评论 -
【算法数据结构Java实现】递归的简单剖析及时间复杂度计算
1.理解 对于递归函数的理解,我觉得是比较重要的,因为很多大神能把递归函数用的惟妙惟肖,不光是他们的编程功力高深,更主要是能理解这个算法。比较直白的理解是,如果一个事件的逻辑可以表示成,f(x)=nf(x-1)+o(x)形式,那么就可以用递归的思路来实现。编写递归逻辑的时候要知道如下法则:1.要有基准 比如说,f(x)=f(x-1)+1,如果不加入基准,f(0)的值是多少,原创 2014-11-21 15:32:16 · 2416 阅读 · 0 评论 -
JAVA enum实现简单状态机功能
(转载请注明出处:http://blog.youkuaiyun.com/buptgshengod)1.背景 我们做android应用,往往要进行多个状态的切换,就像是照相机功能的侦测,预置,拍照等状态。有的时候通过if else也能完成功能,但是却显得代码很乱,这时候用enum枚举方法产生状态机机制,就很清晰的实现功能。2.代码简单的三种状态切换public cl原创 2014-03-12 15:35:22 · 5481 阅读 · 0 评论 -
Java多线程之线程间协作 notify与wait的使用
(转载请注明出处:http://blog.youkuaiyun.com/buptgshengod)1.背景 Java多线程操作运用很广,特别是在android程序方面。线程异步协作是多线程操作的难点也是关键,也是找工作面试经常考到的地方。下面分享一下我的使用心得。介绍几个关键字:synchronized:线程锁,使得系统只执行当前线程。notifyAll():唤醒其它被锁住原创 2014-02-13 09:01:55 · 2985 阅读 · 5 评论 -
关于内存泄漏
本文由 ImportNew - 范琦琦 翻译自 Programcreek。如需转载本文,请先参见文章末尾处的转载要求。ImportNew注:如果你也对Java技术翻译分享感兴趣,欢迎加入我们的 Java开发 小组。参与方式请查看小组简介。Java最显著的优势之一就是它的内存管理机制。你只需简单创建对象,然后Java垃圾回收机制便会小心的分配和释放内存。然而,事实并非如此简单,因转载 2014-01-17 11:35:16 · 1949 阅读 · 0 评论 -
eclipse android环境配置
以后工作中要用到android开发,所以想搭建好开发环境,笔记本装的是win7,在网上找了找相关资料,发现博客园有一片介绍搭建Android开发环境的文章,所以转载过来方便以后查看。转载地址:http://www.cnblogs.com/vengen/archive/2010/04/01/AndroidSetup.html我把该教程做成了一个PDF,网速慢的朋友可以在这里下载 http:/转载 2013-08-15 09:18:05 · 1762 阅读 · 0 评论 -
Android开发把项目打包成apk
做完一个Android项目之后,如何才能把项目发布到Internet上供别人使用呢?我们需要将自己的程序打包成Android安装包文件--APK(Android Package),其后缀名为".apk"。将APK文件直接上传到Android模拟器或Android手机中执行即可进行安装。Android系统要求具有其开发者签名的私人密钥的应用程序才能够被安装。生成数字签名以及打包项目成APK都可以采用转载 2013-08-22 20:39:41 · 1902 阅读 · 0 评论 -
Java的clone()用法实例解析
1.背景 用java写程序的时候很苦恼的一件事就是,如果将一个对象a赋给另一个对象b,那么你改变a的变量值得时候,b的值也对应的变化。如果我们只想单纯的获取那个时刻的a的状况给b的话,就要用到clone方法了。比如说如下代码:public class Main { public static void main(String[] args) { // TODO Auto-原创 2015-02-03 16:48:50 · 6663 阅读 · 0 评论 -
【算法数据结构Java实现】Java实现动态规划(背包问题)
1.背景 追随着buptwusuopu大神的脚步,最近在研习动态规划。动态规划应该叫一种解决问题的思想,记得又一次去某公司面试就被问到了这个。 多于动态规划的理解,大致是这样的,从空集合开始,每增加一个元素就求它的最优解,直到所有元素加进来,就得到了总的最优解。 比较典型的应用就是背包问题,有一个重量一定的包,有若干件物品,他们各自有不同的重量和价值,怎样背原创 2015-02-05 16:25:44 · 6653 阅读 · 1 评论 -
【LeetCode从零单排】No27.Remove Element
题目 Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new length.代码p原创 2015-02-12 16:34:25 · 1254 阅读 · 0 评论 -
【LeetCode从零单排】No21.MergeTwoSortedLists
题目 这道题是链表的简单应用,将两个有序链表合成一个有序链表。 思路是:表一,表二各取两个对象,分别指向current和next,进行交叉比较排序。Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nod原创 2015-02-11 11:18:30 · 1415 阅读 · 0 评论