摘要:56. 合并区间 一开始的想法时,用一个nums数组给各个区间[ startI , rightI ] 自增 1 ,但是这是错误的。 当遇到 [1 , 2 ] [ 3 , 4 ] , [1 , 2 ] [ 0 , 0 ] 等情况会变得很难分辨。 这道题的intervals里面各个 start 和 en
阅读全文
摘要:191. 位1的个数 给定一个正整数 n,编写一个函数,获取一个正整数的二进制形式并返回其二进制表达式中设置位的个数。1 <= n <= 2^32-1 法一:暴力解: class Solution { public: int hammingWeight(uint32_t n) { int count
阅读全文
摘要:leetcode 2289. 使数组按非递减顺序排列 这道题远没有想象中的简单,如果用暴力常规方法,数据量大的情况下会超时 暴力解1: class Solution { public: int totalSteps(vector<int>& nums) { int size = nums.size(
阅读全文
摘要:1909. 删除一个元素使数组严格递增 题解的做法都太复杂了,我的可能好理解一些 class Solution { public: bool canBeIncreasing(vector<int>& nums) { int size = nums.size(); if(size == 2) retu
阅读全文
摘要:1450. 在既定时间做作业的学生人数 法一:差分数组 class Solution { public: int busyStudent(vector<int>& startTime, vector<int>& endTime, int queryTime) { int nums[1001] = {
阅读全文
摘要:1109. 航班预订统计 这道题使用暴力解法,如果数据比较多,first 和 second跨度比较大时会超时。比如下面这个暴力解: class Solution { public: vector<int> corpFlightBookings(vector<vector<int>>& booking
阅读全文
摘要:2099. 找到和最大的长度为 K 的子序列 特别注意,题目要求不改变原来的元素顺序 我的代码 class Solution { public: vector<int> maxSubsequence(vector<int>& nums, int k) { vector<int> temp(nums)
阅读全文
摘要:P1719 最大加权矩形 先给一个 n×n 矩阵,1 <= n <= 127。要求矩阵中最大加权矩形,即矩阵的每一个元素都有一权值,权值定义在整数集上。 从中找一矩形,矩形大小无限制,是其中包含的所有元素的和最大 。矩阵的每个元素属于 [-127,127], 例如 0 –2 –7 0 9 2 –6
阅读全文
摘要:53. 最大子数组和 给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 子数组是数组中的一个连续部分。 法一: 1.假如全是负数,那就是找最大值即可,因为负数肯定越加越大。 2.如果有正数,则肯定从正数开始计算和,不然前面有负值,和肯定变小了
阅读全文
摘要:洛谷P1601 A+B Problem(高精) 输入格式 分两行输入。0 ≤ a , b ≤ 10^500 #include <iostream> using namespace std; #include <string> const int N = 501; int num1[N],num2[N
阅读全文
摘要:leetcode 31. 下一个排列 看了题解的思路,用自己看得懂的方式写的代码 class Solution { public: void reverse(int left,int right,vector<int>& nums){ for(int i = left,j = right;i < j
阅读全文
摘要:这两道题的本质是一样的,都是整数二分查找。题目给出的条件比较强,序列是严格单调递增的。 但是我这个即使序列存在重复的元素也可以满足需求 35. 搜索插入位置 class Solution { public: int searchInsert(vector<int>& nums, int target
阅读全文
摘要:273. 整数转换英文表示 这道题并不难,但是特别麻烦 我写的代码 class Solution { public: //转换个位数的英文 string baseNumber(int num){ if(num == 1) return "One"; else if(num == 2) return
阅读全文
摘要:50. Pow(x, n) 要特别注意 n 的范围 ,如果 n = -2^31,使用int 是不可以直接 n = -n; 的 一、使用long 这里做的时候没注意到已经使用了pow,所以这题可以说使用long做出来 class Solution { public: double myPow(doub
阅读全文
摘要:29. 两数相除 首先是我自己写出来的方法,复杂度为O(n) 一、使用long类型 class Solution { public: long divide2(long dividend,long divisor){ if(dividend < 0 && divisor < 0) return di
阅读全文
摘要:leetcode 4. 寻找两个正序数组的中位数 一、使用额外空间,类似归并排序的做法 class Solution { public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { int m = n
阅读全文
摘要:题目描述 有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛? 输入格式 输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0<n<55),n的含义如题目中描述。n=0表示输入数据的结束,不做处理。 输出格式
阅读全文
摘要:题目描述 将读入的 N 个数从小到大排序后输出。 输入格式 第一行为一个正整数 N。 第二行包含 N 个空格隔开的正整数 ,为你需要进行排序的数。 输出格式 将给定的 N 个数从小到大输出,数之间空格隔开,行末换行且无空格。 输入 #1 5 4 2 4 5 1 输出 #1 1 2 4 4 5 说明/
阅读全文
摘要:题目描述 将读入的 N 个数从小到大排序后输出。 输入格式 第一行为一个正整数 N。 第二行包含 N 个空格隔开的正整数 ,为你需要进行排序的数。 输出格式 将给定的 N 个数从小到大输出,数之间空格隔开,行末换行且无空格。 输入 #1 5 4 2 4 5 1 输出 #1 1 2 4 4 5 说明/
阅读全文
摘要:C++算法相关一些小细节 cin >> stl; //输入字符串时,遇到空格或者回车就会停止 cout << stl << endl; //输出字符串时,遇到空格或者回车不会停止 若要往字符数组读入一行字符串,包括空格,那么就要写成 String类 1. 2. 3. 不能用printf直接输出str
阅读全文