leetcode题目(2)

目录

1.删除有序数组中的重复项

2.移除元素

3.找出字符中第一个匹配项的下标

4.搜索插入位置

5.最大子数组和

6.最后一个单词的长度


1.删除有序数组中的重复项

https://leetcode.cn/problems/remove-duplicates-from-sorted-array/description/

cpp 复制代码
int removeDuplicates(int* nums, int numsSize) {
    if(numsSize == 0)
        return 0;
    int fast = 1,slow = 1;
    while(fast < numsSize)
    {
        if(nums[fast] != nums[fast - 1])
        {
            nums[slow] = nums[fast];
            ++slow;
        }
        ++fast;
    }
    return slow;
}

2.移除元素

https://leetcode.cn/problems/remove-element/

cpp 复制代码
int removeElement(int* nums, int numsSize, int val) {
    int fast = 0,slow = 0;
    for(int i = 0;i < numsSize;i++)
    {
        if(nums[fast] != val)
        {
            nums[slow] = nums[fast];
            fast++;
            slow++;
        }
        else
        {
            ++fast;
        }
    }
    return slow;
}

3.找出字符中第一个匹配项的下标

https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string/

cpp 复制代码
class Solution {
public:
    int strStr(string haystack, string needle) {
        int n = haystack.size(), m = needle.size(); 
        if (m == 0) 
            return 0; 

        vector<int> pi(m); // 创建一个数组pi,用于存储needle的前缀函数值
        for (int i = 1, j = 0; i < m; i++) { // 从needle的第二个字符开始遍历
            while (j > 0 && needle[i] != needle[j]) { // 如果当前字符不匹配,且j不为0
                j = pi[j - 1]; // 更新j为pi[j-1],即最长的相同前缀后缀的长度
            }
            if (needle[i] == needle[j]) { // 如果当前字符匹配
                j++; // 更新j为下一个字符
            }
            pi[i] = j; // 更新pi数组
        }
        for (int i = 0, j = 0; i < n; i++) { // 遍历haystack
            while (j > 0 && haystack[i] != needle[j]) { // 如果当前字符不匹配,且j不为0
                j = pi[j - 1]; // 更新j为pi[j-1]
            }
            if (haystack[i] == needle[j]) { // 如果当前字符匹配
                j++; // 更新j为下一个字符
            }
            if (j == m) { // 如果j等于needle的长度,说明找到了匹配的子字符串
                return i - m + 1; // 返回needle在haystack中的起始索引
            }
        }
        return -1; // 如果遍历完haystack都没有找到needle,返回-1
    }
};

4.搜索插入位置

https://leetcode.cn/problems/search-insert-position/

cpp 复制代码
int searchInsert(int* nums, int numsSize, int target) {
    for(int i = 0;i<numsSize;++i)
    {
        //如果值相等,返回索引
        if(nums[i] == target)
            return i;
        //小于返回当前索引
        if(target < nums[i])
            return i;
    }
    //大于所有,返回数组长度
    return numsSize;
}

5.最大子数组和

https://leetcode.cn/problems/maximum-subarray/

cpp 复制代码
class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int pre = 0,maxsum = nums[0];
        for(const auto& x: nums)
        {
            //将x加入到子数组中与x+pre比较大小,更新最大值
            pre = max(pre + x, x);
            maxsum = max(maxsum,pre);
        }
        return maxsum;
    }
};

6.最后一个单词的长度

https://leetcode.cn/problems/length-of-last-word/description/

cpp 复制代码
class Solution {
public:
    int lengthOfLastWord(string s) {
        int index = s.size() - 1;

        while(s[index] == ' ')
            index--;

        int wordlength = 0;
        while(index >= 0 && s[index] != ' ')
        {
            wordlength++;
            index--;
        }
        return wordlength;
    }
};
相关推荐
Tech Synapse8 分钟前
基于Surprise和Flask构建个性化电影推荐系统:从算法到全栈实现
python·算法·flask·协同过滤算法
終不似少年遊*15 分钟前
国产之光DeepSeek架构理解与应用分析04
人工智能·python·深度学习·算法·大模型·ds
天天扭码15 分钟前
一分钟解决 | 高频面试算法题——最大子数组之和
前端·算法·面试
杰杰批20 分钟前
力扣热题100——矩阵
算法·leetcode·矩阵
明月看潮生23 分钟前
青少年编程与数学 02-016 Python数据结构与算法 28课题、图像处理算法
图像处理·python·算法·青少年编程·编程与数学
_GR34 分钟前
2025年蓝桥杯第十六届C&C++大学B组真题及代码
c语言·数据结构·c++·算法·贪心算法·蓝桥杯·动态规划
心想事“程”34 分钟前
决策树详解+面试常见问题
算法·决策树·机器学习
mahuifa1 小时前
(7)VTK C++开发示例 --- 使用交互器
c++·vtk·cmake·3d开发
照海19Gin2 小时前
数据结构中的宝藏秘籍之广义表
c语言·数据结构·算法
光算科技2 小时前
服务器在国外国内用户访问慢会影响谷歌排名吗?
运维·服务器·c++