【leetcode 665. 非递减数列 c++】教程文章相关的互联网学习教程文章

【LeetCode】996. Number of Squareful Arrays 解题报告(C++)【代码】

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/目录题目描述题目大意解题方法回溯法日期 题目地址:https://leetcode.com/problems/number-of-squareful-arrays/ 题目描述 Given an array A of non-negative integers, the array is squareful if for every pair of adjacent elements, their sum is a perfect square. Return the number of permutations of A that are squareful. Two permutations A1...

C++Leetcode292:Nim游戏【代码】

前言 现在做题上瘾了,拿到题目瞬间做出来,也没有那种调试一直不对的现象,一次性通过,所以时间也能够节省太多。信心大增~ 每天不把博客刷满,就难受! 题目 你和你的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头。 拿掉最后一块石头的人就是获胜者。你作为先手。 你们是聪明人,每一步都是最优解。 编写一个函数,来判断你是否可以在给定石头数量的情况下赢得游戏。 示例: 输入: 4 输出: fals...

Leetcode刷题2-977. 有序数组的平方(C++)【代码】

题目来源:链接: [https://leetcode-cn.com/problems/squares-of-a-sorted-array/comments/]. 977. 有序数组的平方1.问题描述2.解决方案3.代码 1.问题描述 给定一个按非递减顺序排序的整数数组 A,返回每个数字的平方组成的新数组,要求也按非递减顺序排序。 示例 1: 输入:[-4,-1,0,3,10] 输出:[0,1,9,16,100]示例 2: 输入:[-7,-3,2,3,11] 输出:[4,9,9,49,121]提示: 1. 1<= A.length <= 10000 2. -10000 <= A[i] <= 10000 3. A...

LeetCode82. Remove Duplicates from Sorted List II(C++)【代码】

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Example 1:Input: 1->2->3->3->4->4->5 Output: 1->2->5Example 2:Input: 1->1->1->2->3 Output: 2->3 解题思路:双指针法/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/ class Solutio...

LeetCode83. Remove Duplicates from Sorted List(C++)【代码】

Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1:Input: 1->1->2 Output: 1->2Example 2:Input: 1->1->2->3->3 Output: 1->2->3 解题思路:双指针法/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/ class Solution { public:ListNode* deleteDuplicates(ListNode* h...

leetcode7_C++整数反转【代码】

小弟不才,有错误请指出,谢谢。 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 注意: 假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [?231, 231 ? 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。 整数反转: 思路:其实和回文数的思路是差不多。一个数x,把它进行生...

LeetCode 485. 最大连续1的个数(C、C++、python)【代码】

给定一个二进制数组, 计算其中最大连续1的个数。 示例 1:输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.注意: 输入的数组只包含 0 和1。 输入数组的长度是正整数,且不超过 10,000。 Cint findMaxConsecutiveOnes(int* nums, int numsSize) {int n=numsSize;int count=0;int res=0;for(int i=0;i<n;i++){if(1==nums[i]){count+=1;}else{res=res>count?res:count;count=0;}}retu...

LeetCode102 树·二叉树的层次遍历(C++)【代码】

题目描述: 给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。 例如:给定二叉树: [3,9,20,null,null,15,7],3/ 9 20/ 15 7返回其层次遍历结果: [[3],[9,20],[15,7] ]

leetcode 315 计算右侧小于当前元素个数 c++

class Solution { public:vector<int> countSmaller(vector<int>& nums) {vector<pair<int,int>>vec;vector<int>count;for(int i=0;i<nums.size();i++){vec.push_back(make_pair(nums[i],i));count.push_back(0);}merge_sort(vec,count);return count;} private:void merge_sort_two_vec(vector<pair<int,int>>&sub_vec1,vector<pair<int,int>>&sub_vec2,vector<pair<int,int>>&vec,vector<int>&count){int i=0;int j=0;while(i<su...

Leetcode 110. 平衡二叉树 C++【代码】

题目描述: 给定一个二叉树,判断它是否是高度平衡的二叉树。 本题中,一棵高度平衡二叉树定义为:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。示例 1: 给定二叉树 [3,9,20,null,null,15,7] 3/ 9 20/ 15 7 返回 true 。示例 2: 给定二叉树 [1,2,2,3,3,null,null,4,4] 1/ 2 2/ 3 3/ 4 4返回 false 。 分析: 既然平衡二叉树的定义是任意一颗树的左右子树的高度差不超过1,那么做这一道题目...

Leetcode 加一(c++)【代码】

给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。 你可以假设除了整数 0 之外,这个整数不会以零开头。 示例 2: 输入: [4,3,2,1] 输出: [4,3,2,2] 解释: 输入数组表示数字 4321。 class Solution { public:vector<int> plusOne(vector<int>& digits) {int len = digits.size() - 1; for(;len>0 && digits[len]==9 ;--len){digtis[len]=0;}if (l...

【LeetCode】187. 重复的DNA序列 结题报告 (C++)

原题地址:https://leetcode-cn.com/problems/repeated-dna-sequences/description/ 题目描述: 所有 DNA 由一系列缩写为 A,C,G 和 T 的核苷酸组成,例如:“ACGAATTCCG”。在研究 DNA 时,识别 DNA 中的重复序列有时会对研究非常有帮助。 编写一个函数来查找 DNA 分子中所有出现超多一次的10个字母长的序列(子串)。 示例: 输入: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT" 输出: ["AAAAACCCCC", "CCCCCAAAAA"] 解题方案: 使用...

LeetCode 628. 三个数的最大乘积(C、C++、python)【代码】

给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积。 示例 1:输入: [1,2,3] 输出: 6示例 2:输入: [1,2,3,4] 输出: 24注意: 给定的整型数组长度范围是[3,104],数组中所有的元素范围是[-1000, 1000]。 输入的数组中任意三个数的乘积不会超出32位有符号整数的范围。 Cint maximumProduct(int* nums, int numsSize) {if(numsSize<3){return 0;}sort(nums,0,numsSize-1);int res1=nums[numsSize-1]*nums[numsSiz...

【LeetCode】162. 寻找峰值 结题报告 (C++)

原题地址:https://leetcode-cn.com/problems/find-peak-element/description/ 题目描述: 峰值元素是指其值大于左右相邻值的元素。 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引。 数组可能包含多个峰值,在这种情况下,返回任何一个峰值所在位置即可。 你可以假设 nums[-1] = nums[n] = -∞。 示例 1: 输入: nums = [1,2,3,1] 输出: 2 解释: 3 是峰值元素,你的函数应该返回其索引 2。 示例 2: ...

【LeetCode】148. 排序链表 结题报告 (C++)

原题地址:https://leetcode-cn.com/problems/sort-list/description/ 题目描述: 在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。 示例 1: 输入: 4->2->1->3 输出: 1->2->3->4 示例 2: 输入: -1->5->3->4->0 输出: -1->0->3->4->5 解题方案: 本题对时间复杂度有要求:在 O(n log n) 时间复杂度和常数级空间复杂度下。因此平常使用的插入法和交换法都不能使用,这里就会想到归并法。归并之前,需要对链表进行对...