随笔分类 - LeetCode
摘要:/** * 二分查找思想 */ class Solution { public int searchInsert(int[] nums, int target) { int len = nums.length; //判空 if(len == 0) { return 0; } //如果最后一个元素都没
阅读全文
摘要:class Solution { public int numIslands(char[][] grid) { //统计岛屿的数量 int count = 0; for(int i = 0;i < grid.length;i++){ for(int j = 0;j < grid[0].length;
阅读全文
摘要://暴力法 class Solution { public int maxSubArray(int[] nums) { //定义一个当前子序列的和,将初值 设为nums【0】 int sum = nums[0]; //定义一个子序列的全局最大值 int max = sum; //从第二个元素开始 f
阅读全文
摘要:class Solution { //对于 [a, b, c, d],如果有 a <= b <= c <= d ,那么最大收益为 d - a。而 d - a = (d - c) + (c - b) + (b - a) , //因此当访问到一个 prices[i] 且 prices[i] - pric
阅读全文
摘要://暴力法 class Solution { public int maxProfit(int[] prices) { //边界判断 if(prices.length < 2) { return 0; } //定义股票的最大收益 int maxprofit = 0; //定义最低价买入 int mi
阅读全文
摘要:class Solution { public void sortColors(int[] nums) { // [0,zero) = 0 ; [zero,i) = 1; [two,nums.length - 1] = 2 //保证循环开始时[0,zero)为空,所以设置zero 为 -1,遍历时先
阅读全文
摘要:class Solution { public int[] topKFrequent(int[] nums, int k) { //使用HashMap统计每个元素出现的次数,元素为键,元素出现的频次为值 HashMap<Integer,Integer> map = new HashMap<>();
阅读全文
摘要://1.使用最小堆,或最大堆 // 根据 k 的不同,选最大堆和最小堆,目的是让堆中的元素更小 class Solution { public int findKthLargest(int[] nums, int k) { PriorityQueue<Integer> queue = new Pri
阅读全文
摘要:/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ //快慢指
阅读全文
摘要://动态规划,198的升级版 //在于怎样处理 首尾房子,它们之中只能抢一个 //Math(抢首,抢尾) class Solution { public int rob(int[] nums) { //没房子 if(nums.length == 0) return 0; //一间房子,没得选,必抢
阅读全文
摘要://动态规划 class Solution { public int rob(int[] nums) { int pre = 0; int cur = 0; for(int i = 0;i < nums.length; i++){ // 循环开始时,cur 代表 dp[k - 1]; pre 代表
阅读全文
摘要:class Solution { public int[] productExceptSelf(int[] nums) { int[] res = new int[nums.length]; //left 为该数左边的乘积 int left = 1; //right 为该数右边的乘积 int rig
阅读全文
摘要://摩尔投票法 //1.如果候选人不是maj 则 maj,会和其他非候选人一起反对 会反对候选人,所以候选人一定会下台(maj==0时发生换届选举) //2.如果候选人是maj , 则maj 会支持自己,其他候选人会反对,同样因为maj 票数超过一半,所以maj 一定会成功当选 class Solu
阅读全文
摘要:// class Solution { // public int[] countBits(int num) { // int[] res = new int[num + 1]; // res[0] = 0; // for(int i = 1;i<res.length;i++){ // if(i %
阅读全文
摘要:class Solution { public boolean isPowerOfTwo(int n) { return n > 0 && (n & (n-1)) == 0; } } 这里引用评论区Kico大神的解释。
阅读全文
摘要://异或运算 //1、交换律:a ^ b ^ c <=> a ^ c ^ b //2、任何数于0异或为任何数 0 ^ n => n //3、相同的数异或为0: n ^ n => 0 class Solution { public int singleNumber(int[] nums) { int
阅读全文
摘要://二分法查找 class Solution { public int findDuplicate(int[] nums) { //定义数组左边界的指针 left,右边界指针 right int left = 1; int right = nums.length - 1; while(left <
阅读全文
摘要://采用数组桶 class Solution { public int[] findErrorNums(int[] nums) { //定义一个新数组来统计nums数组中元素出现的次数 int[] count = new int[nums.length + 1]; //统计每个元素出现的次数 for
阅读全文
摘要:/* 左下角的元素是这一行中最小的元素,同时又是这一列中最大的元素。比较左下角元素和目标: 1.若左下角元素等于目标,则找到 2.若左下角元素大于目标,则目标不可能存在于当前矩阵的最后一行,问题规模可以减小为在去掉最后一行的子矩阵中寻找目标 3.若左下角元素小于目标,则目标不可能存在于当前矩阵的第一
阅读全文
摘要:class Solution { public int findMaxConsecutiveOnes(int[] nums) { //定义俩个计数器,max为最大的1的数量,cur为当前1的数量 int max = 0; int cur = 0; //遍历数组,如果数组中的元素为1,cur+1,为0
阅读全文

浙公网安备 33010602011771号