【记录】LeetCode刷题C++ - 简单

1480 一维数组的动态和

【问题描述】

给你一个数组 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]
解释:动态和计算过程为 [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1] 。

示例3
输入:nums = [3,1,2,10,1]
输出:[3,4,6,16,17]

【提示】

  • 1 <= nums.length <= 1000
  • -10^6 <= nums[i] <= 10^6

C++中,int占用4字节,32比特,数据范围为-21 4748 3648 ~ 21 4748 3647[-2^31 ~ 2^31-1]。

int占用2字节,16比特,数据范围为-3 2768 ~ 3 2767[-2^15 ~ 2^15-1]。

【题解】

class Solution {
public:
    vector<int> runningSum(vector<int>& nums) {
        //如果去掉这句的话,内存消耗增加。
        int size = nums.size(); 
        for(int i = 1; i < size; i++)
            nums[i] += nums[i-1];
        return nums;
    }
};

1512 好数对的数目

【问题描述】
给你一个整数数组 nums 。
如果一组数字 (i,j) 满足 nums[i] == nums[j] 且 i < j ,就可以认为这是一组 好数对
返回好数对的数目。

示例 1:
输入:nums = [1,2,3,1,1,3]
输出:4
解释:有 4 组好数对,分别是 (0,3), (0,4), (3,4), (2,5) ,下标从 0 开始

示例 2:
输入:nums = [1,1,1,1]
输出:6
解释:数组中的每组数字都是好数对

示例 3:
输入:nums = [1,2,3]
输出:0

【提示】

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100
  • Count how many times each number appears. If a number appears n
    times, then n * (n – 1) // 2 good pairs can be made with this number.

【题解】

【方法1】暴力统计:
时间复杂度:O(n^2)
空间复杂度:O(1)
class Solution {
public:
    int numIdenticalPairs(vector<int>& nums) {
        int ans = 0;
        for (int i = 0; i < nums.size(); ++i) {
            for (int j = i + 1; j < nums.size(); ++j) {
                if (nums[i] == nums[j]) {
                    ++ans;
                }
            }
        }
        return ans;
    }
};
【方法2】组合计数:
时间复杂度:O(n)。
空间复杂度:O(n),即哈希表使用到的辅助空间的空间代价。
class Solution {
public:
    int numIdenticalPairs(vector<int>& nums) {
        unordered_map <int, int> m;
        for (int num: nums) {
            ++m[num];
        }

        int ans = 0;
        for (const auto &[k, v]: m) {
            ans += v * (v - 1) / 2;
        }

        return ans;
    }
};

时间复杂度:指执行算法所需要的计算工作量。
分析方法:

  • 只关注循环执行次数最多的的一段代码。
    大 O 这种复杂度表示方法只是表示一种变化趋势。我们通常会忽略掉公式中的常量、低阶、系数,只需要记录一个最大阶的量级就可以了。所以,我们在分析一个算法、一段代码的时间复杂度的时候,也只关注循环执行次数最多的那一段代码就可以了。这段核心代码执行次数的 n 的量级,就是整段要分析代码的时间复杂度。
  • 加法法则:总复杂度等于量级最大的那段代码的复杂度。
    综合这三段代码的时间复杂度(分别是O(1), O(n), O(n2)),我们取其中最大的量级。所以,整段代码的时间复杂度就为 O(n2)。也就是说:总的时间复杂度就等于量级最大的那段代码的时间复杂度。
  • 乘法法则:嵌套代码的复杂度等于嵌套内外代码复杂度的乘积
    类似嵌套循环的,都是用乘法来处理。
    【记录】LeetCode刷题C++ - 简单_第1张图片
    【记录】LeetCode刷题C++ - 简单_第2张图片

空间复杂度:指执行这个算法所需要的内存空间。
分析方法:
新申请多大的数。
空间复杂度的计算一般不包括返回答案用的数组。

map:
优点:

  • 有序性:
    这是map结构最大的优点,其元素的有序性在很多应用中都会简化很多的操作。
  • 效率高:
    内部实现一个红黑树使得map的很多操作在lgn的时间复杂度下就可以实现,因此效率非常的高。

