- 博客(131)
- 资源 (2)
- 收藏
- 关注
原创 荷兰国旗问题
package com.nanjia.chapter1;public class NetherlandsFlag { //荷兰国旗代码逻辑 public static void partition(int[] arr, int left, int right, int point) { int less = left - 1; int more = right + 1; while (left < more) {
2021-05-07 11:28:48
209
原创 桶排序
桶排序桶排序是一种非比较的排序,下面的代码注释里面描述了桶排序的思路package com.nanjia.chapter1;import java.util.Arrays;public class BucketSort { //计数排序(桶排序) public static void bucketSort(int[] arr) { if (arr == null || arr.length < 2) return; /
2021-05-06 17:44:03
197
原创 插入排序
插入排序代码逻辑以及如何使用对数器进行验证package com.nanjia.chapter1;import java.util.Arrays;public class InsertSort { //插排代码逻辑 public static void insertSort(int[] arr) { if (arr == null || arr.length < 2) return; for (int i = 1; i
2021-05-06 11:17:53
190
原创 leetcode 1002. 查找常用字符
class Solution { public List<String> commonChars(String[] A) { //存放结果list List<String> list = new LinkedList<>(); //开一个HashMap类型的数组 HashMap[] map = new HashMap[A.length]; //把输入的字符串数组的值的每个字符出现的个数都给计
2021-04-06 10:03:09
172
原创 leetcode 面试题 03.04. 化栈为队
class MyQueue { Stack<Integer> stack1 = new Stack<>(); /** Initialize your data structure here. */ public MyQueue() { } /** Push element x to the back of queue. */ public void push(int x) { stack1.p
2021-04-04 21:52:56
118
原创 leetcode 1351. 统计有序矩阵中的负数
//使用二分查找 二分查找一定要注意细节class Solution { public int countNegatives(int[][] grid) { int result = 0; for (int i = 0; i < grid.length; i++) { int start = 0; int end = grid[i].length - 1; int record = -1;
2021-04-04 19:50:42
90
原创 leetcode 804. 唯一摩尔斯密码词
class Solution { public int uniqueMorseRepresentations(String[] words) { String[] a = new String[words.length]; for (int i = 0; i < words.length; i++) a[i] = ""; for (int i = 0; i < words.length; i++) {
2021-04-04 15:25:13
93
原创 leetcode 108. 将有序数组转换为二叉搜索树
/** * 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) { *
2021-04-02 17:30:16
70
原创 leetcode 700. 二叉搜索树中的搜索
/** * 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) { *
2021-04-02 16:17:43
84
原创 leetcode 1266. 访问所有点的最小时间
class Solution { public int minTimeToVisitAllPoints(int[][] points) { int sum = 0; //思路是这样的:两个节点加入 for (int i = 0; i < points.length - 1; i++) { int left = Math.abs(points[i + 1][0] - points[i][0]); in
2021-03-31 11:14:41
105
原创 leetcode 589. N 叉树的前序遍历
/*// Definition for a Node.class Node { public int val; public List<Node> children; public Node() {} public Node(int _val) { val = _val; } public Node(int _val, List<Node> _children) { val = _val;
2021-03-26 10:52:09
94
原创 leetcode 590. N 叉树的后序遍历
/*// Definition for a Node.class Node { public int val; public List<Node> children; public Node() {} public Node(int _val) { val = _val; } public Node(int _val, List<Node> _children) { val = _val;
2021-03-26 10:45:43
77
原创 leetcode 617. 合并二叉树
/** * 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) { *
2021-03-26 09:33:54
85
原创 leetcode 461. 汉明距离
class Solution { public int hammingDistance(int x, int y) { int num = x > y ? x : y; int length = 0; int result = 0; while (num > 0) { num = num >> 1; length++; } for (
2021-03-25 17:08:37
83
原创 leetcode 1768. 交替合并字符串
class Solution { public String mergeAlternately(String word1, String word2) { String result = ""; int end = word1.length() > word2.length() ? word1.length() : word2.length(); for (int i = 0; i < end; i++) { if
2021-03-24 17:52:36
102
原创 leetcode 1221. 分割平衡字符串
class Solution { public int balancedStringSplit(String s) { int record1 = 0; int record2 = 0; int result = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 'R') record1++; if (s.cha
2021-03-24 15:21:52
97
原创 leetcode LCP 17. 速算机器人
class Solution { public int calculate(String s) { int x = 1; int y = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 'A') x = 2 * x + y; if (s.charAt(i) == 'B') y = 2 * y + x; }
2021-03-24 15:06:07
153
原创 leetcode 1588. 所有奇数长度子数组的和
class Solution { public int sumOddLengthSubarrays(int[] arr) { int result = 0; for (int i = 0; i < arr.length; i++) { int record = arr[i]; result += arr[i]; int j = i + 2; while (j <
2021-03-24 14:59:10
66
原创 leetcode 1450. 在既定时间做作业的学生人数
class Solution { public int busyStudent(int[] startTime, int[] endTime, int queryTime) { int result = 0; for (int i = 0; i < startTime.length; i++) { if (queryTime >= startTime[i] && queryTime <= endTime[i])
2021-03-24 10:38:15
78
原创 leetcode 1342. 将数字变成 0 的操作次数
class Solution { public int numberOfSteps (int num) { if (num == 0) return 0; if (num % 2 == 0) return numberOfSteps(num / 2) + 1; else return numberOfSteps(num - 1) + 1; }}
2021-03-24 09:11:09
86
原创 leetcode 1720. 解码异或后的数组
class Solution { public int[] decode(int[] encoded, int first) { int[] result = new int[encoded.length + 1]; result[0] = first; for (int i = 0; i < encoded.length; i++) { first ^= encoded[i]; result[i
2021-03-23 15:15:15
75
原创 leetcode 1773. 统计匹配检索规则的物品数量
class Solution { public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) { int result = 0; if (ruleKey.equals("type")) { for (int i = 0; i < items.size(); i++) {
2021-03-23 15:00:58
121
原创 leetcode 1431. 拥有最多糖果的孩子
class Solution { public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) { int max = 0; List<Boolean> result = new ArrayList<>(); for (int i = 0; i < candies.length; i++) { max = ma
2021-03-23 14:35:26
77
原创 leetcode 258. 各位相加
class Solution { public int addDigits(int num) { if(num == 0) return 0; if (num % 9 == 0) return 9; else return num % 9; }}
2021-03-23 11:26:22
65
原创 leetcode 389. 找不同
class Solution { public char findTheDifference(String s, String t) { List<Character> list = new ArrayList<>(); for (int i = 0; i < t.length(); i++) { list.add(t.charAt(i)); } for (int i = 0; i
2021-03-23 10:37:27
82
原创 leetcode 1688. 比赛中的配对次数
class Solution { public int numberOfMatches(int n) { if (n == 1) return 0; //偶数情况处理 if (n % 2 == 0) { return numberOfMatches(n / 2) + n / 2; //奇数情况处理 } else { return numberOfM
2021-03-23 10:09:54
137
原创 leetcode 1748. 唯一元素的和
class Solution { public int sumOfUnique(int[] nums) { int result = 0; for (int i = 0; i < nums.length; i++) { boolean flag = false; for (int j = 0; j < nums.length; j++) { if (i == j) contin
2021-03-23 09:59:06
69
原创 leetcode 1365. 有多少小于当前数字的数字
class Solution { public int[] smallerNumbersThanCurrent(int[] nums) { int[] result = new int[nums.length]; for (int i = 0; i < nums.length; i++) { int record = 0; for (int j = 0; j < nums.length; j++) {
2021-03-23 09:29:30
112
原创 leetcode 1572. 矩阵对角线元素的和
class Solution { public int diagonalSum(int[][] mat) { int sum = 0; for (int i = 0; i < mat.length; i++) { //一是一个特殊情况,处理 if (mat.length == 1) { sum += mat[i][i]; break;
2021-03-22 23:40:09
152
原创 leetcode 1732. 找到最高海拔
class Solution { public int largestAltitude(int[] gain) { int start = 0; //海拔从0开始 int record = 0; for (int i = 0; i < gain.length; i++) { start += gain[i]; //假如海拔高,就计入 record = reco
2021-03-22 23:20:10
95
原创 leetcode 剑指 Offer 06. 从尾到头打印链表
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public int[] reversePrint(ListNode head) { Stack<Integer> stack = new
2021-03-22 21:49:59
67
原创 leetcode 867. 转置矩阵
class Solution { public int[][] transpose(int[][] matrix) { int[][] array = new int[matrix[0].length][matrix.length]; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) {
2021-03-22 21:20:55
65
原创 leetcode 1290. 二进制链表转整数
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public int getDecimalValue(ListNode head) { String str = ""; while
2021-03-22 21:12:50
120
原创 leetcode 832. 翻转图像
class Solution { public int[][] flipAndInvertImage(int[][] image) { for (int i = 0; i < image.length; i++) { int t; for (int j = 0; j < image[i].length / 2; j++) { t = image[i][j];
2021-03-22 17:23:18
114
原创 leetcode 226. 翻转二叉树
/** * 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) { *
2021-03-22 16:29:22
73
原创 leetcode 1295. 统计位数为偶数的数字
class Solution { public int findNumbers(int[] nums) { int reult = 0; for (int i = 0; i < nums.length; i++) { int record = 0; while (nums[i] > 0) { record++; nums[i] /= 10;
2021-03-22 15:40:04
264
原创 leetcode 1684. 统计一致字符串的数目
class Solution { public int countConsistentStrings(String allowed, String[] words) { HashSet<Character> set = new HashSet<>(); //把字符串的值先存到set里面 for (int i = 0; i < allowed.length(); i++) { set.add(allo
2021-03-22 15:11:05
112
原创 leetcode 1662. 检查两个字符串数组是否相等
class Solution { public boolean arrayStringsAreEqual(String[] word1, String[] word2) { String str = ""; String str1 = ""; int next1 = 0; int next2 = 0; while (next1 < word1.length) { str += word1[n
2021-03-21 21:54:43
125
原创 leetcode LCP 06. 拿硬币
class Solution { public int minCount(int[] coins) { int result = 0; for (int i = 0; i < coins.length; i++) { //1的时候,是一种特殊情况 if (coins[i] == 1) { result += 1; continue;
2021-03-21 20:15:39
149
原创 leetcode 1486. 数组异或操作
class Solution { public int xorOperation(int n, int start) { int sum = 0; int result = 0; for (int i = 0; i < n; i++) { //带入式子 sum = start + 2 * i; //保存结果,并异或计算 result ^= sum;
2021-03-21 20:00:43
134
FlowersImage_Mask-R-CNN-DataSet.rar
2020-07-22
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人