【Leetcode Sheet】Weekly Practice 24

这篇具有很好参考价值的文章主要介绍了【Leetcode Sheet】Weekly Practice 24。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Leetcode Test

447 回旋镖的数量(1.8)

给定平面上 n互不相同 的点 points ,其中 points[i] = [xi, yi]回旋镖 是由点 (i, j, k) 表示的元组 ,其中 ij 之间的距离和 ik 之间的欧式距离相等(需要考虑元组的顺序)。

返回平面上所有回旋镖的数量。

提示:

  • n == points.length
  • 1 <= n <= 500
  • points[i].length == 2
  • -104 <= xi, yi <= 104
  • 所有点都 互不相同

【枚举法】

class Solution {
public:
    int numberOfBoomerangs(vector<vector<int>> &points) {
        int ans = 0;
        unordered_map<int, int> cnt;
        for (auto &p1 : points) {
            cnt.clear();
            for (auto &p2 : points) {
                int d2 = (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]);
                ans += cnt[d2]++ * 2;
            }
        }
        return ans;
    }
};

2707 字符串中的额外字符(1.9)

给你一个下标从 0 开始的字符串 s 和一个单词字典 dictionary 。你需要将 s 分割成若干个 互不重叠 的子字符串,每个子字符串都在 dictionary 中出现过。s 中可能会有一些 额外的字符 不在任何子字符串中。

请你采取最优策略分割 s ,使剩下的字符 最少

提示:

  • 1 <= s.length <= 50
  • 1 <= dictionary.length <= 50
  • 1 <= dictionary[i].length <= 50
  • dictionary[i]s 只包含小写英文字母。
  • dictionary 中的单词互不相同。

【动态规划】

class Solution {
public:
    int minExtraChar(string s, vector<string>& dictionary) {
        int n = s.size();
        vector<int> d(n + 1, INT_MAX);
        unordered_map<string, int> mp;
        for (auto s : dictionary) {
            mp[s]++;
        }
        d[0] = 0;
        for (int i = 1; i <= n; i++) {
            d[i] = d[i - 1] + 1;
            for (int j = i - 1; j >= 0; j--) {
                if (mp.count(s.substr(j, i - j))) {
                    d[i] = min(d[i], d[j]);
                }
            }
        }
        return d[n];
    }
};

2696 删除字串后的字符串最小长度(1.10)

给你一个仅由 大写 英文字符组成的字符串 s

你可以对此字符串执行一些操作,在每一步操作中,你可以从 s 中删除 任一个 "AB""CD" 子字符串。

通过执行操作,删除所有 "AB""CD" 子串,返回可获得的最终字符串的 最小 可能长度。

注意,删除子串后,重新连接出的字符串可能会产生新的 "AB""CD" 子串。

提示:

  • 1 <= s.length <= 100
  • s 仅由大写英文字母组成

【栈】

int minLength(char *s){
    int n = strlen(s), m = 0;
    char *stack = (char *)malloc(sizeof(char) * n);
    for (int i = 0; i < n; i++) {
        stack[m++] = s[i];
        if (m >= 2 && (
            stack[m - 2] == 'A' && stack[m - 1] == 'B' ||
            stack[m - 2] == 'C' && stack[m - 1] == 'D'
        )) {
            m -= 2;
        }
    }
    free(stack);
    return m;
}

2645 构造有效数字串的最少插入数(1.11)

给你一个字符串 word ,你可以向其中任何位置插入 “a”、“b” 或 “c” 任意次,返回使 word 有效 需要插入的最少字母数。

如果字符串可以由 “abc” 串联多次得到,则认为该字符串 有效

提示:

  • 1 <= word.length <= 50
  • word 仅由字母 “a”、“b” 和 “c” 组成。

【计算组数】如果有一次下降,例如ba,则代表b和a肯定不是一个组里面的,需要插入字母变成2个组

int addMinimum(char * word){
    int n=strlen(word);
    int cnt=1;
    for(int i=1;i<n;i++){
        if(word[i]<=word[i-1]){
            cnt++;
        }
    }
    return cnt*3-n;
}

【动态规划】
d p [ 0 ] = 0 dp[0]=0 dp[0]=0

d [ i ] = d [ i − 1 ] + 2 ( w o r d [ i ] 单独存在于一组 a b c 中) d[i]=d[i-1]+2(word[i]单独存在于一组abc中) d[i]=d[i1]+2word[i]单独存在于一组abc中)