缺点:

  • 空间占用率高:
    因为map内部实现了红黑树,虽然提高了运行效率,但是因为每一个节点都需要额外保存父节点、孩子节点和红/黑性质,使得每一个节点都占用大量的空间。

适用处:

  • 对于那些有顺序要求的问题,用map会更高效一些。

unodered_map:
优点:

  • 查找速度快:
    因为内部实现了哈希表,因此其查找速度非常的快。

缺点:

  • 耗费时间:
    哈希表的建立比较耗费时间。

适用处:

  • 对于查找问题,unordered_map会更加高效一些,因此遇到查找问题,常会考虑一下用unordered_map。
  • auto:
    auto即 for(auto x:range) 。这样会拷贝一份range元素,而不会改变range中元素。
    但是,使用for(auto x:vector)时得到一个proxy class,操作时会改变vector本身元素。应用:for(bool x:vector)
  • auto &:
    当需要修改range中元素,用for(auto& x:range)。
    当vector返回临时对象,使用auto&会编译错误,临时对象不能绑在non-const l-value reference (左值引用)需使用auto&&,初始化右值时也可捕获。
  • const auto &:
    当只想读取range中元素时,使用const auto&,如:for(const auto&x:range),它不会进行拷贝,也不会修改range。
  • const auto:
    当需要拷贝元素,但不可修改拷贝出来的值时,使用 for(const auto x:range),避免拷贝开销。

1470 重新排列数组

【问题描述】
给你一个数组 nums ,数组中有 2n 个元素,按 [x1,x2,…,xn,y1,y2,…,yn] 的格式排列。
请你将数组按 [x1,y1,x2,y2,…,xn,yn] 格式重新排列,返回重排后的数组。

示例 1:
输入:nums = [2,5,1,3,4,7], n = 3
输出:[2,3,5,4,1,7] 
解释:由于 x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 ,所以答案为 [2,3,5,4,1,7]

示例 2:
输入:nums = [1,2,3,4,4,3,2,1], n = 4
输出:[1,4,2,3,3,2,4,1]

示例 3:
输入:nums = [1,1,2,2], n = 2
输出:[1,2,1,2]

【提示】

  • 1 <= n <= 500
  • nums.length == 2n
  • 1 <= nums[i] <= 10^3

【题解】

时间复杂度O(n)
空间复杂度O(n)
class Solution {
public:
    vector<int> shuffle(vector<int>& nums, int n) {
        vector<int> ans;
        for(int i = 0; i < n; i++){
            ans.push_back(nums[i]);
            ans.push_back(nums[i + n]);
        }
        return ans;
    }
};

push:栈

push_back:vector、list

vector
使用方法:
vector的使用方法1
vector的使用方法2

1 两数之和

【问题描述】
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。

示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

【题解】

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> ans;
        unordered_map<int, int> hashmap;
        for(int i = 0; i < nums.size(); i++){
            if(hashmap[target-nums[i]]){
                ans.push_back(i);
                ans.push_back(hashmap[target-nums[i]]-1);
                return ans;
            }
            hashmap[nums[i]] = i + 1;
        }
        return ans;
    }
};

map
使用方法:
map使用方法

unordered_map
使用方法

1431 拥有最多糖果的孩子

【问题描述】
给你一个数组 candies 和一个整数 extraCandies ,其中 candies[i] 代表第 i 个孩子拥有的糖果数目。
对每一个孩子,检查是否存在一种方案,将额外的 extraCandies 个糖果分配给孩子们之后,此孩子有 最多 的糖果。注意,允许有多个孩子同时拥有 最多 的糖果数目。

示例 1:
输入:candies = [2,3,5,1,3], extraCandies = 3
输出:[true,true,true,false,true] 
解释:
孩子 12 个糖果,如果他得到所有额外的糖果(3个),那么他总共有 5 个糖果,他将成为拥有最多糖果的孩子。
孩子 23 个糖果,如果他得到至少 2 个额外糖果,那么他将成为拥有最多糖果的孩子。
孩子 35 个糖果,他已经是拥有最多糖果的孩子。
孩子 41 个糖果,即使他得到所有额外的糖果,他也只有 4 个糖果,无法成为拥有糖果最多的孩子。
孩子 53 个糖果,如果他得到至少 2 个额外糖果,那么他将成为拥有最多糖果的孩子。

