Java
文章平均质量分 74
feliciafay
开发工程师
展开
-
Java菜鸟入门(21) Merge K sorted List
假设输入是K个iterator, 如何来merge这K个iterator of Integer,使得输出是 List?1. 类似Merge K sorted List普通题的思路,使用PriorityQueue(or, Heap2. 使用一个wrapper来把current Integer value和它的相关的Iterator给捆绑打包。这样做的原因是,每次使用了iterator.nex原创 2015-12-14 18:00:35 · 1328 阅读 · 0 评论 -
JAVA菜鸟入门(13) outer class与inner class的之间能互相使用field或者method吗?
Nested classes are divided into two categories: static and non-static. 1. Nested classes that are declared static are called static nested classes. 2. Non-static nested classes are called inner cl原创 2015-05-01 04:44:46 · 2764 阅读 · 0 评论 -
JAVA菜鸟入门(16) callback函数
Java中的callback函数, template:interface CallBack { void methodToCallBack();}class CallBackImpl implements CallBack { public void methodToCallBack() { System.out.println("I've been ca原创 2015-05-21 01:24:56 · 1088 阅读 · 0 评论 -
JAVA菜鸟入门(12) reference variable是气球的线 +JVM中怎么实现
1 如果variable是primitive,那就拷贝原来变量的值到新变量。2 如果variable是object referece, 那就拷贝原来reference的值到新的变量,所以就有2个reference varibal指向了相同的object.eg. // Before the method callObject x = null;// Start of method c原创 2015-04-28 23:42:42 · 4683 阅读 · 0 评论 -
JAVA菜鸟入门(14) Anonymous Class 和 final variable
Anonymous Class在使用外面的local variable的时候,要declare为final,否则会有compiler error.import javax.swing.JButton;import javax.swing.JTextField;public class HelloWorldAnonymousClasses { public HelloWorldAno原创 2015-05-09 06:51:07 · 1107 阅读 · 0 评论 -
JAVA菜鸟入门(9) Java打印一维数组,二维数组
一 打印list中的元素0 JAVA风格的最简做法import java.util.List;...List l = new ArrayList(3);System.out.println(l);1 JAVA风格的做法import java.util.Arrays;import java.util.List;import java.util.ArrayList;...原创 2015-04-26 00:39:24 · 15970 阅读 · 1 评论 -
JAVA菜鸟入门(11) 基本类型
一. 基本类型Java 基础数据类型byte (number, 1 byte)short (number, 2 bytes)int (number, 4 bytes)long (number, 8 bytes)float (float number, 4 bytes)double (float number, 8 bytes)char (a characte原创 2015-04-26 01:11:03 · 1141 阅读 · 0 评论 -
JAVA菜鸟入门(10) 类初始化, 字符串比较equals() v.s. ==, Casting, ArrayList注意
一. 类的初始化第一种更推荐, 可以简化或者省略构造函数,代码简洁第二种 public class WeatherForecast{private String skies = "";private int high = 0;private int low = 0;public void setSkies(原创 2015-04-26 00:50:09 · 853 阅读 · 0 评论 -
JAVA菜鸟入门(8) Java的Final关键字
java final 和C++的const功能类似,用来表达常量,还能用在class或者method上,提供不同的用法。1. Java Final Variable Once a final variable has been assigned, it always contains the same value.但是 static final和private final是原创 2015-04-26 00:32:37 · 1245 阅读 · 0 评论 -
JAVA菜鸟入门(6) java中的reference 和 C++中的pointer的区别
首先说明,这里比较的是java中的reference和c++中的pointer,不是java中的reference和c++中的reference,因为这后二者差别非常明显。Referencesmight be implemented by storing the address. Usually Java references will be implemented原创 2015-04-09 13:34:39 · 3340 阅读 · 0 评论 -
LeetCode(119) Pascal's Triangle II (Java)
题目如下:Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use only O(k) extra space?我的代码:原创 2015-04-20 10:01:28 · 2601 阅读 · 0 评论 -
LeetCode(080) Remove Duplicates from Sorted Array II (Java)
题目如下:Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array A = [1,1,1,2,2,3],Your function should return length = 5, and A is now原创 2015-04-13 13:18:27 · 548 阅读 · 0 评论 -
LeetCode(045) Jump Game II (Java)
题目如下:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your原创 2015-04-13 15:00:17 · 1330 阅读 · 0 评论 -
SumOfNestedList
get sum of NestedList是一道非常有名非常高频的的电面题/** * Given a nested list of integers, returns the sum of all integers in the list weighted by their depth * For example, given the list {{1,1},2,{1,1}} the原创 2015-05-18 15:30:54 · 1017 阅读 · 0 评论 -
JAVA菜鸟入门(15) static method/ field与normal method/field的相互调用关系
1. normal method uses static field => finepublic class StaticFieldDemo { public static int staticVar; public int instanceVar; /* * test1 normal method uses static field */ public void No原创 2015-05-18 14:44:43 · 1705 阅读 · 0 评论 -
Java菜鸟入门(20) Producer Consumer经典代码
来自oracle官网 https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/Condition.html class BoundedBuffer { final Lock lock = new ReentrantLock(); final Condition notFull = lock.newC原创 2015-12-13 07:32:23 · 1655 阅读 · 0 评论 -
LeetCode(269) Alien Dictionary (Java)
题目如下:There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of words from the dictionary, where words are sorted lexicog原创 2015-11-25 18:58:18 · 4466 阅读 · 0 评论 -
LeeCode(149) Max Points on a Line
题目如下:Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.分析如下:思路比较直接,对其中某一个点i, 找到点i和剩下的所有n - 1个点构成的斜率。如果在这n - 1个斜率中,能够找到这样一个值,这个出现次数最多,那么就可以认为,原创 2015-12-11 12:19:51 · 891 阅读 · 0 评论 -
JAVA菜鸟入门 (19) inner calss: static v.s. non-static在main中被实例化时的区别
1. 首先, class中的class粗略地分成2类, static v.s non-static."Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are simply called static nested clas原创 2015-10-29 16:53:26 · 1165 阅读 · 0 评论 -
LintCode Number of Airplanes in the Sky(Java)
题目如下:Given an interval list which are flying and landing time of the flight. How many airplanes are on the sky at most?Have you met this question in a real interview? YesExampleFor interval li原创 2015-10-12 02:07:43 · 2334 阅读 · 0 评论 -
LeetCode(30) Substring with Concatenation of All Words (java)
先占位to be continued原创 2015-03-28 02:19:36 · 1899 阅读 · 0 评论 -
Trie相关的一些题目[draft]
关于trie的定义和图形请先看这里的简短描述。下面列出它相关的一些基本操作。1. build a trie这是基本操作,建立一个trie,add a word, search for that word, 正好有道题目在Leetcode上.class TrieNode { // Initialize your data structure here. char elem原创 2015-09-27 15:15:38 · 1042 阅读 · 0 评论 -
How to first sort by one attribute, if equal, then sort by another attribute.
这个小代码来demo一下,如何来按照两个字段进行sort。 场景1有一个list of classes,每个class中的attribute 1是String name, attribute 2是 Int age。现在假设需求是先按照name 字母顺序升序排序,加入两个人同名同姓,再按照age升序排序。 在这样的场景下,就需要使用comparator去对2个字段进行排序。场原创 2015-09-28 13:12:21 · 639 阅读 · 0 评论 -
LeetCode 分类整理(01) DFS 草稿
subset// solution 2 version B// a slightly different version from version A.// make a slight optimization while doing right shift.public class Solution { public List> subsets原创 2015-08-31 15:46:44 · 1020 阅读 · 0 评论 -
LeetCode(078) Subsets (Java)
题目如下:Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For exampl原创 2015-07-02 13:54:17 · 1039 阅读 · 0 评论 -
JAVA菜鸟入门(18) Set的Iterator
1. 使用iterator遍历Set中元素public class SetIteratorDemo { public static void main(String[] args) { HashSet objectSet = new HashSet(); objectSet.add("white"); objectSet.add("white"); objectSet.add原创 2015-06-23 15:03:43 · 3573 阅读 · 0 评论 -
JAVA菜鸟入门(17) 有排序的TreeSet和无排序的HashSet, LinkedHashSet
General-Purpose Set ImplementationsThere are three general-purpose Set implementations — HashSet, TreeSet, and LinkedHashSet. Which of these three to use is generally straightforward.HashSet i原创 2015-06-22 01:51:42 · 783 阅读 · 0 评论 -
LeetCode(055) Jump Game (Java)
题目如下:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Dete原创 2015-04-13 13:43:48 · 580 阅读 · 0 评论 -
LeetCode(074) Search a 2D Matrix (Java)
题目如下:Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of原创 2015-04-13 12:37:58 · 1179 阅读 · 0 评论 -
LeetCode(034) Search For a Range (Java)
题目如下:Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the target is not原创 2015-04-13 10:44:31 · 1065 阅读 · 0 评论 -
LeetCode(189) Rotate Array(Java)
题目如下:Rotate Array Total Accepted: 19161 Total Submissions: 108570 My Submissions Question Solution Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the原创 2015-04-12 00:15:30 · 2773 阅读 · 0 评论 -
JAVA菜鸟入门(7) default parameter , float/double vs BigDecimal
1 java不支持类似C++那样,为函数设定默认参数,所以需要做的事情是,自己用函数重载的方式进行模拟。如下public class FFOverload { public String getName(String givenName,String familyName){ return givenName+"."+familyName; } public String getNa原创 2015-04-11 01:11:58 · 1130 阅读 · 0 评论 -
[draft]字符串匹配 Z_algorithm
Z algorithm 和字符串匹配的几个算法其实类似,主要利用了pattern的重复信息。具体思想在这里,这个slides写得非常好懂。还有个demo也很好。我实现了一下code(暂时还没去测试是否bugfree)public class ZAlgo { private int [] z; private char [] text; public ZAlgo() {原创 2015-03-17 07:54:51 · 1454 阅读 · 0 评论 -
JAVA菜鸟入门(5) Hashmap vs Hashtable
1. Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.2. Hashtabl原创 2015-03-05 08:46:51 · 759 阅读 · 0 评论 -
JAVA菜鸟入门(4) Int vs Integer
首先,Int 是个primitive type。 Integer 是个object, 是个class,是int的wrapper class。然后, 什么时候用int什么时候用Integer呢?其中,use it as a hashtable key的原因是, Immutable objects are much better suited to be Hashtable k原创 2015-03-05 08:43:24 · 1127 阅读 · 0 评论 -
JAVA菜鸟入门(1) ReadFile
最近要开始积极地从C++切换到JAVA上了,大家都说基本可以平滑切换。目前试了试,主要的问题还是语法表达还不够习惯的问题。所以用这个系列来做一些记录吧。这个代码读入一组数据,然后将这组数据转化为2进制表达的字符串,输出出来。eg. 读入input. txt。其中input.txt包含一行, 1,3,6,8。那么输出为1->10103->1016->118->1001原创 2015-02-23 13:27:44 · 5462 阅读 · 0 评论 -
CC150 Arrays and Strings 1.7 ~ 1.8 Set Matrix Zero, IsS1RotationOfS2
1. 7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0.分析见这里解答如下:public class SetMatrixZero { public void SetMatrixToZero (int [][] ma原创 2015-02-27 10:17:48 · 1146 阅读 · 0 评论 -
CC150 Arrays and Strings 1.4 ~ 1.6 Replace Spaces, String Compression, Rotate Image
1.4题目如下:Write a method to replace all spaces in a string with ‘%20’.解答如下:/** * @author feliciafay * @note replace space with Percentage Symbol * @tips 1. manipulate a string from th原创 2015-02-24 16:15:27 · 996 阅读 · 0 评论 -
JAVA菜鸟入门(2) StringBuilder vs StringBuffer,
1. StringBuilder vs StringBuffer.from stackoverflow"StringBuffer is synchronized StringBuffer is thread-safeStringBuffer is slow (try to write a sample program and原创 2015-02-25 04:36:52 · 804 阅读 · 0 评论 -
CC150 Arrays and Strings 1.1 ~ 1.3 Unique Characters, Reverse String, IsPermutation
1.1 题目如下:Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?解答如下:public class stringUniqueChar { // ask your inter原创 2015-02-23 15:15:13 · 1172 阅读 · 1 评论