
Java
文章平均质量分 57
蓝色街灯_BlueLight
编程是一种美德,是促使一个人不断向上发展的一种原动力。
展开
-
LeetCode : Multiply Strings (java)
Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative.思路:通过一位乘法和字符串加法实现,注意进位问题,注意字符串方向问题。原创 2016-04-08 01:22:21 · 593 阅读 · 0 评论 -
LeetCode : Trapping Rain Water (java)
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1,3,2,1,2,1]原创 2016-04-06 00:38:31 · 403 阅读 · 0 评论 -
LeetCode : First Missing Positive [java]
Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses constant原创 2016-03-29 00:09:12 · 321 阅读 · 0 评论 -
LeetCode : Combination Sum II [java]
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the combina原创 2016-03-21 10:57:56 · 334 阅读 · 0 评论 -
多线程 : 面试题[总结]
1. 使用BlockingQueue 实现 生产者 消费者2. 使用 Semaphore 实现 多线程 依次执行3. 不是同一个对象,synchronized()不能互斥4. "1"+"" 编译器优化为 "1" 5. 不定期更新,记录原创 2016-03-17 11:45:37 · 256 阅读 · 0 评论 -
多线程 : 并发库中并发集合
ConcurrentHashMapCopyOnWriteArrayListCopyOnWriteArraySet用法跟普通Map 、 List 、Set 一样,但是这些支持边迭代边修改等并发操作。原创 2016-03-17 10:49:39 · 329 阅读 · 0 评论 -
多线程 : 阻塞队列
BlockingQueue最终会有四种状况,抛出异常、返回特殊值、阻塞、超时,下表总结了这些方法: 抛出异常特殊值阻塞超时插入add(e)offer(e)put(e)offer(e, time, unit)移除remove()poll()take()poll(time, unit)检查转载 2016-03-16 22:50:15 · 350 阅读 · 0 评论 -
多线程 : 使用 Exchanger 两个线程间交换数据(一手交钱一手交货)
Exchanger可以在两个线程之间交换数据,只能是2个线程,他不支持更多的线程之间互换数据。当线程A调用Exchange对象的exchange()方法后,他会陷入阻塞状态,直到线程B也调用了exchange()方法,然后以线程安全的方式交换数据,之后线程A和B继续运行package thread;import java.util.ArrayList;import java.原创 2016-03-16 22:07:53 · 391 阅读 · 0 评论 -
多线程 : CountDownLatch 同步工具类 用法
package thread;import java.text.SimpleDateFormat;import java.util.Date;import java.util.concurrent.CountDownLatch;public class CountDownLatchDemo { final static SimpleDateFormat sdf=new Simple原创 2016-03-16 21:58:49 · 326 阅读 · 0 评论 -
多线程 : CyclicBarrier 同步点 应用
package thread;import java.io.IOException;import java.util.Random;import java.util.concurrent.BrokenBarrierException;import java.util.concurrent.CyclicBarrier;import java.util.concurrent.Executo原创 2016-03-16 21:43:33 · 322 阅读 · 0 评论 -
多线程 : Java 信号量 Semaphore 使用
package thread;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Semaphore;public class TestSemaphore { public static void main(St原创 2016-03-16 21:30:55 · 350 阅读 · 0 评论 -
多线程 : 使用Lock 和 Condition 实现线程间互斥与通信
package thread;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;public class LockConditionTest { private Lock原创 2016-03-16 21:00:32 · 402 阅读 · 0 评论 -
Ubuntu 命令行 安装 Java
# add-apt-repository ppa:webupd8team/java按下Enter ,导入密钥# apt-get update# apt-get install oracle-java8-installer# apt-get install oracle-java8-set-default默认安装路径:/usr/lib/jvm ,可根据需要配置环境变量。原创 2016-03-16 20:12:28 · 451 阅读 · 0 评论 -
HashMap 的扩容机制
HashMap 类中包含3个和扩容相关的常量:static final int DEFAULT_INITIAL_CAPACITY = 1 static final int MAXIMUM_CAPACITY = 1 static final float DEFAULT_LOAD_FACTOR = 0.75f;其中,DEFAULT_INITIAL_CAPACITY 是初始容量,默认原创 2016-03-15 11:38:26 · 3433 阅读 · 0 评论 -
LeetCode : Combination Sum [java]
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited numb原创 2016-03-14 11:22:13 · 269 阅读 · 0 评论 -
多线程 : 读写锁实现缓存系统
import java.util.HashMap;import java.util.Map;import java.util.Random;import java.util.concurrent.locks.ReadWriteLock;import java.util.concurrent.locks.ReentrantReadWriteLock;public class CacheS原创 2016-03-13 22:48:53 · 435 阅读 · 0 评论 -
多线程 : 锁的用法
代码前加锁,代码后解锁,为了保证能够解锁,通常用try-finally语句包起来。import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;public class LockTest { private Lock lock = new ReentrantLock原创 2016-03-13 22:05:44 · 275 阅读 · 0 评论 -
多线程 : 使用 CompletionService 多线程返回结果
import java.util.Random;import java.util.concurrent.Callable;import java.util.concurrent.CompletionService;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorComple原创 2016-03-13 20:27:24 · 854 阅读 · 0 评论 -
多线程 : 使用 Future 获取线程返回结果
import java.util.Random;import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;impor原创 2016-03-13 20:12:10 · 1347 阅读 · 0 评论 -
多线程 : 线程池的基本用法
import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class ThreadPool { public static void main(String[] args) { // 创建一个包含固定数量线程的线程池 ExecutorService th原创 2016-03-13 17:57:38 · 427 阅读 · 0 评论 -
多线程 : 多线程共享局部变量的方法
1. 操作相同时,写一个 Runnable 实现类,内部设置成员变量,run 方法修改该变量,将该Runnable传给不同Thread使用;2. 操作不同时,在Thread调用类实例化一个数据实例,传递给不同Runnable处理,再把不同的Runnable传给不同Thread使用;3. 在Thread调用类实例化一个数据实例,使用内部Runnable类直接访问该数据实例进行处理,再把Run原创 2016-03-13 12:13:37 · 2633 阅读 · 0 评论 -
多线程 : ThreadLocal 实现线程间共享变量隔离例子
package thread;import java.util.Random;public class ThreadLocalShareDataDemo { /** * ThreadLocal类及应用技巧 将线程范围内共享数据进行封装,封装到一个单独的数据类中,提供设置获取方法 * 将该类单例化,提供获取实例对象的方法,获取到的实例对象是已经封装好的当前线程范围内的对象 *原创 2016-03-13 11:46:50 · 1405 阅读 · 0 评论 -
多线程 : 使用 wait 和 notify 实现进程间同步通信
public class S03 { private boolean isSub = true; public synchronized void sub() throws InterruptedException { if (!isSub) { this.wait(); } // do something isSub = !isSub; this.notify(原创 2016-03-12 23:20:25 · 577 阅读 · 0 评论 -
多线程 : 进程同步
静态方法 --> 锁是 类一般方法 --> 锁是 对象代码段 --> 锁是 自定义任何内容当锁相同时,方法间就能达到互斥效果。典型的3种加锁方式:public class S02 { public synchronized void out1(String str) { // something to do } public synch原创 2016-03-12 22:34:46 · 309 阅读 · 0 评论 -
多线程 : 定时器基本用法
new Timer().schedule(new TimerTask() { @Override public void run() { // TODO Auto-generated method stub } }, 5000);原创 2016-03-12 01:11:15 · 275 阅读 · 0 评论 -
多线程 : Thread 与 Runnable 内Run方法优先级
例子代码: new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub } }) { @Override public void run() { // TODO Auto-generated method stub原创 2016-03-12 00:52:32 · 1640 阅读 · 1 评论 -
LeetCode : Count and Say [java]
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原创 2016-03-11 22:09:51 · 340 阅读 · 0 评论 -
LeetCode : Sudoku Solver [java]
Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character '.'.You may assume that there will be only one unique solution.A sudoku原创 2016-03-11 21:23:54 · 320 阅读 · 0 评论 -
LeetCode : Valid Sudoku [java]
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 character '.'.A partially fille原创 2016-03-11 20:21:55 · 297 阅读 · 0 评论 -
LeetCode : Search Insert Position [java]
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.原创 2016-03-10 23:46:17 · 340 阅读 · 0 评论 -
LeetCode : 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 found原创 2016-03-10 23:26:46 · 246 阅读 · 0 评论 -
LeetCode : Search in Rotated Sorted Array [java]
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the array retur原创 2016-03-10 23:11:13 · 232 阅读 · 0 评论 -
LeetCode : Longest Valid Parentheses [java]
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.For "(()", the longest valid parentheses substring is "()",原创 2016-03-10 22:09:29 · 440 阅读 · 0 评论 -
LeetCode : Next Permutation [java]
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible原创 2016-03-10 19:35:47 · 350 阅读 · 0 评论 -
LeetCode : Substring with Concatenation of All Words [java]
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in wordsexactly once and w原创 2016-03-09 21:08:57 · 305 阅读 · 0 评论 -
LeetCode : Divide Two Integers [java]
Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.思路:注意使用long进行计算,防止int溢出,然后再判断溢出时重置。public class Solution { public int divide(原创 2016-03-09 15:10:06 · 321 阅读 · 0 评论 -
LeetCode : Implement strStr() [java]
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 ha原创 2016-03-09 00:35:14 · 278 阅读 · 0 评论 -
LeetCode : Remove Element [java]
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.思原创 2016-03-09 00:06:53 · 256 阅读 · 0 评论 -
LeetCode : Remove Duplicates from Sorted Array [java]
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with原创 2016-03-08 20:10:40 · 245 阅读 · 0 评论 -
LeetCode : Reverse Nodes in k-Group [java]
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is原创 2016-03-08 19:33:16 · 348 阅读 · 0 评论