1200. 最小绝对差(排序)
摘要:思路: 1.先对数组排序 2.遍历第一遍求最小绝对值差 3.遍历第二遍填充最小绝对值差的元素对 1 class Solution { 2 public List<List<Integer>> minimumAbsDifference(int[] arr) { 3 int minAbs=Integer
阅读全文
posted @
2020-01-03 19:45
Chenjin123
阅读(311)
推荐(0)
1218. 最长定差子序列(动态规划)
摘要:题意:从给定数组中提取出最长的等差序列(不一定连续),返回它的长度 暴力超时: 1 class Solution { 2 public int longestSubsequence(int[] arr, int difference) { 3 int res=1; 4 for(int i=0;i<a
阅读全文
posted @
2020-01-03 19:32
Chenjin123
阅读(309)
推荐(0)
1267. 统计参与通信的服务器(计算)
摘要:计算:先遍历一遍矩阵,记录矩阵在行列上服务器的个数。再遍历一遍矩阵,遇到服务器时,如果该行或列上存在服务器个数>1,则它是可以通讯的。 1 class Solution { 2 public int countServers(int[][] grid) { 3 int n=grid.length,m
阅读全文
posted @
2019-12-22 21:36
Chenjin123
阅读(267)
推荐(0)
673. 最长递增子序列的个数(动态规划)
摘要:动态规划: 反向查找。 1 class Solution { 2 public int findNumberOfLIS(int[] nums) { 3 int maxLen=0,res=0; //maxLen:递增子序列的最大长度,res:maxLen长递增子序列的个数 4 int n=nums.l
阅读全文
posted @
2019-12-22 19:21
Chenjin123
阅读(700)
推荐(0)
453. 最小移动次数使数组元素相等(数学)
摘要:题意:一次操作,n元数组中任意n-1个数+1。求取最小操作次数使得数组所有元素相等。 分析: 1.正面思考,每次将数组中除最大值以外的所有值+1,直到所有元素都相等。 2.反面思考,每次将数组中的最大值-1,直到所有元素相等(最少操作次数=数组和-n*数组最小值) n元数组,其中n-1元+1,因为只
阅读全文
posted @
2019-12-22 17:04
Chenjin123
阅读(636)
推荐(0)
396. 旋转函数(数学)
摘要:暴力超时:时间复杂度 O(n^2) 1 class Solution { 2 public int maxRotateFunction(int[] A) { 3 if(A.length==0) return 0; 4 int n=A.length; 5 int res=Integer.MIN_VAL
阅读全文
posted @
2019-12-21 17:37
Chenjin123
阅读(281)
推荐(0)
530. 二叉搜索树的最小绝对差(深搜)
摘要:前序遍历+排序. 时间复杂度: O(nlogn) 空间复杂度: O(n) 1 class Solution { 2 private int res=Integer.MAX_VALUE; 3 private ArrayList<Integer> arry=new ArrayList<>(); 4 pu
阅读全文
posted @
2019-12-14 15:18
Chenjin123
阅读(344)
推荐(0)
136. 只出现一次的数字(异或/哈希表)
摘要:哈希表,暴力. 1 class Solution { 2 public int singleNumber(int[] nums) { 3 Set<Integer> map=new HashSet<>(); 4 for(int num:nums){ 5 if(map.contains(num)){ 6
阅读全文
posted @
2019-12-14 14:25
Chenjin123
阅读(199)
推荐(0)
5. 最长回文子串(动态规划)
摘要:无优化的动态规划: 遍历1-n长度的字符串,并用dp数组记录前面的子回文串 时间复杂度: O(n^2) 空间复杂度: O(n^2) 1 class Solution { 2 public String longestPalindrome(String s) { 3 if(s.isEmpty()) r
阅读全文
posted @
2019-12-13 14:23
Chenjin123
阅读(504)
推荐(0)
965. 单值二叉树(深搜/广搜)
摘要:1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val
阅读全文
posted @
2019-12-12 22:59
Chenjin123
阅读(168)
推荐(0)
721. 账户合并(并查集/深搜/广搜)
摘要:题目大意: 同一个昵称可能不是同一个人,也可能是同一个人,但不同的昵称肯定不是同一个人.按照题意将同一个人的邮箱地址链接起来 并查集: 对对应的邮箱地址进行并查集操作,如果存在有交集的邮箱地址,则两个列表肯定归属于同一个人,将他们连接起来. 1 class Solution { 2 public:
阅读全文
posted @
2019-12-12 22:56
Chenjin123
阅读(236)
推荐(0)
581. 最短无序连续子数组
摘要:排序后,使用双指针对原数组和已排序数组进行比较 1 class Solution { 2 public int findUnsortedSubarray(int[] nums) { 3 int[] sort=Arrays.copyOf(nums,nums.length); 4 Arrays.sort
阅读全文
posted @
2019-12-10 11:03
Chenjin123
阅读(158)
推荐(0)
695. 岛屿的最大面积(深搜)
摘要:递归算法真是太优美了 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { val = x; } 7
阅读全文
posted @
2019-12-07 22:42
Chenjin123
阅读(261)
推荐(0)
147. 对链表进行插入排序(排序)
摘要:选择排序 1 class Solution { 2 public ListNode insertionSortList(ListNode head) { 3 int len=0; 4 for(ListNode p=head;p!=null;p=p.next){ 5 for(ListNode q=p.
阅读全文
posted @
2019-12-07 22:02
Chenjin123
阅读(173)
推荐(0)
566. 重塑矩阵(模拟)
摘要:最容易想到的就是创建一个新的一维数组,然后遍历一遍原数组nums,将其元素都按序填入新数组,最后在遍历一遍新创建的r*c的二维数组将一维数组的值按序填入. 1 class Solution { 2 public int[][] matrixReshape(int[][] nums, int r, i
阅读全文
posted @
2019-12-07 21:53
Chenjin123
阅读(160)
推荐(0)
238. 除自身以外数组的乘积(前后缀积)
摘要:没什么思路,看了题解才知道可以巧妙的运用数组前后缀积来解决.可以用数学分析一下,下式是每项结果的数学表达式,可以拆成两个部分,部分一是[1,k-1]部分数组元素的乘积,部分二是[k+1,n]部分数组元素的乘积,随着k++,部分一总是在原来的基础上乘上前一个(k-1)元素,部分二逆序过来也是同理.所以
阅读全文
posted @
2019-12-05 22:30
Chenjin123
阅读(216)
推荐(0)
105. 从前序与中序遍历序列构造二叉树(深搜)
摘要:注意: 二叉树中没有重复元素,有重复元素就搞不出来了. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7
阅读全文
posted @
2019-11-30 19:43
Chenjin123
阅读(172)
推荐(0)
104. 二叉树的最大深度(深搜/广搜)
摘要:宽度优先搜索,层序遍历各节点,并记录各节点所在层,时间复杂度 O(n)。 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode
阅读全文
posted @
2019-11-28 22:35
Chenjin123
阅读(197)
推荐(0)
101. 对称二叉树(深搜)
摘要:迭代是不可能迭代的 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x)
阅读全文
posted @
2019-11-28 22:02
Chenjin123
阅读(155)
推荐(0)
100. 相同的树(深搜)
摘要:主要是要把握同方向搜索比较,其他比较简单,另外就是剪枝提高效率要牢记心中。 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode
阅读全文
posted @
2019-11-28 21:52
Chenjin123
阅读(123)
推荐(0)