Java
文章平均质量分 52
蓝色街灯_BlueLight
编程是一种美德,是促使一个人不断向上发展的一种原动力。
展开
-
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 · 287 阅读 · 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 · 280 阅读 · 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 · 254 阅读 · 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 · 297 阅读 · 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 · 216 阅读 · 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 · 195 阅读 · 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 · 355 阅读 · 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 · 273 阅读 · 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 · 256 阅读 · 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 · 281 阅读 · 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 · 240 阅读 · 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 · 215 阅读 · 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 · 269 阅读 · 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 · 211 阅读 · 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 · 293 阅读 · 0 评论 -
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 · 297 阅读 · 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 · 1583 阅读 · 1 评论 -
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 · 528 阅读 · 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 · 3376 阅读 · 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 · 237 阅读 · 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 · 385 阅读 · 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 · 227 阅读 · 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 · 798 阅读 · 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 · 1292 阅读 · 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 · 389 阅读 · 0 评论 -
多线程 : 多线程共享局部变量的方法
1. 操作相同时,写一个 Runnable 实现类,内部设置成员变量,run 方法修改该变量,将该Runnable传给不同Thread使用;2. 操作不同时,在Thread调用类实例化一个数据实例,传递给不同Runnable处理,再把不同的Runnable传给不同Thread使用;3. 在Thread调用类实例化一个数据实例,使用内部Runnable类直接访问该数据实例进行处理,再把Run原创 2016-03-13 12:13:37 · 2525 阅读 · 0 评论 -
多线程 : ThreadLocal 实现线程间共享变量隔离例子
package thread;import java.util.Random;public class ThreadLocalShareDataDemo { /** * ThreadLocal类及应用技巧 将线程范围内共享数据进行封装,封装到一个单独的数据类中,提供设置获取方法 * 将该类单例化,提供获取实例对象的方法,获取到的实例对象是已经封装好的当前线程范围内的对象 *原创 2016-03-13 11:46:50 · 1356 阅读 · 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 · 524 阅读 · 0 评论 -
多线程 : 进程同步
静态方法 --> 锁是 类一般方法 --> 锁是 对象代码段 --> 锁是 自定义任何内容当锁相同时,方法间就能达到互斥效果。典型的3种加锁方式:public class S02 { public synchronized void out1(String str) { // something to do } public synch原创 2016-03-12 22:34:46 · 273 阅读 · 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 · 348 阅读 · 0 评论 -
多线程 : 定时器基本用法
new Timer().schedule(new TimerTask() { @Override public void run() { // TODO Auto-generated method stub } }, 5000);原创 2016-03-12 01:11:15 · 244 阅读 · 0 评论 -
LeetCode : Swap Nodes in Pairs [java]
Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. Y原创 2016-03-08 00:43:08 · 347 阅读 · 0 评论 -
LeetCode : Merge k Sorted Lists [java]
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.思路:归并法,主要利用切分简化问题。/** * Definition for singly-linked list. * public class ListNode { * in原创 2016-03-07 23:48:43 · 331 阅读 · 0 评论 -
LeetCode : ZigZag Conversion [java]
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I原创 2016-03-05 15:19:16 · 215 阅读 · 0 评论 -
多线程 : 面试题[总结]
1. 使用BlockingQueue 实现 生产者 消费者2. 使用 Semaphore 实现 多线程 依次执行3. 不是同一个对象,synchronized()不能互斥4. "1"+"" 编译器优化为 "1" 5. 不定期更新,记录原创 2016-03-17 11:45:37 · 230 阅读 · 0 评论 -
LeetCode : Longest Palindromic Substring [java]
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.(思路:把字符串字符间的间隙考虑进去原创 2016-03-05 12:34:27 · 232 阅读 · 0 评论 -
LeetCode : Median of Two Sorted Arrays [java]
There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).思路:类似于归并排序,找到一半的排好序的序列,最原创 2016-03-05 11:52:19 · 191 阅读 · 0 评论 -
LeetCode : Longest Substring Without Repeating Characters [java]
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原创 2016-03-05 11:01:37 · 201 阅读 · 0 评论 -
多线程 : 并发库中并发集合
ConcurrentHashMapCopyOnWriteArrayListCopyOnWriteArraySet用法跟普通Map 、 List 、Set 一样,但是这些支持边迭代边修改等并发操作。原创 2016-03-17 10:49:39 · 284 阅读 · 0 评论 -
多线程 : 阻塞队列
BlockingQueue最终会有四种状况,抛出异常、返回特殊值、阻塞、超时,下表总结了这些方法: 抛出异常特殊值阻塞超时插入add(e)offer(e)put(e)offer(e, time, unit)移除remove()poll()take()poll(time, unit)检查转载 2016-03-16 22:50:15 · 294 阅读 · 0 评论