【提示】
2 <= candies.length <= 100
1 <= candies[i] <= 100
1 <= extraCandies <= 50
【题解】

class Solution {
public:
    vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {
	    vector<bool> result;
	    auto max_ptr = max_element(candies.begin(), candies.end());
	    int max = *max_ptr;
	    for (auto i : candies)
		    result.push_back(i + extraCandies >= max);
	    return result;
    }
};

vector用法:
max_element(vector.begin(), vector.end());
//得到指向最大值的指针

1486 数组异或操作

【问题描述】
给你两个整数,n 和 start 。
数组 nums 定义为:nums[i] = start + 2*i(下标从 0 开始)且 n == nums.length 。
请返回 nums 中所有元素按位异或(XOR)后得到的结果。

示例 1:
输入:n = 5, start = 0
输出:8
解释:数组 nums 为 [0, 2, 4, 6, 8],其中 (0 ^ 2 ^ 4 ^ 6 ^ 8) = 8"^" 为按位异或 XOR 运算符。

【题解】

class Solution {
public:
    int xorOperation(int n, int start) {
        int ans = start;
        for(int i = 1; i < n; i++)
            ans = ans ^ (start + 2 * i);
        return ans;
    }
};

剑指 Offer 58 - II 左旋转字符串

【问题描述】
字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。

示例 1:
输入: s = "abcdefg", k = 2
输出: "cdefgab"

【题解】

class Solution {
public:
    string reverseLeftWords(string s, int n) {
        return (s+s).substr(n,s.size());
    }
};

面试题 02.03 删除中间节点

【问题描述】
实现一种算法,删除单向链表中间的某个节点(即不是第一个或最后一个节点),假定你只能访问该节点。

示例:
输入:单向链表a->b->c->d->e->f中的节点c
结果:不返回任何数据,但该链表变为a->b->d->e->f

【题解】

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void deleteNode(ListNode* node) {
        node->val = node->next->val;
        ListNode* tmp = node->next;
        node->next = node->next->next;
        delete tmp;
    }
};

LCP 01 猜数字

【问题描述】
小A 和 小B 在玩猜数字。小B 每次从 1, 2, 3 中随机选择一个,小A 每次也从 1, 2, 3 中选择一个猜。他们一共进行三次这个游戏,请返回 小A 猜对了几次?
输入的guess数组为 小A 每次的猜测,answer数组为 小B 每次的选择。guess和answer的长度都等于3。

示例 1:
输入:guess = [1,2,3], answer = [1,2,3]
输出:3
解释:小A 每次都猜对了。

示例 2:
输入:guess = [2,2,3], answer = [3,2,1]
输出:1
解释:小A 只猜对了第二次。

【题解】

class Solution {
public:
    int game(vector<int>& guess, vector<int>& answer) {
        int cnt = 0;
        for(int i=0; i < answer.size(); i++){
            if(guess[i]==answer[i])
                cnt++;
        }
        return cnt;
    }
};

class Solution {
public:
	int game(vector<int>& guess, vector<int>& answer) {
		return (guess[0]==answer[0])+(guess[1]==answer[1])+(guess[2]==answer[2]);
	}
}

771 宝石与石头

【问题描述】
给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。
J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。

示例 1:
输入: J = "aA", S = "aAAbbbb"
输出: 3

示例 2:
输入: J = "z", S = "ZZ"
输出: 0

【题解】

class Solution {
public:
    int numJewelsInStones(string J, string S) {
        unordered_map<char,int> map;
        for(auto c:S) map[c] ++;
        int res = 0;
        for(auto c:J) res += map[c];
        return res;
    }
};

1108 IP地址无效化

【问题描述】
给你一个有效的 IPv4 地址 address,返回这个 IP 地址的无效化版本。
所谓无效化 IP 地址,其实就是用 “[.]” 代替了每个 “.”。

