随笔分类 - LeetCode每日一题
摘要:1601. 最多可达成的换楼请求数目 解题思路:由于数据不超过20,所以二进制枚举是可行的 class Solution { public: int maximumRequests(int n, vector<vector<int>>& requests) { int ans=0; int flag
阅读全文
摘要:553. 最优除法 思路:因为要x1/x2/x3/...../xn最大,可以看成x/y,当x仅仅为x1时,x最大 class Solution { public: string optimalDivision(vector<int>& nums) { int len=nums.size(); str
阅读全文
摘要:题目链接 解题思路:简单模拟即可 class Solution { public: int numberOfMatches(int n) { int sum=0; while(n!=1){ if(n%2==0){ n=n>>1; sum+=n; } else{ n=n>>1; sum+=n; n++
阅读全文
摘要:题目链接 水题:因为仅有‘a’与‘b’,最多删除两次 第一次删除所有‘a’,第二次删除所有‘b’。 如果字符串s为回文串,一次删完即可,否则删两次 class Solution { public: int removePalindromeSub(string s) { string str=s; r
阅读全文
摘要:题目 挺简单的一道DFS水题 DFS遍历每一个节点,求每个节点的子树的最大深度,所以根的最大深度,就是整棵树的最大深度。 public: int DFS(Node* node) { if (node == nullptr) return 0; int ans = 1; for (int i = 0;
阅读全文
摘要:给定一个正整数 n ,你可以做如下操作: 如果 n 是偶数,则用 n / 2替换 n 。 如果 n 是奇数,则可以用 n + 1或n - 1替换 n 。 n 变为 1 所需的最小替换次数是多少? 示例 1: 输入:n = 8 输出:3 解释:8 -> 4 -> 2 -> 1 示例 2: 输入:n =
阅读全文