![](https://img-blog.csdnimg.cn/20201014180756919.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
记录一下刷过的LeetCode...
放羊的大飞
这个作者很懒,什么都没留下…
展开
-
LeetCode_ThreeSum_Array_M
15. 三数之和 先排序,然后用双指针即可快速查找。注意数据的去重class Solution { public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> result = new ArrayList<>(); ...原创 2020-04-05 00:53:29 · 97 阅读 · 0 评论 -
LeetCode_合并两个有序链表_LinkedList_E
21. 合并两个有序链表pre:/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public...原创 2020-05-04 00:20:15 · 127 阅读 · 0 评论 -
LeetCode_数组的度_Array_E
697. 数组的度class Solution { public int findShortestSubArray(int[] nums) { if (nums == null || nums.length == 0) { return -1; } Map<Integer, Node> map = n...原创 2020-05-03 23:43:45 · 121 阅读 · 0 评论 -
LeetCode_旋转矩阵_Array_M
01.07. 旋转矩阵简单粗暴:class Solution { public void rotate(int[][] matrix) { int size = matrix.length; int[][] arr = new int[size][size]; for (int i = 0; i < size; i++) { ...原创 2020-04-07 22:30:48 · 111 阅读 · 0 评论 -
LeetCode_最接近的三数之和_Array_M
16. 最接近的三数之和class Solution { public int threeSumClosest(int[] nums, int target) { if (nums.length < 3) { return -1; } Arrays.sort(nums); int res =...原创 2020-04-05 02:23:16 · 71 阅读 · 0 评论 -
LeetCode_两数之和_Array_E
1. 两数之和 暴力:class Solution { public int[] twoSum(int[] nums, int target) { int[] res = new int[2]; if (nums == null || nums.length == 0) { return res; } ...原创 2020-04-05 01:25:06 · 71 阅读 · 0 评论 -
LeetCode_除自身以外数组的乘积_Array_M
238. 除自身以外数组的乘积 不能用除法,不能额外空间,所以先用一个数组存第 i 位左边的所有数乘积,再倒过来遍历赋值:res = 左边乘积 * 右边乘积class Solution { public int[] productExceptSelf(int[] nums) { int[] result = new int[nums.length]; ...原创 2020-04-04 17:39:11 · 81 阅读 · 0 评论 -
LeetCode_盛最多水的容器_Array_M
11. 盛最多水的容器 暴力...class Solution { public int maxArea(int[] height) { int max = 0; for (int i = 0; i < height.length - 1; i++) { for (int j = i + 1; j < h...原创 2020-04-04 15:29:45 · 69 阅读 · 0 评论 -
LeetCode_整数反转_Math_E
7. 整数反转class Solution { public int reverse(int x) { long result = 0; while (x != 0) { // 如果是-123,%的值是-3、-2、-1,所以不用单独考虑负数 result = result * 10 + x % 10; ...原创 2020-04-04 14:45:01 · 76 阅读 · 0 评论 -
LeetCode_字符串转换整数 (atoi)_String_M
8. 字符串转换整数 (atoi)class Solution { public int myAtoi(String str) { if (str == null || str.length() == 0 || str.trim().length() == 0) { return 0; } str = str....原创 2020-04-03 21:21:41 · 71 阅读 · 0 评论 -
LeetCode_生命游戏_Array_M
289. 生命游戏class Solution { public void gameOfLife(int[][] board) { List<IndexNum> modifyList = new ArrayList<>(); //存需要改变的坐标 int m = board.length, n = board[0].length; // 坐标数组...原创 2020-04-02 23:30:01 · 72 阅读 · 0 评论 -
LeetCode_有效的括号_Stack_E
20. 有效的括号class Solution { public boolean isValid(String s) { Stack<Character> stack = new Stack<>(); for (char c : s.toCharArray()) { if (stack.isEmpty()...原创 2020-04-01 02:54:59 · 75 阅读 · 0 评论 -
LeetCode_排序数组_Array_M
912. 排序数组class Solution { public int[] sortArray(int[] nums) { quickSort(nums, 0, nums.length - 1); return nums; } // 快排 private void quickSort(int[] arr, int l, int r...原创 2020-03-31 21:21:07 · 99 阅读 · 0 评论 -
LeetCode_圆圈中最后剩下的数字_Math_E
面试题62. 圆圈中最后剩下的数字class Solution { public int lastRemaining(int n, int m) { // 模拟法 List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) { ...原创 2020-03-30 03:01:26 · 78 阅读 · 0 评论 -
LeetCode_地图分析_BFS_M
1162. 地图分析class Solution { public int maxDistance(int[][] grid) { Queue<Node> queue = new LinkedList<>(); // 把首节点放进去: 所有的1 for (int i = 0; i < grid.length...原创 2020-03-30 01:43:13 · 97 阅读 · 0 评论 -
LeetCode_实现 Trie (前缀树)_Trie_M
208. 实现 Trie (前缀树)class Trie { //定义节点 class TrieNode { boolean isEnd; TrieNode[] next = new TrieNode[26]; public TrieNode(){} } private TrieNode root; ...原创 2020-03-29 02:12:09 · 88 阅读 · 0 评论 -
LeetCode_单词的压缩编码_Array_M
820. 单词的压缩编码 暴力..class Solution { public int minimumLengthEncoding(String[] words) { String[] temp = new String[words.length]; // 不能压缩的字符串集合 int size = 0, sum = 0; ...原创 2020-03-28 23:44:35 · 112 阅读 · 0 评论 -
LeetCode_可以被一步捕获的棋子数_Array_E
999. 可以被一步捕获的棋子数class Solution { public int numRookCaptures(char[][] board) { if (board == null) { return -1; } int x = 0, y = 0, sum = 0; for (int i...原创 2020-03-28 21:09:48 · 146 阅读 · 0 评论 -
LeetCode_卡牌分组_Array_E
914. 卡牌分组// 暴力..class Solution { public boolean hasGroupsSizeX(int[] deck) { int[] temp = new int[10000]; for (int a : deck) { temp[a] ++; } int j =...原创 2020-03-27 02:47:52 · 77 阅读 · 0 评论 -
LeetCode_三维形体的表面积_Math_E
892. 三维形体的表面积class Solution { public int surfaceArea(int[][] grid) { int n = grid.length, sum = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) {...原创 2020-03-25 01:58:43 · 66 阅读 · 0 评论 -
LeetCode_环形链表_LinkedList_E
141. 环形链表常规思路:放到哈希表里面,如果碰到相同的,就认为是有环/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * nex...原创 2020-03-23 22:56:09 · 71 阅读 · 0 评论 -
LeetCode_链表的中间结点_LinkedList_E
876. 链表的中间结点/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode...原创 2020-03-23 21:12:59 · 92 阅读 · 0 评论 -
LeetCode_使数组唯一的最小增量_Array_M
945. 使数组唯一的最小增量class Solution { public int minIncrementForUnique(int[] A) { if (A == null || A.length == 0) { return 0; } Arrays.sort(A); int sum = 0...原创 2020-03-23 00:23:47 · 81 阅读 · 0 评论 -
LeetCode_最长回文串_String_E
409. 最长回文串class Solution{ public int longestPalindrome(String s) { if(s == null || s.length() == 0) { return 0; } int[] ch = new int[52]; for (char c : s.toCharArray()) { if (c >...原创 2020-03-19 21:44:42 · 66 阅读 · 0 评论 -
LeetCode_矩形重叠_Math_E
836. 矩形重叠class Solution { public boolean isRectangleOverlap(int[] rec1, int[] rec2) { // 方法一: 计算重叠区域 if (Math.min(rec1[2], rec2[2]) > Math.max(rec1[0], rec2[0]) && ...原创 2020-03-18 23:16:39 · 97 阅读 · 0 评论 -
LeetCode_拼写单词_Array_E
1160. 拼写单词 hashMap日常暴力...class Solution { public int countCharacters(String[] words, String chars) { char[] ch = chars.toCharArray(); if (ch.length == 0) { re...原创 2020-03-17 22:27:21 · 62 阅读 · 0 评论 -
LeetCode_重复的子字符串_String_E
459. 重复的子字符串class Solution { public boolean repeatedSubstringPattern(String s) { if (s == null || s.length() < 2) { return false; } int length = s.length(...原创 2020-03-16 22:04:52 · 77 阅读 · 0 评论 -
LeetCode_字符串压缩_String_E
01.06. 字符串压缩class Solution { public String compressString(String S) { if (S == null || S.length() == 0) { return ""; } char[] c = S.toCharArray(); St...原创 2020-03-16 21:26:49 · 80 阅读 · 0 评论 -
LeetCode_用栈实现队列_Stack_E
232. 用栈实现队列 两个栈实现: 栈s1中的数据是:直接push,从下到上是1234,跟队列的顺序相反; 栈s2中的数据是:从上到下是1234,直接pop符合队列的顺序。 s2不为空时,直接在s2中pop就行; s2为空时,先把s1中所有元素都倒过来挪到s2,再从s2中popclass MyQueue { Stack<Integer...原创 2020-03-16 02:31:06 · 177 阅读 · 0 评论 -
LeetCode_用队列实现栈_Stack_E
225. 用队列实现栈方法一: 用两个队列来回poll和offer实现: 压入栈O(1)、弹出栈O(n)class MyStack { private Queue<Integer> q1, q2; int top; // 记录栈的栈顶元素,其实就是队列的尾 /** Initialize your data structure here...原创 2020-03-16 01:34:06 · 113 阅读 · 0 评论 -
LeetCode_求众数II_Array_M
229. 求众数 II 169. 多数元素的升级版class Solution { public List<Integer> majorityElement(int[] nums) { // 先用投票法,找出两个候选人; 再遍历这两个候选人出现的次数是不是符合要求 if (nums == null || nums.len...原创 2020-03-13 21:39:21 · 69 阅读 · 0 评论 -
LeetCode_多数元素_Array_E
169. 多数元素// 自己的笨比方法,用hashMap存,又慢又费内存class Solution { public int majorityElement(int[] nums) { if (nums == null || nums.length == 0) { return -1; } Map<I...原创 2020-03-13 20:45:33 · 64 阅读 · 0 评论 -
LeetCode_字符串的最大公因子_String_E
1071. 字符串的最大公因子// 又是自己的暴力..class Solution { public String gcdOfStrings(String str1, String str2) { if(str1 == null || str2 == null) { return ""; } String s...原创 2020-03-12 22:00:11 · 85 阅读 · 0 评论 -
LeetCode_二叉树的堂兄弟节点_BFS/DFS_E
993. 二叉树的堂兄弟节点 自己做的第一个想到的还是BFS... 代码又臭又长效率还低../** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(...原创 2020-03-11 21:42:21 · 115 阅读 · 0 评论 -
LeetCode_将数组分成和相等的三个部分_Array_E
1013. 将数组分成和相等的三个部分class Solution { public boolean canThreePartsEqualSum(int[] A) { int sum = 0; for (int a : A) { sum += a; } if (sum % 3 != 0 || A....原创 2020-03-11 20:29:51 · 85 阅读 · 0 评论 -
LeetCode_二叉树的层次遍历_BFS/DFS_M
102. 二叉树的层次遍历自己做的,完全按BFS模板搞的:/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; }...原创 2020-03-10 22:36:09 · 124 阅读 · 0 评论 -
LeetCode_买卖股票的最佳时机_DP_E
121. 买卖股票的最佳时机DP?????原创 2020-03-09 22:33:48 · 91 阅读 · 0 评论 -
LeetCode_和为s的连续正数序列_Math_E
和为s的连续正数序列class Solution { public int[][] findContinuousSequence(int target) { List<int[]> list = new ArrayList<>(); int sum = 0; int max = target / 2 + 1; ...原创 2020-03-06 21:35:33 · 105 阅读 · 0 评论 -
LeetCode_反转链表_LinkedList_E
206. 反转链表/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { // 迭代 publi...原创 2020-03-05 22:44:29 · 68 阅读 · 0 评论 -
LeetCode_分糖果_Math_E
1103. 分糖果 IIclass Solution { public int[] distributeCandies(int candies, int num_people) { int[] arr = new int[num_people]; int i = 0, n = 1; // i表示自增的下标,n表示本次应该分多少。方便看就没合到一个属...原创 2020-03-05 21:40:29 · 93 阅读 · 0 评论