示例 1:
输入:address = "1.1.1.1"
输出:"1[.]1[.]1[.]1"

示例 2:
输入:address = "255.100.50.0"
输出:"255[.]100[.]50[.]0"

【题解】

class Solution {
public:
    string defangIPaddr(string address) {
        for(int i = 0; i < address.length(); i++){
            if(address[i] == '.'){
                address.replace(i, 1, "[.]");
                i += 2;
            }
        }
        return address;
    }
};

1281 整数的各位积和之差

【问题描述】
给你一个整数 n,请你帮忙计算并返回该整数「各位数字之积」与「各位数字之和」的差。

示例 1:
输入:n = 234
输出:15 
解释:
各位数之积 = 2 * 3 * 4 = 24 
各位数之和 = 2 + 3 + 4 = 9 
结果 = 24 - 9 = 15

【题解】

class Solution {
public:
    int subtractProductAndSum(int n) {
        int prt = 1, sum = 0;
        while(n) {
            prt *= n % 10;
            sum += n % 10;
            n /= 10;
        }
        return prt-sum;
    }
};

LCP 06 拿硬币

【问题描述】
桌上有 n 堆力扣币,每堆的数量保存在数组 coins 中。我们每次可以选择任意一堆,拿走其中的一枚或者两枚,求拿完所有力扣币的最少次数。

示例 1:
输入:[4,2,1]
输出:4
解释:第一堆力扣币最少需要拿 2 次,第二堆最少需要拿 1 次,第三堆最少需要拿 1 次,总共 4 次即可拿完。

示例 2:
输入:[2,3,10]
输出:8

【题解】

class Solution {
public:
    int minCount(vector<int>& coins) {
        int ans = 0;
        for(const auto c:coins) ans += (c / 2) + (c % 2);
        return ans;
    }
};

1313 解压缩编码列表

【问题描述】
给你一个以行程长度编码压缩的整数列表 nums 。
考虑每对相邻的两个元素 [freq, val] = [nums[2i], nums[2i+1]] (其中 i >= 0 ),每一对都表示解压后子列表中有 freq 个值为 val 的元素,你需要从左到右连接所有子列表以生成解压后的列表。
请你返回解压后的列表。

示例:
输入:nums = [1,2,3,4]
输出:[2,4,4,4]
解释:第一对 [1,2] 代表着 2 的出现频次为 1,所以生成数组 [2]。
第二对 [3,4] 代表着 4 的出现频次为 3,所以生成数组 [4,4,4]。
最后将它们串联到一起 [2] + [4,4,4] = [2,4,4,4]。

示例 2:
输入:nums = [1,1,2,3]
输出:[1,3,3]

【题解】

class Solution {
public:
    vector<int> decompressRLElist(vector<int>& nums) {
        vector<int> ans;
        for(int i = 1; i < nums.size(); i += 2){
            for(int j = nums[i-1]; j > 0; j--){
                ans.push_back(nums[i]);
            }
        }
        return ans;
    }
};

237 删除链表中的节点

【问题描述】
请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点。传入函数的唯一参数为 要被删除的节点 。

示例 1:
输入:head = [4,5,1,9], node = 5
输出:[4,1,9]
解释:给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.

示例 2:
输入:head = [4,5,1,9], node = 1
输出:[4,5,9]
解释:给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9.

【提示】

  • 链表至少包含两个节点。
  • 链表中所有节点的值都是唯一的。
  • 给定的节点为非末尾节点并且一定是链表中的一个有效节点。
  • 不要从你的函数中返回任何结果。

【题解】

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void deleteNode(ListNode* node) {
        node->val = node->next->val;
        ListNode* tmp = node->next;
        node->next = node->next->next;
        delete tmp;
    }
};

1342 将数字变成 0 的操作次数

【问题描述】
给你一个非负整数 num ,请你返回将它变成 0 所需要的步数。 如果当前数字是偶数,你需要把它除以 2 ;否则,减去 1 。

示例 1:
输入:num = 14
输出:6
解释:
步骤 1) 14 是偶数,除以 2 得到 7 。
步骤 27 是奇数,减 1 得到 6 。
步骤 36 是偶数,除以 2 得到 3 。
步骤 43 是奇数,减 1 得到 2 。
步骤 52 是偶数,除以 2 得到 1 。
步骤 61 是奇数,减 1 得到 0

