LeetCode 25-28

Problem 25 Reverse Nodes in k-Group

这个解法因该是不符合题目要求的,不过暂时没有新的想法,就先这样吧。
既然要分成每组k个的小组,那么如果这是一个数组的话就很好操作。首先用vector存下这个链表,然后利用下标索引来找到每一个分组。这里需要的单独处理第一个分组。因为头指针的特殊性——没有指向头指针的指针,所以需要单独处理。其他情况下,只需要把分组的第一个指针的前一个节点的指针指向分组的最后一个节点,分组第一个节点的next指向分组最后一个节点的下一个节点,并重新排列分组中的每一个节点的指针指向即可。对于不满k个的分组则不做变动。

ListNode* reverseKGroup(ListNode* head, int k) {
    if (head == NULL) return head;
    if (k <= 1) return head;
    ListNode* current = head, *new_head;
    vector<ListNode*> list_vec;
    for (ListNode* current = head; current != NULL; current = current->next) {
        list_vec.push_back(current);
    }
    int size = list_vec.size();
    if (k > size) return head;
    bool first = true;
    for (int start = 0, end = start + k - 1;start < size && end < size; start += k, end += k) {
        int right = end;
        if (first) {
            while (right > start) {
                list_vec[right]->next = list_vec[right - 1];
                right--;
            }
            list_vec[0]->next = end < size - 1 ? list_vec[end + 1] : NULL;
            new_head = list_vec[end];
            first = false;
        }
        else
        {
            while (right > start) {
                list_vec[right]->next = list_vec[right - 1];
                right--;
            }
            list_vec[start]->next = end < size - 1 ? list_vec[end + 1] : NULL;
            list_vec[start - k]->next = list_vec[end];
        }
    }
    return new_head;
}

Problem 26 Remove Duplicates from Sorted Array

由于数组是排过序的,所以只要简单的遍历一遍,并对每个节点都检查是否和前一个数是否相同,如果相同则去掉,如果不同则将比较的数更新。

int removeDuplicates(vector<int>& nums) {
    if (nums.size() == 0) return 0;
    int length = 1;
    int before = nums[0];
    int size = nums.size();
    for (int i = 1; i < size;) {
        if (nums[i] != before) {
            before = nums[i];
            length++;
            i++;
        }
        else {
            size--;
            nums.erase(nums.begin() + i);
        }
    }
    return length;
}

Problem 27 Remove Element

这题删去数组中指定元素,非常简单,一次遍历即可完成。

int removeElement(vector<int>& nums, int val) {
    int size = nums.size();
    if (size == 0) return 0;
    for (int i = 0; i < size;) {
        if (nums[i] == val) {
            size--;
            nums.erase(nums.begin() + i);
        }
        else
            i++;
    }
    return nums.size();
}

Problem 28 Implement strStr()

strStr()的作用是返回字符串中第一次出现所查找字符串的下标。也是一个简单的遍历的过程,当出现和目标字符串第一个字符相匹配的位置时,开始向后遍历,检查是否是目标子串,如果是,返回当前位置,如果不是,继续寻找

int strStr(string haystack, string needle) {
    int hay_size = haystack.length(), needle_size = needle.length();
    if (needle_size == 0) return 0;
    for (int i = 0; i < hay_size; i++) {
        if (haystack[i] == needle[0]) {
            int current_hay = i, current_needle = 0;
            while (current_hay < hay_size && current_needle < needle_size) {
                if (haystack[current_hay] != needle[current_needle])
                    break;
                current_hay++;
                current_needle++;
            }
            if (current_needle == needle_size)
                return i;
        }
    }
    return -1;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值