CopyOnWriteArrayList是通过在添加元素的时候复制一份副本,再在其基础上添加完新的元素之后,在将新的数组作为CopyOnWriteArrayList所保存的数组来达到线程安全。
其add()方法如下:
public boolean add(E e) {
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] elements = getArray();
int len = elements.length;
Object[] newElements = Arrays.copyOf(elements, len + 1);
newElements[len] = e;
setArray(newElements);
return true;
} finally {
lock.unlock();
}
}
在整个过程需要加锁,保证只有一个线程写入。
读不需要加锁,直接获取即可。
private E get(Object[] a, int index) {
return (E) a[index];
}
题目描述
Given a collection of numbers, return all possible permutations.
For example,
[1,2,3]have the following permutations:
[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2], and[3,2,1].
import java.util.ArrayList;
import java.util.Arrays;
public class Solution {
ArrayList<ArrayList<Integer>> result;
public ArrayList<ArrayList<Integer>> permute(int[] num) {
result = new ArrayList<>();
if(num == null || num.length == 0) {
return result;
}
Arrays.sort(num);
compute(new ArrayList<Integer>(), num);
return result;
}
public void compute(ArrayList<Integer> temp, int[] num){
if(temp.size() == num.length) {
result.add(new ArrayList<>(temp));
}
for(int i = 0; i < num.length; i++) {
if(temp.contains(num[i])) {
continue;
}
temp.add(num[i]);
compute(temp, num);
temp.remove(temp.size() - 1);
}
}
}
题目描述
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 goal is to reach the last index in the minimum number of jumps.
For example:
Given array A =[2,3,1,1,4]
The minimum number of jumps to reach the last index is2. (Jump1step from index 0 to 1, then3steps to the last index.)
public class Solution {
public int jump(int[] A) {
if(A == null || A.length <= 0) return -1;
int cur = 0;
int pre = 0;
Integer step = 0;
for(int i = 0; i < A.length; i++){
if(pre >= A.length){
return step;
}
if(i > pre){
pre = cur;
step++;
}
cur = Math.max(cur, A[i] + i);
}
return step;
}
}
题目描述
Implement wildcard pattern matching with support for'?'and'*'.
'?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "*") → true isMatch("aa", "a*") → true isMatch("ab", "?*") → true isMatch("aab", "c*a*b") → false
public class Solution {
public boolean isMatch(String s, String p) {
int si = 0;
int pi = 0;
int pis = -1;
int smatch = 0;
while(si < s.length()){
if(pi < p.length() && (s.charAt(si) == p.charAt(pi) || p.charAt(pi) == '?')){
pi++;
si++;
} else if(pi < p.length() && p.charAt(pi)=='*'){
pis = pi;
pi++;
smatch = si;
} else if(pis != -1){
pi = pis + 1;
smatch++;
si = smatch;
} else {
return false;
}
}
while(pi < p.length() && p.charAt(pi) == '*'){
pi++;
}
return pi == p.length();
}
}
题目描述
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], return6.
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
public class Solution {
public int trap(int[] A) {
if(A == null || A.length < 3) {
return 0;
}
Integer result = 0;
int leftMax = 0;
int rightMax = 0;
int left = 0;
int right = A.length - 1;
while(left <= right) {
if (leftMax < rightMax) {
result += Math.max(0, leftMax - A[left]);
leftMax = Math.max(leftMax, A[left]);
left++;
} else {
result += Math.max(0, rightMax - A[right]);
rightMax = Math.max(rightMax, A[right]);
right--;
}
}
return result;
}
}
题目描述
The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...
1is read off as"one 1"or11.
11is read off as"two 1s"or21.
21is read off as"one 2, thenone 1"or1211.
Given an integer n, generate the n th sequence.
Note: The sequence of integers will be represented as a string.
public class Solution {
public String countAndSay(int n) {
String result = "1";
int time = 1;
while(time < n) {
result = compute(result);
time++;
}
return result;
}
public String compute(String pre) {
StringBuilder result = new StringBuilder();
char tmp = pre.charAt(0);
int num = 1;
for(int i = 1; i < pre.length(); i++) {
if(pre.charAt(i) == tmp) {
num++;
} else {
result.append(num).append(tmp);
num = 1;
tmp = pre.charAt(i);
}
}
result.append(num).append(tmp);
return result.toString();
}
}