d [ i ] = d [ i − 1 ] − 1 ( w o r d [ i ] 可以和 w o r d [ i − 1 ] 在同一组 a b c 中) d[i]=d[i-1]-1(word[i]可以和word[i-1]在同一组abc中) d[i]=d[i1]1word[i]可以和word[i1]在同一组abc中)

int addMinimum(char * word) {
    int n = strlen(word);
    int d[n + 1];
    memset(d, 0, sizeof(d));
    for (int i = 1; i <= n; i++) {
        d[i] = d[i - 1] + 2;
        if (i > 1 && word[i - 1] > word[i - 2]) {
            d[i] = d[i - 1] - 1;
        }
    }
    return d[n];
}

2085 统计出现过一次的公共字符串(1.12)

给你两个字符串数组 words1words2 ,请你返回在两个字符串数组中 都恰好出现一次 的字符串的数目。

提示:

  • 1 <= words1.length, words2.length <= 1000
  • 1 <= words1[i].length, words2[j].length <= 30
  • words1[i]words2[j] 都只包含小写英文字母。

【hash】

class Solution {
public:
    int countWords(vector<string>& words1, vector<string>& words2) {
        unordered_map<string,int>f1,f2;
        for(auto w:words1){
            f1[w]++;
        }
        for(auto w:words2){
            f2[w]++;
        }
        int cnt=0;
        for(auto [w,num]:f1){
            if(num==1 && f2[w]==1){
                cnt++;
            }
        }
        return cnt;
    }
};

2182 构造限制重复的字符串(1.13)

给你一个字符串 s 和一个整数 repeatLimit ,用 s 中的字符构造一个新字符串 repeatLimitedString ,使任何字母 连续 出现的次数都不超过 repeatLimit 次。你不必使用 s 中的全部字符。

返回 字典序最大的 repeatLimitedString

如果在字符串 ab 不同的第一个位置,字符串 a 中的字母在字母表中出现时间比字符串 b 对应的字母晚,则认为字符串 a 比字符串 b 字典序更大 。如果字符串中前 min(a.length, b.length) 个字符都相同,那么较长的字符串字典序更大。

提示:

  • 1 <= repeatLimit <= s.length <= 105
  • s 由小写英文字母组成

【贪心 + 双指针 + hash计数】

char* repeatLimitedString(char* s, int repeatLimit) {
    int *cnt=(int*)malloc(sizeof(int)*26);
    for(int i=0;i<26;i++){
        cnt[i]=0;
    }
    int n=strlen(s);
    for(int i=0;i<n;i++){
        cnt[s[i]-'a']++;
    }
    
    char *ret=(char*)malloc(sizeof(char)*(n+1));
    char *p=ret;
    memset(ret,0,sizeof(char)*(n+1));

    int m=0;
    for(int i=25,j=24; i>=0 && j>=0; ){
        //当前字符用完
        if(cnt[i]==0){
            m=0;
            i--;
        }
        //当前字符未超过limit
        else if(m < repeatLimit){
            cnt[i]--;
            *(p++)='a'+i;
            m++;
        }
        //当前字符超过限制,查找其他可填入字符
        else if(j>=i || cnt[j]==0){
            j--;
        }
        //当前字符超过limit,填入其他字符
        else{
            cnt[j]--;
            *(p++)='a'+j;
            m=0;
        }
    }
    return ret;
}

83 删除排序链表中的重复元素(1.14)

给定一个已排序的链表的头 head删除所有重复的元素,使每个元素只出现一次 。返回 已排序的链表

提示:

  • 链表中节点数目在范围 [0, 300]
  • -100 <= Node.val <= 100
  • 题目数据保证链表已经按升序 排列

【模拟】文章来源地址https://www.toymoban.com/news/detail-802689.html

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* deleteDuplicates(struct ListNode* head) {
    if(head == NULL) return head;
    
    struct ListNode* t=head;
    while(t->next != NULL){
        if(t->next->val == t->val){
            t->next=t->next->next;
        }
        else{
            t=t->next;
        }
    }
    return head;
}

