铁锤的Blog 
  • Home
  • Archives
  • Categories
  • Tags
  • About
  •     

JVM总结

JVM总结1.类加载机制1.1类加载时机: 加载–> 验证–>准备–>解析—>初始化–>使用—>卸载 初始化的触发时机(主动引用): new一个新对象,或者调用这个类的静态方法或静态参数时。 对类进行反射调用时 初始化一个类,发现其父类还没有初始化时 当虚拟机启动时,用户指定了一个主类,发现其主类还没有进行初始化时。 1.2类的加载过程:1.2.1加载所完成的事情 通过一个类的全限定名来获取定义此类的二进制字节流。 将这个字节流转化为方法区的运行时数据结构 在内存中生成一个代表这个类的java.lang.Class对象。 1.2.2验证验证内容 文件格式验证(流—>方法区的数据结构) 元数据验证 字节码验证 符号引用验证 1.2.3准备 为类变量分配内存并设置类变量初始值的阶段。 1.2.4解析 将符号引用替换为直接引用的过程。 1.2.5初始化 执行构造器的过程 2.可达性分析算法 当一个对象到GC Roots没有任何引用链相连时,就是从GC Roots不可到达这个对象时,这个对象时不可达的。 在Java中,可
 2018-08-01   Java  编程  Jvm    Jvm 

ConcurrentHashMap学习