【题解】

class Solution {
public:
    int numberOfSteps (int num) {
        int cnt = 0;
        while(num){
            if(num % 2 == 0) num /= 2;
            else num -= 1;
            cnt ++;
        }
        return cnt;
    }
};

1389 按既定顺序创建目标数组

【问题描述】
给你两个整数数组 nums 和 index。你需要按照以下规则创建目标数组:
目标数组 target 最初为空。
按从左到右的顺序依次读取 nums[i] 和 index[i],在 target 数组中的下标 index[i] 处插入值 nums[i] 。
重复上一步,直到在 nums 和 index 中都没有要读取的元素。
请你返回目标数组。
题目保证数字插入位置总是存在。

示例 1:
输入:nums = [0,1,2,3,4], index = [0,1,2,2,1]
输出:[0,4,1,3,2]
解释:
nums       index     target
0            0        [0]
1            1        [0,1]
2            2        [0,1,2]
3            2        [0,1,3,2]
4            1        [0,4,1,3,2]

【题解】

class Solution {
public:
    vector<int> createTargetArray(vector<int>& nums, vector<int>& index) {
        vector<int> target;
        for(int i = 0; i < index.size(); i++){
            target.insert(target.begin()+index[i], nums[i]);
        }
        return target;
    }
};

1365 有多少小于当前数字的数字

【问题描述】
给你一个数组 nums,对于其中每个元素 nums[i],请你统计数组中比它小的所有数字的数目。
换而言之,对于每个 nums[i] 你必须计算出有效的 j 的数量,其中 j 满足 j != i 且 nums[j] < nums[i] 。
以数组形式返回答案。

示例 1:
输入:nums = [8,1,2,2,3]
输出:[4,0,1,1,3]
解释: 
对于 nums[0]=8 存在四个比它小的数字:(1223)。 
对于 nums[1]=1 不存在比它小的数字。
对于 nums[2]=2 存在一个比它小的数字:(1)。 
对于 nums[3]=2 存在一个比它小的数字:(1)。 
对于 nums[4]=3 存在三个比它小的数字:(122)。

【题解】


485 Max Consecutive Ones

[本人已经裂开,英文题走起]
【description】
Given a binary array, find the maximum number of consecutive 1s in this array.

Example 1:
Input: [1,1,0,1,1,1]
Output: 3
Explanation: The first two digits or the last three digits are consecutive 1s.
    The maximum number of consecutive 1s is 3.

【note】
The input array will only contain 0 and 1.
The length of input array is a positive integer and will not exceed 10,000
【answer】

class Solution {
public:
    int findMaxConsecutiveOnes(vector<int>& nums) {
        int ans = 0, cnt = 0;
        for(const auto& num : nums){
            cnt = num==1?cnt+1:0;
            ans = cnt>ans?cnt:ans;
        }
        return ans;
    }
};

495 Teemo Attacking

【discription】
In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo’s attacking ascending time series towards Ashe and the poisoning time duration per Teemo’s attacking, you need to output the total time that Ashe is in poisoned condition.
You may assume that Teemo attacks at the very beginning of a specific time point, and makes Ashe be in poisoned condition immediately.

Example 1:
Input: [1,4], 2
Output: 4
Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned immediately. 
This poisoned status will last 2 seconds until the end of time point 2. 
And at time point 4, Teemo attacks Ashe again, and causes Ashe to be in poisoned status for another 2 seconds. 
So you finally need to output 4.

Example 2:
Input: [1,2], 2
Output: 3
Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned. 
This poisoned status will last 2 seconds until the end of time point 2. 
However, at the beginning of time point 2, Teemo attacks Ashe again who is already in poisoned status. 
Since the poisoned status won't add up together, though the second poisoning attack will still work at time point 2, it will stop at the end of time point 3. 
So you finally need to output 3.

【note】

You may assume the length of given time series array won't exceed 10000.
You may assume the numbers in the Teemo's attacking time series and his poisoning time duration per attacking are non-negative integers, which won't exceed 10,000,000.