到了这里,关于【Leetcode Sheet】Weekly Practice 24的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode笔记:Weekly Contest 359

    LeetCode笔记:Weekly Contest 359 1. 题目一 1. 解题思路 2. 代码实现 2. 题目二 1. 解题思路 2. 代码实现 3. 题目三 1. 解题思路 2. 代码实现 4. 题目四 1. 解题思路 2. 代码实现 比赛链接:https://leetcode.com/contest/weekly-contest-359 给出题目一的试题链接如下: 2828. Check if a String Is an Acronym of

    2024年02月12日
    浏览(27)
  • LeetCode笔记:Weekly Contest 354

    LeetCode笔记:Weekly Contest 354 1. 题目一 1. 解题思路 2. 代码实现 2. 题目二 1. 解题思路 2. 代码实现 3. 题目三 1. 解题思路 2. 代码实现 4. 题目四 1. 解题思路 2. 代码实现 比赛链接:https://leetcode.com/contest/weekly-contest-354/ 给出题目一的试题链接如下: 2778. Sum of Squares of Special Elements

    2024年02月16日
    浏览(29)
  • LeetCode笔记:Weekly Contest 360

    LeetCode笔记:Weekly Contest 360 0. 吐槽 1. 题目一 1. 解题思路 2. 代码实现 2. 题目二 1. 解题思路 2. 代码实现 3. 题目三 1. 解题思路 2. 代码实现 4. 题目四 1. 解题思路 2. 代码实现 比赛链接:https://leetcode.com/contest/weekly-contest-360/ 这次的题目真的是,一言难尽,难倒是不难,就是各种

    2024年02月11日
    浏览(31)
  • LeetCode笔记:Weekly Contest 349

    LeetCode笔记:Weekly Contest 349 1. 题目一 1. 解题思路 2. 代码实现 2. 题目二 1. 解题思路 2. 代码实现 3. 题目三 1. 解题思路 2. 代码实现 4. 题目四 比赛链接:https://leetcode.com/contest/weekly-contest-349 给出题目一的试题链接如下: 2733. Neither Minimum nor Maximum 1. 解题思路 这一题我实现的比较

    2024年02月08日
    浏览(24)
  • LeetCode笔记:Weekly Contest 357

    LeetCode笔记:Weekly Contest 357 1. 题目一 1. 解题思路 2. 代码实现 2. 题目二 1. 解题思路 2. 代码实现 3. 题目三 1. 解题思路 2. 代码实现 4. 题目四 比赛链接:https://leetcode.com/contest/weekly-contest-357 给出题目一的试题链接如下: 2810. Faulty Keyboard 1. 解题思路 这一题就是按照题目给出的条

    2024年02月14日
    浏览(28)
  • Algorithms practice:leetcode 33. Search in Rotated Sorted Array

    Algorithms practice:leetcode33 Search in Rotated Sorted Array There is an integer array ,nums , sorted in ascending order (with distinct values). Prior to being passed to your function, nums is possibly rotated,at an unknown pivot index k (1 = k nums.length) such that the resulting array is [nums[k], nums[k+1], …, nums[n-1], nums[0], nums[1], …, nums

    2024年01月21日
    浏览(36)
  • LeetCode171. Excel Sheet Column Number

    Given a string columnTitle that represents the column title as appears in an Excel sheet, return its corresponding column number. For example: A - 1 B - 2 C - 3 … Z - 26 AA - 27 AB - 28 … Example 1: Input: columnTitle = “A” Output: 1 Example 2: Input: columnTitle = “AB” Output: 28 Example 3: Input: columnTitle = “ZY” Output: 701 Constraints:

    2024年02月19日
    浏览(29)
  • 代码随想录刷题第4天|LeetCode24、LeetCode19、LeetCode160、LeetCode142

    1、LeetCode24 两两交换链表中的节点 题目链接:24、两两交换链表中的节点 要想清楚终止条件,cur每次指向要交换的两个节点的前一个节点,cur = cur-next-next; 若链表元素个数为偶数 , 则最后时刻 cur-next = NULL; 若链表元素个数为奇数,则最后时刻 cur-next-next = NULL; 最后要返回

    2024年02月05日
    浏览(37)
  • LeetCode75——Day24

    2390. Removing Stars From a String You are given a string s, which contains stars *. In one operation, you can: Choose a star in s. Remove the closest non-star character to its left, as well as remove the star itself. Return the string after all stars have been removed. Note: The input will be generated such that the operation is always possible. It can be s

    2024年02月06日
    浏览(23)
  • 【LeetCode: 面试题 17.24. 最大子矩阵 | 动态规划 】

    🚀 算法题 🚀 🌲 算法刷题专栏 | 面试必备算法 | 面试高频算法 🍀 🌲 越难的东西,越要努力坚持,因为它具有很高的价值,算法就是这样✨ 🌲 作者简介:硕风和炜,CSDN-Java领域优质创作者🏆,保研|国家奖学金|高中学习JAVA|大学完善JAVA开发技术栈|面试刷题|面经八股文

    2024年02月06日
    浏览(46)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包