随笔分类 -  LeetCode

上一页 1 2 3 4 5 6 7 8 ··· 12 下一页
摘要:class Solution { public String replaceSpace(String s) { StringBuilder str = new StringBuilder(); for (int i = 0; i < s.length(); i++) { if (s.charAt(i 阅读全文
posted @ 2022-02-19 18:34 振袖秋枫问红叶 阅读(17) 评论(0) 推荐(0)
摘要:双指针 class Solution { public String reverseStr(String s, int k) { char[] chars = s.toCharArray(); /** * 循环的间隔设置为2k,如果最后的区间小于k,全部反转;大于k小于2k则反转前k个 * 每次右边 阅读全文
posted @ 2022-02-19 18:25 振袖秋枫问红叶 阅读(35) 评论(0) 推荐(0)
摘要:哈希表 class Solution { public boolean canConstruct(String ransomNote, String magazine) { int[] arr = new int[128]; for (int i = 0; i < magazine.length() 阅读全文
posted @ 2022-02-18 17:29 振袖秋枫问红叶 阅读(27) 评论(0) 推荐(0)
摘要:双指针法 class Solution { public boolean hasCycle(ListNode head) { /** * 判断是否存在环形链表:创建快慢指针,从头节点出发,如果两个指针能相遇,说明存在环形链表 */ ListNode fast = head; ListNode slo 阅读全文
posted @ 2022-02-18 13:29 振袖秋枫问红叶 阅读(23) 评论(0) 推荐(0)
摘要:双指针法 class Solution { public ListNode detectCycle(ListNode head) { /** * 1、判断是否存在环形链表:创建快慢指针,从头节点出发,如果两个指针能相遇,说明存在环形链表 * 2、找到环形入口:在链表头节点和快慢指针相遇的节点分别创建 阅读全文
posted @ 2022-02-18 13:22 振袖秋枫问红叶 阅读(26) 评论(0) 推荐(0)
摘要:class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { ListNode h1 = headA; ListNode h2 = headB; /** * 两个长度不同的链表,如果同步遍 阅读全文
posted @ 2022-02-18 11:24 振袖秋枫问红叶 阅读(25) 评论(0) 推荐(0)
摘要:class MyLinkedList { int size; ListNode dummyHead; public MyLinkedList() { size = 0; dummyHead = new ListNode(-1); } /** * 定义一个获取任意索引前一个节点的方法 */ publi 阅读全文
posted @ 2022-02-18 09:33 振袖秋枫问红叶 阅读(24) 评论(0) 推荐(0)
摘要:模拟 class Solution { public int[] spiralOrder(int[][] matrix) { if (matrix.length == 0){ return new int[0]; } int m = matrix.length; int n = matrix[0]. 阅读全文
posted @ 2022-02-17 16:29 振袖秋枫问红叶 阅读(22) 评论(0) 推荐(0)
摘要:模拟 import java.util.ArrayList; import java.util.List; class Solution { public List<Integer> spiralOrder(int[][] matrix) { /** * 定义上下左右边界,每次循环一圈后都要缩小边界 阅读全文
posted @ 2022-02-17 16:14 振袖秋枫问红叶 阅读(25) 评论(0) 推荐(0)
摘要:模拟 class Solution { public int[][] generateMatrix(int n) { /** * 定义上下左右边界,每次循环一圈后都要缩小边界 * 从1开始赋值 */ int[][] arr = new int[n][n]; int top = 0; int bott 阅读全文
posted @ 2022-02-17 15:50 振袖秋枫问红叶 阅读(24) 评论(0) 推荐(0)
摘要:滑动窗口法 class Solution { public int totalFruit(int[] fruits) { /** * 滑动窗口 * 最多只能存在两种数字,right从1开始 * i和j记录最先遇到的两种数字,一旦发现第三种数字,就记录当前的长度 */ int left = 0; in 阅读全文
posted @ 2022-02-17 13:46 振袖秋枫问红叶 阅读(30) 评论(0) 推荐(0)
摘要:双指针法 class Solution { public int[] sortedSquares(int[] nums) { /** * 先求出所有元素的平方,然后从两端双指针遍历 * 两端的元素总是最大的,逆序插入新的数组 */ for (int i = 0; i < nums.length; i 阅读全文
posted @ 2022-02-17 10:57 振袖秋枫问红叶 阅读(33) 评论(0) 推荐(0)
摘要:栈 import java.util.Stack; class Solution { public boolean backspaceCompare(String s, String t) { /** * 将字符串压入栈,遇到#则弹出 */ Stack<Character> stack1 = new 阅读全文
posted @ 2022-02-17 09:57 振袖秋枫问红叶 阅读(31) 评论(0) 推荐(0)
摘要:二分查找 class Solution { public boolean isPerfectSquare(int num) { int left = 1; int right = num; while (left <= right){ int mid = left + (right - left) 阅读全文
posted @ 2022-02-16 21:28 振袖秋枫问红叶 阅读(33) 评论(0) 推荐(0)
摘要:二分查找 class Solution { public int mySqrt(int x) { int left = 0; int right = x; while (left <= right){ int mid = left + (right - left) / 2; /** * 避免整形溢出 阅读全文
posted @ 2022-02-16 21:22 振袖秋枫问红叶 阅读(18) 评论(0) 推荐(0)
摘要:两个二分查找分别寻找 class Solution { public int[] searchRange(int[] nums, int target) { /** * 分别寻找第一个和最后一个target */ int left = 0; int right = nums.length - 1; 阅读全文
posted @ 2022-02-16 20:30 振袖秋枫问红叶 阅读(30) 评论(0) 推荐(0)
摘要:二分查找 class Solution { public int searchInsert(int[] nums, int target) { /** * 左闭右闭写法,left可以等于right */ int left = 0; int right = nums.length - 1; while 阅读全文
posted @ 2022-02-16 19:35 振袖秋枫问红叶 阅读(36) 评论(0) 推荐(0)
摘要:完全背包 import java.util.Arrays; class Solution { public int coinChange(int[] coins, int amount) { /** * dp[j]定义为总金额为j时最少的硬币数量 * 因为是求最小值,因此所有位置初始化为最大值 * 阅读全文
posted @ 2022-01-26 14:30 振袖秋枫问红叶 阅读(26) 评论(0) 推荐(0)
摘要:完全背包 class Solution { public int combinationSum4(int[] nums, int target) { /** * dp[i]定义为总和为i时排列的数量 * 初始dp[0] == 1 */ int[] dp = new int[target + 1]; 阅读全文
posted @ 2022-01-26 11:04 振袖秋枫问红叶 阅读(32) 评论(0) 推荐(0)
摘要:完全背包 class Solution { public int change(int amount, int[] coins) { /** * dp[j]定义为总金额为j时组合的方式数量 * 初始dp[0] == 1 */ int[] dp = new int[amount + 1]; dp[0] 阅读全文
posted @ 2022-01-26 10:33 振袖秋枫问红叶 阅读(34) 评论(0) 推荐(0)

上一页 1 2 3 4 5 6 7 8 ··· 12 下一页