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();}
}

CopyOnWriteArrayList原理及算法题存档相关推荐

  1. 算法题存档20200505

    给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...

  2. 算法题存档20191223

    题目描述 删除给出链表中的重复元素,使链表中的所有元素都只出现一次 例如: 给出的链表为1->1->2,返回1->2. 给出的链表为1->1->2->3->3 ...

  3. 算法题存档20190207

    题目描述 如果一个整数只能被1和自己整除,就称这个数是素数. 如果一个数正着反着都是一样,就称为这个数是回文数.例如:6, 66, 606, 6666 如果一个数字既是素数也是回文数,就称这个数是回文 ...

  4. 算法题存档20190127

    题目描述 假设一个探险家被困在了地底的迷宫之中,要从当前位置开始找到一条通往迷宫出口的路径.迷宫可以用一个二维矩阵组成,有的部分是墙,有的部分是路.迷宫之中有的路上还有门,每扇门都在迷宫的某个地方有与 ...

  5. 算法题存档20200627(树)

    给你一棵以 root 为根的二叉树和一个 head 为第一个节点的链表. 如果在二叉树中,存在一条一直向下的路径,且每个点的数值恰好一一对应以 head 为首的链表中每个节点的值,那么请你返回 Tru ...

  6. 算法题存档2020425

    给定一个 没有重复 数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [   [1,2,3],   [1,3,2],   [2,1,3],   [2,3,1],   [3, ...

  7. 20191219算法题存档

    题目描述 给出两个有序的整数数组A和B,请将数组B合并到数组A中,变成一个有序的数组 注意: 可以假设A数组有足够的空间存放B数组的元素,A和B中初始的元素数目分别为m和n public class ...

  8. 20190730算法题存档

    题目描述 Given a singly linked list L: L 0→L 1→-→L n-1→L n, reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→ ...

  9. 20190724算法题存档

    题目描述 Sort a linked list in O(n log n) time using constant space complexity. public class Solution {p ...

最新文章

  1. DotNetTextBox V3.0 所见即所得编辑器控件 For Asp.Net2.0(ver 3.0.9Beta)
  2. java请求怎么获取token,如何获取变量token的值
  3. python Intel Realsense D435 图像自动采集程序 自动剔除模糊图像
  4. SpingBoot-Thymeleaf-bootstrapTable-分页之H5
  5. 安装 | Matlab2022a下载及新功能一览
  6. 【nodejs笔记3】Express基本用法,和路由控制,和模板渲染ejs
  7. mysql大量重复值建立索引_对于有大量重复数据的表添加唯一索引
  8. 信息学奥赛C++语言:摘李子
  9. MySQL 的慢 SQL 怎么优化?
  10. android使用地图编程,基于Android实现百度地图定位过程详解
  11. [转载] Linux进程状态解析之R、S、D、T、Z、X
  12. Mysql提权之反弹shell
  13. 魔兽世界模型文件.m2 在D3D下的渲染
  14. vb mysql 教程_[转载]VB.net教程之数据库简单操作
  15. 【Python学习笔记】(九)正则表达式:re模块
  16. 如何使用手机将PDF合并呢?分享一个手机合并文件方法
  17. CMU CSAPP : Decoding lab
  18. 机器学习:决策树的预剪枝和后剪枝
  19. HTML和CSS隐藏元素的四种方法
  20. 「儒系」产品经理:管理预期,做好增长的3个核心要素

热门文章

  1. Struts2中Action的属性接收参数
  2. Android之使用MediaPlayer和SurfaceView组件播放一个简单的视频
  3. Jsp之我遇到过的中文乱码问题和解决方法
  4. 30. 包含min函数的栈
  5. Elasticsearch安装X-Pack插件
  6. 为什么手机升级系统反应慢了_手机卡顿反应慢怎么解决?久用不卡顿的手机盘点...
  7. [蓝桥杯][2019年第十届真题c/c++B组]迷宫(寻找路径bfs及文件输入输出)
  8. web.xml mysql_JSP登录验证脚本失败(mysql后端)web.xml servlet映射?
  9. mysql 设置 server id_MySQL中如何设置 auto_increment 重新计数 主键id从1开始
  10. PostgreSQL统计信息的几个重要视图