ConcurrentHashMap学习官方文档: ConcurrentHashMap提供了和hashTable同样的线程安全性,但是实现细节却完全不一样。取值操作是不加锁的,而且也不提供方法锁住整个表。 因为get操作不加锁,所以很多更新操作可能会同时发生。取回的值意味着最近完成的结果。对于并集操作,例如 {@code putAll} 和{@code clear}, 并发取值意味着紧紧一部分Node的插入和删除操作。同时,一次只允许一个线程做iterator操作。 重要方法:get: 可以看到get操作是不经过加锁的。 /** * Returns the value to which the specified key is mapped, * or {@code null} if this map contains no mapping for the key. * * <p>More formally, if this map contains a mapping from a key * {@code k} to
 2018-07-31   Java  编程    Java 

LeetCode-N Queens

LeetCode: N-Queens题目: 输入一个数字n,输出一个n*n大小的棋盘能放下n个皇后的所有情况的集合。 例子: Input: 4 Output: [ [".Q..", // Solution 1 "...Q", "Q...", "..Q."], ["..Q.", // Solution 2 "Q...", "...Q", ".Q.."] ] 解释:对于4皇后来说有以上两种独特的解法 思路: 不难想象解这个题需要回溯法,每一行只可能有一个Queen,所以通过行来找,每一行放置一个Q即结束。我的解法思路和之前解数独问题的解法一致。 代码: 虽然代码思路差不多,但是有很多细节也比较麻烦,比如判断这个地方能否放置Queen。 class Solution { public List<List<String>> solveNQueens(int n) {
 2018-07-24   编程  算法    算法  Leetcode 

LeetCode-Merge Intervals

LeetCode:Merge Intervals题目: 给一个集合的区间,把有重叠的区间合并起来 例子: Example 1: Input: [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6]. Example 2: Input: [[1,4],[4,5]] Output: [[1,5]] Explanation: Intervals [1,4] and [4,5] are considerred overlapping. 思路: 一开始我的思路是把start排序一遍,这样O(n)的复杂度就可以搞定,但是越写越恶心,因为边界情况很多。 代码:/** * Definition for an interval. * public class Interval { * int start; * int end; * Interva
 2018-07-24   编程  算法    算法  Leetcode 

LeetCode-Rotate Image

LeetCode:Rotate Image题目: 将一个矩阵顺时针旋转90度,要求空间复杂度为O(1) 例子: Given input matrix = [ [1,2,3], [4,5,6], [7,8,9] ], rotate the input matrix in-place such that it becomes: [ [7,4,1], [8,5,2], [9,6,3] ] 思路: 说来惭愧,这个题一开始拿到没思路,因为没有暴力方法,搁置几天后再看发现其实可以通过翻转来解题,先以反对角线为轴旋转,然后在水平翻转即为旋转90度的效果。 [1,2,3] [9,6,3] [7,4,1] [4,5,6] --> [8,5,2] --> [8,5,2] [7,8,9] [7,4,1] [9,6,3] 代码: 做完居然是标答超过100% class Solution { public void rotate(int
 2018-07-23   编程  算法    算法  Leetcode 

LeetCode-Group Anagrams

LeetCode:Group Anagrams题目: 给一个数组的字符串,将其中含有相同字母的分组 例子: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], ["nat","tan"], ["bat"] ] 思路: 这道题很简单,就是遍历数组,放进Map中,但是第一次我将字符的和作为Key,这里就出问题了,因为存在不同字符组合相同值得情况,比如ad和bc。后面改成用排过序的字符数组新生成的字符串作为Key就没问题了。但是这个做法复杂度为O(nmlogm),有优化空间。 代码:class Solution { public List<List<String>> groupAnagrams(S
 2018-07-23   编程  算法    算法  Leetcode 

LeetCode-Bulb Switcher

LeetCode: Bulb Switcher题目: 初始时有 n 个灯泡关闭。 第 1 轮,你打开所有的灯泡。 第 2 轮,每两个灯泡你关闭一次。 第 3 轮,每三个灯泡切换一次开关(如果关闭则开启,如果开启则关闭)。第 i 轮,每 i 个灯泡切换一次开关。 对于第 n 轮,你只切换最后一个灯泡的开关。 找出 n 轮后有多少个亮着的灯泡。 例子: Input: 3 Output: 1 Explanation: At first, the three bulbs are [off, off, off]. After first round, the three bulbs are [on, on, on]. After second round, the three bulbs are [on, off, on]. After third round, the three bulbs are [on, off, off]. So you should return 1, because there is only one bulb is on. 思路: 一开始我想的是就根据
 2018-07-20   编程  算法    算法  Leetcode 

LeetCode-Trapping Rain Water

LeetCode: Trapping Rain Water题目: 给一个不含负值的数组,求其最大的容水量。 例子: 结合下图看 Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6 思路: 这道题经历了很久才完成,主要是感觉很棘手,不好找切入点。后来借鉴了一个思路,就是每次找最高的点,然后找到次高的左右各一个点,以次高的点为容器边缘,算它们与最高点之间的容水量,然后通过递归,再找次高点之外的的存水量。 代码: 有些丑陋,但是效率击败了96%的人。 class Solution { public int trap(int[] height) { if( height == null || height.length < 3){ return 0; } int total = getTheTotalWater(height,0,height.length,0); return total; } private int getTh
 2018-07-19   编程  算法    算法  Leetcode 

LeetCode-Find the Duplicate Number

LeetCode: Find the Duplicate Number题目: 一个n+1长度的数组,其中的值为1~n,所以肯定有一个重复的值,找出这个重复的值,并且只用O(n)的时间复杂度,O(1)的空间复杂度 例子: Input: [1,3,4,2,2] Output: 2 思路: 这个题是当时小米面试我的一道题,当时思路已经想出来了,但是代码没写出来,很遗憾。思路就是把当前的值不在对应的下标时则替换,如果当前的值等于对应下标的值,也就说明重复了,返回即可。 代码:class Solution { public int findDuplicate(int[] nums) { if( nums == null || nums.length < 1){ return 0; } int i = 0; while(nums[i] != i){ if(nums[nums[i]] == nums[i]){ return num
 2018-07-19   编程  算法    算法  Leetcode 

LeetCode-Spiral Matrix II

LeetCode: Spiral Matrix II题目: 输入一个n,返回它的平方的一个顺时针排列的矩阵 例子: Input: 3 Output: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 思路: 同顺时针打印矩阵一样,甚至要简单一些,因为不会存在只剩1行,1列的情况 代码:class Solution { public int[][] generateMatrix(int n) { if(n == 0){ return null; } int[][] result = new int[n][n]; int rowBegin = 0; int rowEnd = n - 1; int colBegin = 0; int colEnd = n - 1; int index = 1; while( rowBegin <= rowEnd &&
 2018-07-18   编程  算法    算法  Leetcode 
1…34567

Search

Hexo Fluid
 总访问量 次   总访客数 人