
算法
zhanghe_zht
这个作者很懒,什么都没留下…
展开
-
LeetCode136. 只出现一次的数字
【代码】LeetCode136. 只出现一次的数字。原创 2023-04-13 15:05:17 · 109 阅读 · 0 评论 -
LeetCode215. 第k大的数
【代码】LeetCode215. 第k大的数。原创 2023-04-13 15:00:08 · 70 阅读 · 0 评论 -
Java 栈和队列实现
【代码】Java 栈和队列实现。原创 2023-03-30 08:54:31 · 79 阅读 · 0 评论 -
LeetCode34. 在排序数组中查找元素的第一个和最后一个位置
思路:二分查找原创 2022-06-11 17:22:54 · 90 阅读 · 0 评论 -
LeetCode31. 下一个排列
步骤:1、从右边开始,找第一个不是顺序增长的位置i;2、从右边开始,找到第一个大于等于nums[i]的nums[j]的位置j;3、交换nums[i]和nums[j];4、nums[i+1]到末尾的所有数字倒序;class Solution { public void nextPermutation(int[] nums) { int l = nums.length; int i = l - 2; while (i >= 0 &&原创 2022-05-15 17:39:07 · 131 阅读 · 0 评论 -
LeetCode24:两两交换链表中的节点
思路:递归class Solution { public ListNode swapPairs(ListNode head) { if (head == null || head.next == null){ return head; } ListNode first = head; ListNode second = first.next; ListNode third = second.ne原创 2022-05-01 16:05:48 · 461 阅读 · 0 评论 -
LeetCode22:括号生成
class Solution { List<String> res = new ArrayList<>(); String nowStr = ""; public List<String> generateParenthesis(int n) { dfs(0, 0, n); return res; } // public void dfs(int open, int close, in原创 2022-04-30 10:55:41 · 381 阅读 · 0 评论 -
LeetCode20:有效的括号
class Solution { public boolean isValid(String s) { int n = s.length(); if (n % 2 == 1) { return false; } Map<Character, Character> pairs = new HashMap<Chara`在这里插入代码片`cter, Character>() {{原创 2022-04-17 10:42:28 · 256 阅读 · 0 评论 -
LeetCode17:电话号码的字母组合
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。思路:深度优先搜索public class Solution { private static Map<String, String> numToChar = new HashMap<>(); static { numToChar.put("2", "abc");原创 2022-04-04 18:35:26 · 153 阅读 · 0 评论 -
LeetCode11:盛最多水的容器
给定一个长度为 n 的整数数组 height 。有 n 条垂线,第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。返回容器可以储存的最大水量。public class Solution { public int maxArea(int[] height) { int l = 0, r = height.length - 1; int ans = 0; w原创 2022-03-27 10:19:56 · 428 阅读 · 0 评论 -
LeetCode5:最长回文子串
class Solution { public String longestPalindrome(String s) { if (s.length() <= 1){return s;} int length = s.length(); int max = 0; int maxi = 0, maxj = 0; int[][] dp = new int[length][length]; for (int原创 2022-03-19 11:25:29 · 219 阅读 · 0 评论 -
LeetCode206:反转链表
/** * 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-05 09:49:42 · 251 阅读 · 0 评论 -
LeetCode1004:最大连续1的个数 III
给定一个二进制数组 nums 和一个整数 k ,如果可以翻转最多k 个 0 ,则返回 数组中连续 1 的最大个数 。class Solution { public int longestOnes(int[] nums, int k) { int left = 0; int max = 0; for (int right = 0; right < nums.length; right++) { if (nums[right原创 2022-03-03 22:13:52 · 267 阅读 · 0 评论 -
LeetCode78:子集
递归,回溯法public class Solution { List<List<Integer>> res = new ArrayList<>(); List<Integer> path = new ArrayList<>(); public List<List<Integer>> subsets(int[] nums) { dfs(nums, 0); return原创 2022-02-26 10:48:06 · 193 阅读 · 0 评论 -
LeetCode56:合并区间
public class Solution { public int[][] merge(int[][] intervals) { if (intervals.length == 0) { return new int[0][2]; } // 根据第一个数字排序 Arrays.sort(intervals, new Comparator<int[]>() { @Override原创 2022-02-19 22:20:18 · 496 阅读 · 0 评论 -
LeetCode54:螺旋矩阵
class Solution { public List<Integer> spiralOrder(int[][] matrix) { List<Integer> order = new ArrayList<>(); int rows = matrix.length; // 行数 int columns = matrix[0].length; // 列数 int left = 0, right = c原创 2022-02-13 11:57:46 · 369 阅读 · 0 评论 -
LeetCode33:搜索旋转排序数组
思路 变种二分查找:找到有序的半个数组,如果target比有序数组第一个数大,比有序数组最后一个数小,则在有序数组里找,否则在另一半无序数组里找class Solution { public int search(int[] nums, int target) { return search(nums, target, 0, nums.length-1); } public int search(int[] nums, int target, int begin原创 2021-01-14 23:01:51 · 219 阅读 · 1 评论 -
Java 快速排序
import java.util.Arrays;public class Qsort { public static void main(String[] args) { int[] array = {7,3,12,4,10}; qsort(array, 0, array.length-1); System.out.println(Arrays.toString(array)); } public static void qsor原创 2021-01-10 22:46:41 · 92 阅读 · 0 评论 -
递增数组中绝对值最小的数
求一个递增的整数数组中,绝对值最小的数。(数组中可以有正负整数和0,要求复杂度 O(logN))思路:有序数组,用二分查找。如果一个数大于零,那么要找的数在他左边或者是他本身。如果一个数小于零,那么要找的数在他右边或者是他本身。递归查找。public class Solution { public static int findMinAbsValue(int[] array, int start, int end){ int len = end - start + 1;原创 2021-01-06 19:16:35 · 229 阅读 · 0 评论 -
LeetCode704:二分查找
class Solution { public int search(int[] nums, int target) { return search(nums,target,0,nums.length-1); } public int search(int[] nums,int target, int start, int end){ if (start == end || start == end - 1){ if (nu原创 2021-01-02 10:55:02 · 106 阅读 · 0 评论 -
LeetCode110:平衡二叉树
简单思路,时间复杂度:O(n2n^2n2)class Solution { public boolean isBalanced(TreeNode node){ if (node == null){ return true; } int leftH = getHeight(node.left); int rightH = getHeight(node.right); return isBalanc原创 2020-12-30 19:25:54 · 94 阅读 · 0 评论 -
Leetcode1:两数之和
Leetcode1:两数之和思路:哈希表存储class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < nums.length; i++) { if (map.containsKey(t原创 2020-12-17 19:26:36 · 138 阅读 · 1 评论 -
Leetcode3:无重复字符的最长子串
Leetcode3,无重复字符的最长子串思路:滑动窗口class Solution { public int lengthOfLongestSubstring(String s) { if (s == null || s.length() ==0){ return 0; } if (s.length() == 1){ return 1; } int l = s.leng原创 2020-12-17 19:12:22 · 96 阅读 · 0 评论