ICode9

精准搜索请尝试: 精确搜索
  • 18.4-sum 四数之和2022-08-08 12:03:04

    参考三数之和,相比三数之和,只是外面再套了一层循环,即两层循环内部使用双指针法。 同时要注意int 的取值范围,转换成long long,防止溢出。 #include <algorithm> #include <vector> using std::vector; class Solution { public: vector<vector<int>> fourSum(vector<int> &nums

  • Cow Picnic S2022-08-07 21:03:24

    P2853 [USACO06DEC]Cow Picnic S - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 以每一头牛为起点开始一遍dfs,每路过一个点那么当前的点nums值+1 所有点中nums值为牛的总次数的点代表可以 每次dfs时注意要将vis数组清空 // 2 4 4 // 2 // 3 // 1 2 // 1 4 // 2 3 // 3 4 // htt

  • 银河英雄传说2022-08-07 20:30:45

    P1196 [NOI2002] 银河英雄传说 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) front数组代表每个点到队头的距离,x,y之间隔了几个舰队答案为abs(front[x]-front[y])-1; nums数组代表当前队伍一共有几个舰队 合并操作只需要将x,y对应祖先也就是fa(x),fa(y)也就是它们各自的队头front nums

  • 力扣-300-最长递增子序列2022-08-07 17:02:01

    直达链接 想到了连续子数组的最大和 自己想 我本来想倒着推,有点像mari和shiny,但是不对 class Solution { public: int lengthOfLIS(vector<int>& nums) { int length = nums.size(); if (length < 2) return 1; vector<int> dp(length); dp[length - 1] = 1; nums[le

  • LeetCode 798. Smallest Rotation with Highest Score2022-08-07 15:03:57

    原题链接在这里:https://leetcode.com/problems/smallest-rotation-with-highest-score/ 题目: You are given an array nums. You can rotate it by a non-negative integer k so that the array becomes [nums[k], nums[k + 1], ... nums[nums.length - 1], nums[0], nums[1],

  • LeetCode 90 Subsets II 回溯2022-08-07 05:00:09

    Given an integer array nums that may contain duplicates, return all possible subsets (the power set). The solution set must not contain duplicate subsets. Return the solution in any order. Solution 如何处理相同的子集:先把 \(vector\) \(sort\) 一下,然后在 \(ans\)

  • 1480. 一维数组的动态和2022-08-06 18:28:52

    1.题目: 给你一个数组 nums 。数组「动态和」的计算公式为:runningSum[i] = sum(nums[0]…nums[i]) 。 请返回 nums 的动态和。   示例 1: 输入:nums = [1,2,3,4]输出:[1,3,6,10]解释:动态和计算过程为 [1, 1+2, 1+2+3, 1+2+3+4] 。示例 2: 输入:nums = [1,1,1,1,1]输出:[1,2,3,4,5]解释:动

  • 剑指 Offer 03. 数组中重复的数字2022-08-06 18:27:23

    1.题目: 找出数组中重复的数字。 在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。 示例 1: 输入:[2, 3, 1, 0, 2, 5, 3]输出:2 或 3   限制: 2 <= n <= 10

  • LeetCode 1838. Frequency of the Most Frequent Element2022-08-05 06:31:06

    原题链接在这里:https://leetcode.com/problems/frequency-of-the-most-frequent-element/ 题目: The frequency of an element is the number of times it occurs in an array. You are given an integer array nums and an integer k. In one operation, you can choose an i

  • 算法:数组中数字出现的次数2022-08-04 21:32:08

    问题 一个整型数组 nums 里除两个数字之外,其他数字都出现了两次。请写程序找出这两个只出现一次的数字。要求时间复杂度是O(n),空间复杂度是O(1)。 解决 //1、利用hashset的特性进行判断 class Solution { public int[] singleNumbers(int[] nums) { int len1=nums

  • 1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit2022-08-04 09:34:29

    原题链接在这里:https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/ 题目: Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute di

  • LeetCode 239 Sliding Window Maximum 单调队列 [Hard]2022-08-03 02:31:06

    You are given an array of integers nums, there is a sliding window of size \(k\) which is moving from the very left of the array to the very right. You can only see the \(k\) numbers in the window. Each time the sliding window moves right by one posit

  • #Leetcode 912 Sort an Array 快排 改进2022-08-03 00:35:03

    改进版快排,pivot 不再是左边第一个元素,而是正中间元素(或者随机)。 有一个比较坑的地方就是,在每一趟双指针完成所有交换后,需要判断 pivot 需不需要被交换。 比如 test case 1 2 4 3,第一趟开始时 pivot 是 2, 先动右边的指针 j, 找到第一个比 2 小的数也就是 1。此时 1 不应该和 2

  • Java二分查找:给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target  ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -12022-08-02 23:02:54

    给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target  ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。 示例 1: 输入: nums = [-1,0,3,5,9,12], target = 9 输出: 4 解释: 9 出现在 nums 中并且下标为 4 利用二分查找思想 class Solutio

  • leetcode.81. 搜索旋转排序数组 II2022-08-02 20:35:21

    已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同。 在传递给函数之前,nums 在预先未知的某个下标 k(0 <= k < nums.length)上进行了 旋转 ,使数组变为 [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]](下标 从 0 开始 计数)。例如, [0,1,2,4,4

  • 15. 三数之和2022-08-02 19:01:55

    15. 三数之和 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。 注意:答案中不可以包含重复的三元组。   示例 1: 输入:nums = [-1,0,1,2,-1,-4] 输出:[[-1,-1,2],[-1,0,1]] 示例 2: 输入:nums =

  • 力扣算法JS LC [96. 不同的二叉搜索树] LC [416. 分割等和子集]2022-08-01 22:37:37

    ​  LC 96. 不同的二叉搜索树 给你一个整数 n ,求恰由 n 个节点组成且节点值从 1 到 n 互不相同的 二叉搜索树 有多少种?返回满足题意的二叉搜索树的种数。 示例 1: ​ 编辑 输入:n = 3 输出:5 示例 2: 输入:n = 1 输出:1 解题思路:遍历整数 n ,让每个数都去做一次根节点,然后求出每个

  • LeetCode 713 Subarray Product Less Than K 滑动窗口2022-08-01 18:35:31

    Given an array of integers nums and an integer k, return the number of contiguous subarrays where the product of all the elements in the subarray is strictly less than k. Solution 滑动窗口的思想。不断增大右端点 \(r\), 当乘积 \(pd\ge k\) 的时候,缩小左端点 \(l\),

  • PAT (Advanced Level) Practice 1008 Elevator 分数 20 Python 解法2022-08-01 14:33:58

    题目 The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 second

  • 力扣53.最大子数和2022-08-01 12:32:01

    三种情况:   1.左半部分(leftsum)最大(与右边不连续,不一定包括mid)   2.右半部分(rightsum)最大(与左边不连续,不一定包括mid+1)   3.左右部分联合最大(左右两边连续,包括mid)   1和2是前面返回的(与另一边不连续 !!!,左半部分不一定是包含mid 以mid结尾的,右半部分也不一定包含mid+1,从mid+1

  • LeetCode/二分法综合2022-07-30 21:00:13

    1. 寻找两个正序数组的中位数 2. 两数相除 3. 快速幂 4. 搜索旋转排序数组 5. 数组中的逆序对 6. 在排序数组中查找元素的第一个和最后一个位置 class Solution { public: vector<int> searchRange(vector<int>& nums, int target) { return {find(nums,target,true),

  • LeetCode 238 Product of Array Except Self 前缀积&后缀积2022-07-30 20:35:18

    Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. You must write an algorithm t

  • LeetCode 108 Convert Sorted Array to Binary Search Tree DFS2022-07-29 06:31:26

    Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree. A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more tha

  • 第一题,Two Sum2022-07-28 15:03:09

    此题给定一个数组和目标target,求两个数之和等于target,返回这两个数的下标位置   Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one

  • leetcode之两数相加2022-07-28 02:31:41

    // 1.两数之和 // set 存每一位被需要的值,作为key , 值为当前位置的 index // 然后继续遍历下一位,判断下一位是否就是已经存起来的被需要的值 // 如果是,则直接返回当时被需要时那一位的索引 及 当前索引 function sum(nums, target) { let map = new Map(); for (let i

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有