Java
文章平均质量分 64
Java Mini CookBook
Tech In Pieces
这个作者很懒,什么都没留下…
展开
-
Java:实现接口的方法,是否需要@override?
@override notation这种方法 是在告诉编译器,下面的方法是我们重写父类的方法。如果不加的话 编译器是不会判断你是不是重写了父类的方法(还是说自己搞了一个方法)如果参数不同(override的话参数需要相同)程序是不会报错的(就是说不会认为你的"typo"是错误)这会留下一个潜在的Bug.同样 当我们实现接口方法的时候 如果我们不override 那么编译器就不知道这个究竟是重写 还是说自己customized的方法。总结:还是写上吧。...原创 2021-12-11 05:32:23 · 2310 阅读 · 0 评论 -
Core Java回顾:OOP, JVM, Keywords, Collections
OOP, JVM, Keywords, Collections要深入一点 不要只会速记的那些东西\![\\\\\\\\\\\\\\\\](https://img-blog.csdnimg.cn/bcbc7cc99f16485c8a0e908b359b51f7.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAVGVjaCBJbiBQaWVjZXM=,size_20,color_FF原创 2021-12-11 05:30:31 · 65 阅读 · 0 评论 -
LeetCode必备:Java数据结构的常用方法
基础数据类型和字符串:基础操作符 比如说+ == !=之类的 只能用于基础数据 类型和字符串能用。而且尤其注意相比较两个对象的值只能用equals()字符串的相关方法:Stringvar.length(): intvar.indexOf(“a”):intvar.charAt(int index): charvar.substring(int beginIndex): Stringvar.substring(int beginIndex, int endIndex): String //包原创 2021-07-23 04:45:53 · 475 阅读 · 0 评论 -
从几个典型例题来深入思考Backtracking
前言:我自己的理解:backtracking是complete search+剪枝,最坏的复杂度是complete search,但是如果我们能发现不符合条件的 我们会提前剪枝,减少复杂度。而且backtracking的最典型特征就是add-recursion-remove模式。backtracking的本质实际上就是dfsLC46 Permutationclass Solution { public List<List<Integer>> permute(int原创 2021-01-07 09:11:25 · 203 阅读 · 0 评论 -
Bit Manipulation(位运算)技巧总结
异或的特性。第 136 题,第 268 题,第 389 题,第 421 题x ^ 0 = xx ^ 11111……1111 = ~xx ^ (~x) = 11111……1111x ^ x = 0a ^ b = c => a ^ c = b => b ^ c = a (交换律)a ^ b ^ c = a ^ (b ^ c) = (a ^ b)^ c (结合律)构造特殊 Mask,将特殊位置放 0 或 1。将 x 最右边的 n 位清零, x & ( ~0 <<原创 2021-01-02 12:09:26 · 231 阅读 · 0 评论 -
LeetCode 152: Maximum Product subarray:
LeetCode 152: Maximum Product subarray:using dp, the dp[i] indicates that the maximum product of subarray ends in A[i]. however, due to we have negative number in A, and we know that any number mupltiplied by a negative will make原创 2020-12-25 07:42:08 · 119 阅读 · 0 评论 -
Java虚拟机(JVM)和Python解释器有什么区别?
在Java中始终使用"虚拟机"时,很少会读到Python"虚拟机"。两者都解释字节码(一个是解释Java 一个是解释Python); 为什么一个叫虚拟机,另一个叫解释器(Interpreter)?注意 这里的"虚拟机"是指处理虚拟机,而不是系统虚拟机(例如Qemu或Virtualbox)。进程虚拟机是只是提供一般编程环境的程序-程序可以编程。下面详细解释以下两种叫法的起源:Java具有解释器和虚拟机,而Python具有虚拟机机器和口译员。之所以"虚拟机"更多Java中的通用术语,而"解释器"是Pyt原创 2020-12-24 05:18:17 · 3243 阅读 · 0 评论 -
如何在JVM上执行Java文件呢?为什么Java文件本质上要在JVM中运行?
首先我们写个 HelloWorld.java接下来有两步:编译.java,运行.java进行编译:zhangjg@linux:/deve/workspace/HelloJava/src$ javac HelloWorld.java zhangjg@linux:/deve/workspace/HelloJava/src$ lsHelloWorld.class HelloWorld.javajavac命令的意思是:java complier编译出来一个新的文件:HelloWord.class原创 2020-12-24 03:04:23 · 746 阅读 · 0 评论 -
Java Redis最基础入门
首先 什么是Redis?Redis是个开源免费的高性能的k-v数据库。Redis有哪些特点?支持数据持久化支持多种类型的数据:k-v型数据,list, set, hash型数据支持数据的备份,即master slave模式的数据备份。redis所有的操作都是原子性的,意思是要么成功执行 要么完全失败不执行redis都支持哪些数据类型呢?String, Hash, List, Set, sorted Set.另外一种数据库:Memcache与Redis有什么区别?Memcache把数据原创 2020-12-23 10:26:48 · 172 阅读 · 0 评论 -
036 Three Chars Distinct
given a string s, find out all the substring of this string with length equals to 3 that all of its three chars are distinct.get the number of substring that meets the requirements. return the value.this problem is pretty straight forward too.but I’m th原创 2020-12-19 07:19:43 · 274 阅读 · 0 评论 -
模板问题:Knapsack (LeetCode: Coin Change)[背包问题九讲笔记]
1原创 2020-12-19 05:29:03 · 311 阅读 · 0 评论 -
模板题目:Interval (LeetCode253:Meeting Rooms I: I, LeetCode729: My Calendar I)
1原创 2020-12-19 00:15:35 · 149 阅读 · 0 评论 -
模板问题:Mono Stack/Queue (LeetCode739: Daily Temperatures)
1原创 2020-12-19 05:31:10 · 371 阅读 · 0 评论 -
模板题目:Base Conversion( LeetCode: Base 7, LeetCode: Convert to Base -2)
1原创 2020-12-18 06:42:24 · 262 阅读 · 0 评论 -
模板题目:Heap( 347 Top K Frequent Elements)
Given a non-empty array of integers, return the k most frequent elements.public class Solution { public List<Integer> topKFrequent(int[] nums, int k) { Map<Integer, Integer> map = new HashMap<>(); for(int n: nums){原创 2020-12-18 01:00:33 · 148 阅读 · 0 评论 -
模板题目:Trie( 720 Longest Word in Dictionary)
Given a list of strings words representing an English Dictionary, find the longest word in words that can be built one character at a time by other words in words. If there is more than one possible answer, return the longest word with the smallest lexicog原创 2020-12-18 00:57:07 · 114 阅读 · 0 评论 -
模板题目:Union Find( 721 Accounts Merge)
Given a list accounts, each element accounts[i] is a list of strings, where the first element accounts[i][0] is a name, and the rest of the elements are emails representing emails of the account.Now, we would like to merge these accounts. Two accounts def原创 2020-12-18 00:49:46 · 187 阅读 · 0 评论 -
模板题目:Stack( 20 Valid Parentheses)
class Solution { public boolean isValid(String s) { if (s == null || s.length() == 0) return false; if (s.length() % 2 == 1) return false; Stack<Character> stack = new Stack<>(); for (char c: s.toCha原创 2020-12-18 00:46:58 · 64 阅读 · 0 评论 -
如何把HashMap所有的元素放入PQ并且按照值排序?
LC347 Top K Frequent elements就是绝佳的例子public class Solution { public List<Integer> topKFrequent(int[] nums, int k) { Map<Integer, Integer> map = new HashMap<>(); for(int n: nums){ map.put(n, map.getOrDefault原创 2020-12-18 00:45:15 · 206 阅读 · 0 评论 -
面试的时候让你手写Java类,慌了神,咋办?
首先 慌个屁。然后 想一想你都要注意点啥。注意的点:第一:格式:class className { int attribute1; String attribute2; List< Integer> list; //属性列表,别着急初始化 public className(List< Integer> list, int attri1) { this.list = list; //同名的一定要this. attri1 =原创 2020-12-18 00:39:35 · 134 阅读 · 0 评论 -
模板题目:LinkedList/PreSum(1171 Remove Zero Sum Consecutive Nodes from Linked List)
Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences.After doing so, return the head of the final linked list. You may return any such answer.就是说 一遍又一遍的遍历这个likedlist,删除所有的加和原创 2020-12-18 00:38:10 · 143 阅读 · 0 评论 -
Java for循环中语句执行的顺序
虽然大多数时候这儿都没有什么问题 但是有的时候还是很犯迷糊 比如下面这样:for (ListNode p = preHead; p != null; p = p.next) {}问:上面的我们到底是在p == null的时候退出 还是在p.next == null的时候退出?针对上面的问题 一个图片就完事了:所以 针对上面的问题 我的答案是:先更新 再判断(即最后一步判断完 符合条件 然后再更新 )所以上述的for循环 实际上是while (p.next != null) {}...原创 2020-12-18 00:36:47 · 405 阅读 · 0 评论 -
Some Ideas about ‘Invisible Bugs‘
Some ideas About invisiable bugLeetcode 532 as example:Following is the right solution:class Solution {public int findPairs(int[] nums, int k) {int res = 0;Arrays.sort(nums);int i = 0;int j = ...原创 2020-12-17 06:05:06 · 144 阅读 · 0 评论 -
模板题目:DFS (130. Surrounded Regions)
Given a 2D board containing ‘X’ and ‘O’ (the letter O), capture all regions surrounded by ‘X’.不太好直观理解 见下面的例子:Example:X X X XX O O XX X O XX O X XAfter running your function, the board should be:X X X XX X X XX X X XX O X X也就是说将所有被X包围的O 都变成X 但是如原创 2020-12-17 05:56:58 · 125 阅读 · 0 评论 -
模板题目:Binary Search (35. Search Insert Position)
Given a sorted array of distinct integers 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.就是找插入的位置 一个最最最最基本的二分查找 但是这只是最简单的情况 还有许多情况需要添加进去。 public int searchInsert(in原创 2020-12-17 05:33:28 · 254 阅读 · 0 评论 -
模板题目:Backtracking/Subset( 90 Subsets II)
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).Note: The solution set must not contain duplicate subsets.这道题目就是非常经典的backtracking.就是采用DFSclass Solution { public List<List<Integer原创 2020-12-17 01:00:55 · 158 阅读 · 0 评论 -
模板题目:Two Pointer (186 Reverse Words in a String II)
Given an input string , reverse the string word by word.Input: [“t”,“h”,“e”," “,“s”,“k”,“y”,” “,“i”,“s”,” “,“b”,“l”,“u”,“e”]Output: [“b”,“l”,“u”,“e”,” “,“i”,“s”,” “,“s”,“k”,“y”,” ",“t”,“h”,“e”]reverse words, but now we got comma in that and we can’t do原创 2020-12-17 01:00:11 · 153 阅读 · 0 评论 -
模板题目:Interval/Mergelist (986 Interval List Intersections/56 Merge Intervals)
这两道题目 简直就是怪异方法使用大合集 toArray(), copyOf()从来不敢使用 现在 都用上了 现在是时候好好研究一下这几个方法了986Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]class Solution { public int[][] intervalInters原创 2020-12-17 00:54:47 · 128 阅读 · 0 评论 -
Java 获取变量的类型
在debug的时候还挺重要的 因为有的时候是因为类型转换的错误 而且这总错误还很蛋疼。而且有的时候还要判断某个方法到底是有没有返回。总之获取变量类型的方法时时刻刻都用的到。说了这么多 这个方法就是下面这个:System.out.println(var.getClass().toString());但是注意 如果var是个int/boolean,都不能用这个如果var是个int[] boolean[], 可以使用这个 但是返回的结果只有 class [I如果var是个Integer[] 那么返回结原创 2020-12-17 00:56:36 · 831 阅读 · 0 评论 -
Java 关于list.toArray()方法的进一步说明
经典场景(同时也是高频出错场景):很多时候 我们想把list< Integer >转化成int[] 或者把int[]转化成list.但是这其中因为原始类型和引用类型的问题。我们把list< Integer >转化成int[]第一个坑:list.toArray()函数不会改变list 而是返回Object[] arr,所以我们需要新键一个数组来承接。第二个坑:不能用int[]来承接Object[] 会出现类型转换的错误。所以如果真的想最终转化成int[] 只能遍历了(也可以用原创 2020-12-17 00:53:11 · 2351 阅读 · 0 评论 -
如何写出干净整洁的代码?
之前写swift 写的跟屎一样 以后真的工作写成这样就GG了。干净整洁的代码这几个基本原则我都懂:使用一致的格式和缩进使用清晰的变量名和方法名在必要时使用注释使用「DRY」原则(不要重复做一件事)写起来也能大致遵守着几条原则 但是有些时候局限于自己捉急的代码实现能力 不知道这种功能如何”整洁“的去实现。比如说我需要在多个类之间传递参数 所以我就把这个参数放在构造器参数集合中 一个一个往下传 但是因为在此过程中我们还要传别的参数 传着传着这参数就7,8个了 这谁顶的住啊。所以这种情况应该怎么办呢原创 2020-12-16 01:54:35 · 156 阅读 · 0 评论 -
LeetCode The Series of Typical Problem with Follow ups
为什么这些问题有follow-ups?因为他们被问的太多了,以至于follow ups都被整合到leetcode里面了。1 #atmostkdistinct, #string Substring with at most k distinct characters2 #twosum, #array TwoSum Problems & Follow-Up3 #maxsubarraysum, #array Maximum subarray problem4 #nextpermutation, #原创 2020-12-16 01:50:39 · 196 阅读 · 0 评论 -
LeetCode 25个模板题目
1 #bfs Leetcode: Max Area of Island2 #dfs LeetCode: Surrounded Regions3 #binarysearch LeetCode: Search Insert Position4 #interval, #mergelist LeetCode: Interval List Intersections, Leetcode: Merge Intervals5 #twopointer LeetCode: Reverse Words in a Str原创 2020-12-18 00:58:48 · 291 阅读 · 0 评论 -
模板题目:BFS (695. Max Area of Island)
这道题可以说是很经典了。对每一个点进行bfs 注意对遍历过的点的值的修改 用于防止多次重复统计。而且我们不用像backtracking一样最后再改回来,因为如果两个land联通 那么这两者的面积是一样的。但是实际上 下面的代码不是BFS 而是典型的DFS因为有递归class Solution { private int count = 0; public int maxAreaOfIsland(int[][] grid) { if(grid == null || gri原创 2020-12-17 00:57:35 · 122 阅读 · 0 评论 -
Why Does it not Working?(LC695 Max Area of Island)
LC695 Max Area of Islandclass Solution { private int count = 0; public int maxAreaOfIsland(int[][] grid) { if(grid == null || grid.length == 0) return 0; int res = 0; int row = grid.length; int col = grid[0原创 2020-12-14 00:44:58 · 138 阅读 · 0 评论 -
LeetCode025 Reverse Nodes in k-Group
a classic one.Two Notes:Only constant extra memory is allowed.You may not alter the values in the list’s nodes, only nodes itself may be changed.although I’ve met thing problem many times, still can’t solve it.class Solution { public ListNode reve原创 2020-12-13 01:02:42 · 120 阅读 · 0 评论 -
【经典代码块系列】排序算法
//都是哪些代码需要背下来呢?//首先是经典的排序和搜索算法 以及各种经典的算法//然后是在题型技巧讲解中明确要求背诵的算法或者代码块(这个工作量还挺大,要过一遍 记下来 然后背诵)//最后是一些常见题型的背诵 比如reverse/palindrome/BFS/DFS 这个就是自己看着 觉得有价值就背等等//首先是排序算法及其优化算法public class BubbleSort { private static void bubbleSort(int[] nums){原创 2020-12-13 00:58:55 · 159 阅读 · 0 评论 -
LeetCode263/264/1201 Ugly Number Series
the definite of ugly number is: positive numbers whose prime factors only include 2, 3, 5.LeetCode263: check if a number is ugly number or not.Idea1: think of this as a tree, and starting from the root, we have three subnodes(dividend) to choose. if ther原创 2020-12-12 01:02:33 · 143 阅读 · 0 评论 -
LeetCode035 Search Insert Position
given an array that contains no duplicate value, and a target value, search the insert position this target should be.it’s just a regular binary search.class Solution { public int searchInsert(int[] nums, int target) { int left = 0, right = nums.le原创 2020-12-12 00:56:40 · 133 阅读 · 0 评论 -
LeetCode218 The SkyLine problem
this problem is a kind of problem that is easy to understand, but have not much of experience in solving such problems.what is there not to understand? well, there are few questions:how do we outpu...原创 2020-12-12 00:56:11 · 175 阅读 · 0 评论