算法与数据结构
白露甘三
这里是白露甘三,白鹭甘三也是我
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
150. 逆波兰表达式求值
LeetCode链接 栈 class Solution { public int evalRPN(String[] tokens) { Deque<String> stack = new LinkedList<>(); for(String s : tokens){ if("+".equals(s)){ int a = Integer.parseInt(stack.pop());原创 2022-03-28 23:56:42 · 456 阅读 · 0 评论 -
1047. 删除字符串中的所有相邻重复项
LeetCode链接 栈 class Solution { public String removeDuplicates(String s) { Deque<Character> que = new LinkedList<>(); for(Character c: s.toCharArray()){ if(que.isEmpty() || que.peek() != c){ que.pu原创 2022-03-28 00:16:19 · 216 阅读 · 0 评论 -
20. 有效的括号
LeetCode链接 在这里插入代码片 时间复杂度: 空间复杂度:原创 2022-03-27 00:33:59 · 319 阅读 · 0 评论 -
225. 用队列实现栈
LeetCode链接 方法一 class MyStack { private Deque<Integer> a; private Deque<Integer> b; public MyStack() { a = new ArrayDeque<>(); b = new ArrayDeque<>(); } public void push(int x) { a.ad原创 2022-03-26 23:38:48 · 238 阅读 · 0 评论 -
232. 用栈实现队列
LeetCode链接 方法一 class MyQueue { private Deque<Integer> a; private Deque<Integer> b; public MyQueue() { a = new ArrayDeque<>(); b = new ArrayDeque<>(); } public void push(int x) { a.pu原创 2022-03-26 22:53:37 · 215 阅读 · 0 评论 -
459. 重复的子字符串
LeetCode链接 在这里插入代码片 时间复杂度: 空间复杂度:原创 2022-03-19 23:24:37 · 197 阅读 · 0 评论 -
55. 跳跃游戏
LeetCode链接 贪心算法 class Solution { public boolean canJump(int[] nums) { if(nums.length == 1) return true; // 取数组第一个元素为初始范围, 即初始化可以跳到的最远下标 int cover = nums[0]; int i = 1; while(i <= cover){ if(cover+1原创 2022-03-17 20:50:15 · 2924 阅读 · 0 评论 -
53. 最大子数组和
LeetCode链接 暴力 class Solution { public int maxSubArray(int[] nums) { int max = nums[0]; for(int i = 0; i < nums.length; i++){ int count = 0; for(int j = i; j < nums.length; j++){ count += num原创 2022-03-17 16:58:15 · 639 阅读 · 0 评论 -
376. 摆动序列
LeetCode链接 贪心算法 class Solution { public int wiggleMaxLength(int[] nums) { if(nums.length == 1) return 1; int curr = 0; int prev = 0; // 最开始的峰值 int count = 1; for(int i = 0; i < nums.length-1; i++){原创 2022-03-17 15:26:00 · 922 阅读 · 0 评论 -
455. 分发饼干
LeetCode链接 贪心算法 class Solution { public int findContentChildren(int[] g, int[] s) { // 对两个数组进行排序 Arrays.sort(g); Arrays.sort(s); int count = 0; int i = g.length-1; int j = s.length-1; // 遍历 g,即孩子胃口数组原创 2022-03-17 00:20:39 · 1119 阅读 · 0 评论 -
28. 实现 strStr()
LeetCode链接 暴力 class Solution { public int strStr(String haystack, String needle) { int m = haystack.length(); int n = needle.length(); // 遍历 haystack 字符串 for(int i = 0; i<= m-n; i++){ int index = i;原创 2022-03-16 13:17:01 · 352 阅读 · 0 评论 -
349. 两个数组的交集
LeetCode链接 集合法 class Solution { public int[] intersection(int[] nums1, int[] nums2) { Set<Integer> s1 = new HashSet<>(); Set<Integer> s2 = new HashSet<>(); for(int i: nums1){ // 存入 nums1 中所有不重复的元素原创 2022-03-16 00:02:51 · 352 阅读 · 0 评论 -
剑指 Offer 58 - II. 左旋转字符串
LeetCode链接 方法一 class Solution { public String reverseLeftWords(String s, int n) { int len = s.length(); char[] chars = s.toCharArray(); // 整个字符串数组反转 reverse(chars, 0, len-1); // 以 k 为界分别反转两个字符串 reverse(c原创 2022-03-15 17:19:08 · 340 阅读 · 0 评论 -
151. 颠倒字符串中的单词
LeetCode链接 API 法 class Solution { public String reverseWords(String s) { // 去除首尾的空格 s = s.trim(); // 以空格为分隔获取字符串集合 List<String> arr = Arrays.asList(s.split("\\s+")); Collections.reverse(arr); return原创 2022-03-15 16:29:07 · 194 阅读 · 0 评论 -
541. 反转字符串 II
LeetCode链接 方法一 class Solution { public String reverseStr(String s, int k) { char[] chars = s.toCharArray(); for(int i = 0; i< chars.length; i += 2*k){ int start = i; int end = Math.min(i+k, chars.length)-1;原创 2022-03-15 13:08:28 · 140 阅读 · 0 评论 -
剑指 Offer 05. 替换空格
LeetCode链接 方法一 class Solution { public String replaceSpace(String s) { StringBuilder sb = new StringBuilder(s); for(int i = 0; i< sb.length(); i++){ if(' ' == sb.charAt(i)){ sb.replace(i, i+1, "%20");原创 2022-03-15 00:04:07 · 137 阅读 · 0 评论 -
344. 反转字符串
LeetCode 双指针 class Solution { public void reverseString(char[] s) { // 特殊判断 if(s == null || s.length == 1) return; int j = s.length-1; for(int i = 0; i< s.length/2; i++){ char temp = s[i]; s[i] =原创 2022-03-14 21:59:20 · 403 阅读 · 0 评论 -
18. 四数之和
LeetCode链接 排序 + 双指针 class Solution { public List<List<Integer>> fourSum(int[] nums, int target) { List<List<Integer>> res = new ArrayList<>(); if(nums == null || nums.length <4) return res; Array原创 2022-03-14 21:41:13 · 117 阅读 · 0 评论 -
15. 三数之和
LeetCode链接 排序 + 双指针 class Solution { public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> res = new ArrayList<>(); // 特殊判断,长度小于 3 的数组不可能有结果 if(nums == null || nums.length <3) ret原创 2022-03-14 19:38:21 · 564 阅读 · 0 评论 -
383. 赎金信
LeetCode链接 字母表 class Solution { public boolean canConstruct(String ransomNote, String magazine) { int[] alphbet = new int[26]; // 遍历 ransomNote 字符串 for(int i = 0; i< ransomNote.length(); i++){ char c = ransomNote.原创 2022-03-13 23:34:21 · 108 阅读 · 0 评论 -
454. 四数相加 II
LeetCode链接 这题不能用暴力法,会超时 哈希表 class Solution { public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { HashMap<Integer, Integer> map = new HashMap<>(); int count = 0; for(int i = 0; i< nums1.le原创 2022-03-13 21:41:40 · 350 阅读 · 0 评论 -
202. 快乐数
LeetCode链接 哈希表 class Solution { public boolean isHappy(int n) { Set<Integer> set = new HashSet<>(); while(true){ int count = 0; // 计算整数每个位置上数字的平方和 while(n > 0){ int pow原创 2022-03-13 20:38:07 · 345 阅读 · 0 评论 -
1. 两数之和
LeetCode链接 暴力法 class Solution { public int[] twoSum(int[] nums, int target) { int curr, trav; int[] res = new int[2]; for(int i = 0; i< nums.length - 1; i++){ for(int j = i + 1; j< nums.length; j++){原创 2022-03-12 01:06:16 · 319 阅读 · 0 评论 -
707. 设计链表
嗯写法 class ListNode{ int val; ListNode next; ListNode(){} ListNode(int val){this.val = val;} } class MyLinkedList { private ListNode head; private int size; public MyLinkedList() { head = null; size = 0; }原创 2022-03-10 10:48:07 · 264 阅读 · 0 评论 -
242. 有效的字母异位词
LeetCode链接 HashMap法 class Solution { public boolean isAnagram(String s, String t) { if(s.length() != t.length()) return false; HashMap<Character, Integer> map = new HashMap<>(); for(int i = 0; i< s.length(); i++){原创 2022-03-09 23:46:06 · 309 阅读 · 0 评论 -
142. 环形链表 II
LeetCode 双指针法 public class Solution { public ListNode detectCycle(ListNode head) { ListNode slow = head; ListNode fast = head; while(fast != null && fast.next != null){ slow = slow.next; fast = fa原创 2022-03-09 20:15:50 · 376 阅读 · 0 评论 -
07. 链表相交
LeetCode 暴力法 public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { ListNode nodeA = headA; while(nodeA != null){ ListNode nodeB = headB; while(nodeB != null){原创 2022-03-09 18:52:41 · 163 阅读 · 0 评论 -
19. 删除链表的倒数第 N 个结点
leetCode链接 虚拟头结点迭代 class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { // 虚拟头节点 ListNode virt = new ListNode(0, head); // 快指针 ListNode fast = virt; // 慢指针 ListNode slow = virt;原创 2022-03-09 17:47:36 · 232 阅读 · 0 评论 -
24. 两两交换链表中的节点
leetCode链接 迭代 class Solution { public ListNode swapPairs(ListNode head) { // 前置节点 ListNode prev = null; // 交换左节点 ListNode curr = head; // 前一个条件作用于 head 为空和节点数为偶数,后一个条件作用于节点数为奇数 while(curr != null &&am原创 2022-03-08 00:17:59 · 669 阅读 · 0 评论 -
206. 反转链表
LeetCode链接 三指针法 class Solution { public ListNode reverseList(ListNode head) { if(head == null || head.next == null) return head; // 中间节点 ListNode node = head; // 后置节点 ListNode next = head.next; while(next原创 2022-03-07 00:10:34 · 443 阅读 · 0 评论 -
203. 移除链表元素
虚拟头结点法 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next;原创 2022-03-04 22:10:57 · 303 阅读 · 0 评论 -
59. 螺旋矩阵 II
leetCode链接 模拟法 class Solution { public int[][] generateMatrix(int n) { // 定义一个 n*n 的空矩阵 int[][] matrix = new int[n][n]; // 定义左上右下边界 int left = 0; int top = 0; int right = n - 1; int bottom = n -原创 2022-03-02 11:04:05 · 147 阅读 · 0 评论 -
209.长度最小的子数组
leetCode链接 暴力法 class Solution { public int minSubArrayLen(int target, int[] nums) { int min = 0; for(int i = 0; i < nums.length;i++){ int count = 0; for(int j = i;j < nums.length; j++){ if(t原创 2022-03-01 00:11:14 · 265 阅读 · 0 评论 -
977. 有序数组的平方
leetCode链接 暴力法 class Solution { public int[] sortedSquares(int[] nums) { int len = nums.length; int[] sortedNums = new int[len]; for(int i = 0; i< len; i++){ sortedNums[i] = nums[i] * nums[i]; } Ar原创 2022-02-28 22:43:51 · 165 阅读 · 0 评论 -
27. 移除元素
leetCode 链接 第一版思路及代码 一开始的思路是,记录数组前后两个下标,右指针数据与 val 相同时下标减一,然后左指针数据和 val 相同时与右指针的数据交换,当左右指针相同时退出循环,这样就可以做到在原数组上修改 class Solution { public int removeElement(int[] nums, int val) { int left = 0; int right = nums.length - 1; while(l原创 2022-02-28 00:01:12 · 679 阅读 · 0 评论 -
704. 二分查找
LeetCode 连接https://leetcode-cn.com/problems/binary-search/ 第一版代码 class Solution { public int search(int[] nums, int target) { int left = 0; int right = nums.length - 1; while(left <= right){ int mid = (right原创 2022-02-27 16:24:34 · 272 阅读 · 0 评论
分享