
算法
深度挖掘各种算法原理和思想
AI媛
百米冲刺,进步提速。
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
求3的幂问题
class Solution { public boolean isPowerOfThree(int n) { if (n > 1) while (n % 3 == 0) n /= 3; return n == 1; }}原创 2022-01-28 12:35:49 · 494 阅读 · 0 评论 -
缺失数字问题
class Solution { public int missingNumber(int[] nums) { int length = nums.length; int sum = (0 + length) * (length + 1) / 2; for (int i = 0; i < length; i++) sum -= nums[i]; return sum; }}原创 2022-01-28 12:33:39 · 457 阅读 · 0 评论 -
计算质数问题
class Solution { public int countPrimes(int n) { int[] isPrime = new int[n]; Arrays.fill(isPrime, 1); int ans = 0; for (int i = 2; i < n; ++i) { if (isPrime[i] == 1) { ans += 1; .原创 2022-01-28 12:30:46 · 419 阅读 · 0 评论 -
位1的个数
public class Solution { // you need to treat n as an unsigned value public int hammingWeight(int n) { int count = 0; while (n != 0) { count += n & 1; n = n >>> 1; } return count; }}原创 2022-01-28 12:06:58 · 969 阅读 · 0 评论 -
颠倒二进制位
public class Solution { // you need treat n as an unsigned value public int reverseBits(int n) { int res = 0; for (int i = 0; i < 32; i++) { //res先往左移一位,把最后一个位置空出来, //用来存放n的最后一位数字 res <<.原创 2022-01-28 11:52:34 · 360 阅读 · 0 评论 -
阶乘后的零
class Solution {public int trailingZeroes(int n) { int zeroCount = 0; long currentMultiple = 5; while (n > 0) { n /= 5; zeroCount += n; } return zeroCount;}}原创 2022-01-28 11:08:22 · 382 阅读 · 0 评论 -
分数到小数
class Solution { public String fractionToDecimal(int numerator, int denominator) { long numeratorLong = (long) numerator; long denominatorLong = (long) denominator; if (numeratorLong % denominatorLong == 0) { return.原创 2022-01-27 16:03:53 · 127 阅读 · 0 评论 -
直线上最多的点数
class Solution { public int maxPoints(int[][] points) { if (points.length == 1) { return 1; } Map<Double, Integer> map = new HashMap<>(); int max = Integer.MIN_VALUE; for (int[] point1 : .原创 2022-01-27 16:00:59 · 1944 阅读 · 0 评论 -
只出现一次的数字
位运算解决这题说的是只有一个数出现了一次,其他数字都出现了2次,让我们求这个只出现一次的数字。这题使用位运算是最容易解决的,关于位运算有下面几个规律1^1=0;1^0=1;0^1=1;0^0=0;也就说0和1异或的时候相同的异或结果为0,不同的异或结果为1,根据上面的规律我们得到a^a=0;自己和自己异或等于0a^0=a;任何数字和0异或还等于他自己abc=acb;异或运算具有交换律有了这3个规律,这题就很容易解了,我们只需要把所有的数字都异或一遍,最终的结果就是我们要求的那个数字。.原创 2022-01-27 15:04:28 · 193 阅读 · 0 评论 -
课程表2问题
class Solution { // 存储有向图 List<List<Integer>> edges; // 标记每个节点的状态:0=未搜索,1=搜索中,2=已完成 int[] visited; // 用数组来模拟栈,下标 n-1 为栈底,0 为栈顶 int[] result; // 判断有向图中是否有环 boolean valid = true; // 栈下标 int index; pub.原创 2022-01-27 13:27:12 · 131 阅读 · 0 评论 -
课程表问题
class Solution { List<List<Integer>> edges; int[] visited; boolean valid = true; public boolean canFinish(int numCourses, int[][] prerequisites) { edges = new ArrayList<List<Integer>>(); for (int i .原创 2022-01-27 13:21:47 · 404 阅读 · 0 评论 -
岛屿数量问题
class Solution { void dfs(char[][] grid, int r, int c) { int nr = grid.length; int nc = grid[0].length; if (r < 0 || c < 0 || r >= nr || c >= nc || grid[r][c] == '0') { return; } grid[r.原创 2022-01-27 13:12:30 · 119 阅读 · 0 评论 -
单词接龙问题
class Solution { Map<String, Integer> wordId = new HashMap<String, Integer>(); List<List<Integer>> edge = new ArrayList<List<Integer>>(); int nodeNum = 0; public int ladderLength(String beginWord, Strin.原创 2022-01-27 13:06:32 · 266 阅读 · 0 评论 -
矩阵中的最长递增路径
class Solution { public int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; public int rows, columns; public int longestIncreasingPath(int[][] matrix) { if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { r.原创 2022-01-27 12:36:49 · 118 阅读 · 0 评论 -
零钱兑换问题
public class Solution { public int coinChange(int[] coins, int amount) { int max = amount + 1; int[] dp = new int[amount + 1]; Arrays.fill(dp, max); dp[0] = 0; for (int i = 1; i <= amount; i++) { .原创 2022-01-27 12:00:17 · 614 阅读 · 0 评论 -
最长上升子序列
class Solution { public int lengthOfLIS(int[] nums) { int len = 1, n = nums.length; if (n == 0) { return 0; } int[] d = new int[n + 1]; d[len] = nums[0]; for (int i = 1; i < n; ++i) { .原创 2022-01-27 11:08:59 · 511 阅读 · 0 评论 -
完全平方数
class Solution { public int numSquares(int n) { int[] f = new int[n + 1]; for (int i = 1; i <= n; i++) { int minn = Integer.MAX_VALUE; for (int j = 1; j * j <= i; j++) { minn = Math.min(min.原创 2022-01-25 17:14:30 · 240 阅读 · 0 评论 -
打家劫舍问题
class Solution { public int rob(int[] nums) { if(nums ==null ||nums.length==0){ return 0; } int[] left={0,0}; int[] right={nums[0],0}; for(int i=1;i<nums.length;i++){ left[0]=right[0];.原创 2022-01-25 17:11:23 · 119 阅读 · 0 评论 -
最长连续序列
class Solution { public int longestConsecutive(int[] nums) { Set<Integer> num_set = new HashSet<Integer>(); for (int num : nums) { num_set.add(num); } int longestStreak = 0; for (int nu.原创 2022-01-25 17:04:16 · 422 阅读 · 0 评论 -
二叉树中的最大路径和
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { .原创 2022-01-25 16:54:33 · 315 阅读 · 0 评论 -
至少有K个重复字符的最长子串
class Solution { public int longestSubstring(String s, int k) { if (s.length() < k) return 0; HashMap<Character, Integer> counter = new HashMap(); for (int i = 0; i < s.length(); i++) { counter.put(s.cha.原创 2022-01-25 16:39:03 · 132 阅读 · 0 评论 -
至少有K个重复字符的最长子串
class Solution { public int longestSubstring(String s, int k) { int ret = 0; int n = s.length(); for (int t = 1; t <= 26; t++) { int l = 0, r = 0; int[] cnt = new int[26]; int tot = 0; .原创 2022-01-25 12:48:52 · 185 阅读 · 0 评论 -
计算右侧小于当前元素的个数
class Solution { private int[] index; private int[] temp; private int[] tempIndex; private int[] ans; public List<Integer> countSmaller(int[] nums) { this.index = new int[nums.length]; this.temp = new int[nums.le.原创 2022-01-24 16:35:38 · 198 阅读 · 0 评论 -
【寻找重复数】
class Solution { public int findDuplicate(int[] nums) { int n = nums.length; int l = 1, r = n - 1, ans = -1; while (l <= r) { int mid = (l + r) >> 1; int cnt = 0; for (int i = 0; i &.原创 2022-01-24 16:05:59 · 157 阅读 · 0 评论 -
寻找峰值问题
class Solution { public int findPeakElement(int[] nums) { int idx = 0; for (int i = 1; i < nums.length; ++i) { if (nums[i] > nums[idx]) { idx = i; } } return idx; }}原创 2022-01-24 15:46:27 · 218 阅读 · 0 评论 -
最大数问题
class Solution { public String largestNumber(int[] nums) { if (nums.length == 0 || nums == null) return null; int n = nums.length; Integer[] NUMS = new Integer[n]; int i = 0; for (int num : nums) {.原创 2022-01-21 13:13:59 · 243 阅读 · 0 评论 -
天际线问题
class Solution { public List<List<Integer>> getSkyline(int[][] buildings) { PriorityQueue<int[]> pq = new PriorityQueue<int[]>((a, b) -> b[1] - a[1]); List<Integer> boundaries = new ArrayList<Integer.原创 2022-01-20 13:20:03 · 134 阅读 · 0 评论 -
二叉树的序列化与反序列化
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * }*/public class Codec { public String serialize(TreeNode root) { return.原创 2022-01-20 12:42:31 · 84 阅读 · 0 评论 -
二叉树的最近公共祖先
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { Map<Integer, TreeNode> parent = new HashMap<Integ.原创 2022-01-20 12:30:13 · 341 阅读 · 0 评论 -
二叉搜索树中第K小的元素
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { .原创 2022-01-20 12:14:45 · 325 阅读 · 0 评论 -
常数时间插入、删除和获取随机元素
class RandomizedSet { Map<Integer, Integer> dict; List<Integer> list; Random rand = new Random(); /** Initialize your data structure here. */ public RandomizedSet() { dict = new HashMap(); list = new ArrayList(); } /** .原创 2022-01-19 20:18:40 · 100 阅读 · 0 评论 -
四数相加2
class Solution { public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { int res = 0; HashMap<Integer, Integer> map = new HashMap<>(); for (int i : nums1) { for (int j : nums2) { map.p.原创 2022-01-19 20:15:54 · 286 阅读 · 0 评论 -
Excel表列序号
class Solution { public int titleToNumber(String columnTitle) { int number = 0; int multiple = 1; for (int i = columnTitle.length() - 1; i >= 0; i--) { int k = columnTitle.charAt(i) - 'A' + 1; number .原创 2022-01-19 20:05:07 · 257 阅读 · 0 评论 -
奇偶链表问题
/** * 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-01-19 19:10:28 · 107 阅读 · 0 评论 -
回文链表问题
/** * 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-01-18 13:52:20 · 101 阅读 · 0 评论 -
反转链表问题
/** * 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-01-18 13:44:37 · 176 阅读 · 0 评论 -
相交链表问题
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public ListNode getIntersectionNo.原创 2022-01-18 13:27:17 · 87 阅读 · 0 评论 -
环形链表问题
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public boolean hasCycle(ListNode head) {.原创 2022-01-10 12:40:25 · 171 阅读 · 0 评论 -
复制带随机指针的链表
class Solution { Map<Node, Node> cachedNode = new HashMap<Node, Node>(); public Node copyRandomList(Node head) { if (head == null) { return null; } if (!cachedNode.containsKey(head)) { N.原创 2022-01-09 22:28:17 · 174 阅读 · 0 评论 -
基本计算器2
class Solution {public int calculate(String s) { //首尾增加 ’#‘ 作为标志 String s_temp = "#" + s.replaceAll(" ","") + "#"; Stack<Integer> stack_num = new Stack<>(); Stack<Character> stack_operator = new Stack<.原创 2022-01-05 23:42:18 · 447 阅读 · 0 评论