【answer】

class Solution {
public:
    int findPoisonedDuration(vector<int>& timeSeries, int duration) {
    	//一定注意对空数组的判定
        if((duration == 0)||(timeSeries.empty())) return 0;
        int ans = duration;
        for(int i = 1; i < timeSeries.size(); i++){
            ans += min(timeSeries[i]-timeSeries[i-1], duration);
        }
        return ans;
    }
};

1295 Find Numbers with Even Number of Digits

【discription】
Given an array nums of integers, return how many of them contain an even number of digits.

Example 1:
Input: nums = [12,345,2,6,7896]
Output: 2
Explanation: 
12 contains 2 digits (even number of digits). 
345 contains 3 digits (odd number of digits). 
2 contains 1 digit (odd number of digits). 
6 contains 1 digit (odd number of digits). 
7896 contains 4 digits (even number of digits). 
Therefore only 12 and 7896 contain an even number of digits.

【answer】

class Solution {
public:
    int findNumbers(vector<int>& nums) {
        int ans = 0;
        for(int n:nums){
        	// if(to_string(n).size()%2==0) ans++;
            if((int)(log10(n)+1)%2==0) ans++;
        }
        return ans;
    }
};

1266 Minimum Time Visiting All Points

【discription】
On a plane there are n points with integer coordinates points[i] = [xi, yi]. Your task is to find the minimum time in seconds to visit all points.
You can move according to the next rules:
In one second always you can either move vertically, horizontally by one unit or diagonally (it means to move one unit vertically and one unit horizontally in one second).
You have to visit the points in the same order as they appear in the array.

Example 1:
Input: points = [[1,1],[3,4],[-1,0]]
Output: 7
Explanation: One optimal path is [1,1] -> [2,2] -> [3,3] -> [3,4] -> [2,3] -> [1,2] -> [0,1] -> [-1,0]   
Time from [1,1] to [3,4] = 3 seconds 
Time from [3,4] to [-1,0] = 4 seconds
Total time = 7 seconds

Example 2:
Input: points = [[3,2],[-2,2]]
Output: 5

【answer】
【记录】LeetCode刷题C++ - 简单_第3张图片

class Solution {
public:
    int minTimeToVisitAllPoints(vector<vector<int>>& points) {
        int ans = 0;
        for(int i = 0; i < points.size()-1; i ++){
        	// 虽然题目分为两大种情况:dx不等dy、dx等于dy;事实上等同于求max(dx,dy)。
            ans += max(abs(points[i][0]-points[i+1][0]), abs(points[i][1]-points[i+1][1]));
        }
        return ans;
    }
};

1450 Number of Students Doing Homework at a Given Time

【discription】
Given two integer arrays startTime and endTime and given an integer queryTime.
The ith student started doing their homework at the time startTime[i] and finished it at time endTime[i].
Return the number of students doing their homework at time queryTime. More formally, return the number of students where queryTime lays in the interval [startTime[i], endTime[i]] inclusive.

Example 1:
Input: startTime = [1,2,3], endTime = [3,2,7], queryTime = 4
Output: 1
Explanation: We have 3 students where:
The first student started doing homework at time 1 and finished at time 3 and wasn't doing anything at time 4.
The second student started doing homework at time 2 and finished at time 2 and also wasn't doing anything at time 4.
The third student started doing homework at time 3 and finished at time 7 and was the only student doing homework at time 4.

【answer】

class Solution {
public:
    int busyStudent(vector<int>& startTime, vector<int>& endTime, int queryTime) {
        int cnt = 0;
        for(int i = 0; i < startTime.size(); i++){
            cnt = (queryTime >= startTime[i])&&(queryTime <= endTime[i])?cnt+1:cnt;
        }
        return cnt;
    }
};

1528 Shuffle String

【discription】
Given a string s and an integer array indices of the same length.
The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string.
Return the shuffled string.

Example 1:
Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3]
Output: "leetcode"
Explanation: As shown, "codeleet" becomes "leetcode" after shuffling.

【answer】

你可能感兴趣的:(算法)