- 博客(158)
- 资源 (1)
- 收藏
- 关注
原创 求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
475
原创 缺失数字问题
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
424
原创 计算质数问题
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
398
原创 位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
953
原创 颠倒二进制位
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
350
原创 阶乘后的零
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
372
原创 分数到小数
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
112
原创 直线上最多的点数
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
1924
原创 只出现一次的数字
位运算解决这题说的是只有一个数出现了一次,其他数字都出现了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
180
原创 课程表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
114
原创 课程表问题
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
388
原创 岛屿数量问题
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
110
原创 单词接龙问题
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
250
原创 矩阵中的最长递增路径
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
107
原创 零钱兑换问题
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
603
原创 最长上升子序列
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
499
原创 完全平方数
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
229
原创 打家劫舍问题
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
107
原创 最长连续序列
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
409
原创 二叉树中的最大路径和
/** * 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
302
原创 至少有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
116
原创 至少有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
173
原创 计算右侧小于当前元素的个数
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
185
原创 【寻找重复数】
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
146
原创 寻找峰值问题
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
208
原创 最大数问题
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
232
原创 天际线问题
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
120
原创 二叉树的序列化与反序列化
/** * 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
74
原创 二叉树的最近公共祖先
/** * 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
332
原创 二叉搜索树中第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
313
原创 常数时间插入、删除和获取随机元素
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
86
原创 四数相加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
268
原创 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
247
原创 奇偶链表问题
/** * 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
97
原创 回文链表问题
/** * 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
86
原创 反转链表问题
/** * 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
165
原创 相交链表问题
/** * 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
78
原创 环形链表问题
/** * 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
160
原创 复制带随机指针的链表
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
164
原创 基本计算器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
434
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人