Leetcode Note

算法刷题笔记

Leetcode-11. Container With Most Water

Method: (对撞指针)每次保留两指针中最大的那个即可求得最大的面积
Runtime: 16 ms, faster than 95.65% of C++ online submissions for Container With Most Water.
Memory Usage: 9.9 MB, less than 43.30% of C++ online submissions for Container With Most Water.

class Solution {
public:
    int maxArea(vector<int>& height) {
        int i = 0;
        int r = height.size()-1;
        long long max = -1;
        while (i<r)
        {
            long long area = (r - i)*min(height[r], height[i]);
            if (area > max) max = area;
            if (height[r] > height[i]) i++;
            else r--;
        }
        return max;
    }
};

Leetcode-26 Remove Duplicates from Sorted Array

Method: temp 作为一个标志表示遍历过的数,用temp来比较是否重复。
Runtime: 16 ms, faster than 99.08% of C++ online submissions for Remove Duplicates from Sorted Array.
Memory Usage: 9.8 MB, less than 97.50% of C++ online submissions for Remove Duplicates from Sorted Array.

    int removeDuplicates(vector<int>& nums) {
        int length = 0;
        int temp = 99999;
        for (int i = 0;i < nums.size();i++) {
            if (nums[i] != temp) {
                nums[length++] = nums[i];
                temp = nums[i];
            }
        }
        return length;
    }

Leetcode-27 Remove Element

Method: k为非val值的个数, 查询到一个重复值$‘i’$则继续往下遍历,若非val值则进行赋值。
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Remove Element.
Memory Usage: 8.5 MB, less than 97.06% of C++ online submissions for Remove Element.

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

Leetcode-75 Sort Color

Method

  1. 可以采用计数排序,统计0,1,2个数然后按数量插入就好。
    Runtime: 4 ms, faster than 68.84% of C++ online submissions for Sort Colors.
    Memory Usage: 8.5 MB, less than 98.25% of C++ online submissions for Sort Colors.
    void sortColors(vector<int>& nums) {
        const int n = 3;
        int count[n] = { 0 };
        for (int i = 0;i < nums.size();i++) {
            count[nums[i]]++;
        }
        int index = 0;
        for (int i = 0;i < n;i++) {
            for (int j = 0;j < count[i];j++) nums[index++] = i;
        }   
    }
  1. 三路快排方法实现,只需要执行一次三路快排,遍历一遍把输入序列分成以下三块:arr[0 ... zero] == 0 / arr[zero+1 ... i-1] == 1 / arr[two ... n-1] == 2
    Alt text
    Runtime: 0 ms, faster than 100.00% of C++ online submissions for Sort Colors.
    Memory Usage: 8.5 MB, less than 96.49% of C++ online submissions for Sort Colors
    void sortColors(vector<int>& nums) {
        int zero = -1;  //nums[0 .. zero] == 0
        int two = nums.size(); // nums[two..-n-1] == 2
        for (int i = 0;i < two;) {
            if (nums[i] == 1)
                i++;
            else if (nums[i] == 2) {
                two--;
                swap(nums[i], nums[two]);
            }
            else {// nums[i] == 0
                zero++;
                swap(nums[zero], nums[i]);
                i++;
            }
        }
    }

Leetcode-80 Remove Duplicates from Sorted Array II

Method: @k 为合法数字的数量,@t 判断一类数字是否<2, @temp 用于比较的数字的拷贝, 迭代位置与合法数字的位置做交换,一次遍历即可完成。
Runtime: 8 ms, faster than 99.12% of C++ online submissions for Remove Duplicates from Sorted Array II.
Memory Usage: 8.8 MB, less than 89.47% of C++ online submissions for Remove Duplicates from Sorted Array II.

int removeDuplicates(vector<int>& nums) {
        int k = 0;
        bool t = true;
        int temp = 99999;
        for (int i = 0;i < nums.size();i++) {
            if (nums[i] == temp) {
                if (t == true) continue;
                else {
                    t = true;
                    if (k != i) {
                        nums[k] = nums[i];
                    }
                    k++;
                }
            }
            else {
                t = false;
                temp = nums[i];
                if (k != i) {
                    nums[k] = nums[i];
                }
                k++;
            }
        }

        return k;
    }

Leetcode-88 Merge Sorted Array

Method: 归并排序,设置一个外置数组用以空间换时间,时间为遍历一遍+拷贝长数组一遍的时间。
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Merge Sorted Array.
Memory Usage: 8.7 MB, less than 80.43% of C++ online submissions for Merge Sorted Array.、

void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        int p1 = 0, p2 = 0;
        vector<int> temp;
        for (int i = 0;i < m;i++) {
            temp.push_back(nums1[i]);
        }
        int index = 0;
        while (p1 < m && p2 < n)
        {
            if (temp[p1] <= nums2[p2]) {
                if (index != p1) 
                    nums1[index] = temp[p1];
                index++;
                p1++;
            }
            else {
                nums1[index++] = nums2[p2++];
            }
        }
        if (p1 != m) for (;p1 < m;p1++) nums1[index++] = temp[p1];
        else for (;p2 < n;p2++) nums1[index++] = nums2[p2];
    }

Leetcode-125. Valid Palindrome

Method: (对撞指针)先判断字符的有效性,再判断是否要转换大小写,最后在相同类型的文字格式下进行字符的比较。
Runtime: 12 ms, faster than 39.15% of C++ online submissions for Valid Palindrome.
Memory Usage: 9.3 MB, less than 81.63% of C++ online submissions for Valid Palindrome.

    bool isPalindrome(string s) {
        int p = 0;
        int r = s.length() - 1;
        while (p<r)
        {
            if ((s[p] >= 'A'&& s[p] <= 'Z') || (s[p] >= 'a'&&s[p] <= 'z') || (s[p] >= '0'&&s[p] <= '9')) {
                if ((s[r] >= 'A'&& s[r] <= 'Z') || (s[r] >= 'a'&&s[r] <= 'z') || (s[r] >= '0'&&s[r] <= '9')) {
                    char c = s[p]<'A'?s[p]:(s[p] >= 'a' ? s[p] : s[p] - 'A' + 'a');
                    char k = s[r]<'A' ? s[r] : (s[r] >= 'a' ? s[r] : s[r] - 'A' + 'a');
                    if (c != k) {
                        return false;
                    }
                    else {
                        p++;
                        r--;
                    }
                }
                else {
                    r--;
                    continue;
                }
            }
            else {
                p++;
            }
        }
        return true;
    }

Leetcode-167 Two Sum II - Input array is sorted(*)
Method:

  1. (二分查找)从起始点开始遍历,在除了遍历元素$num[i]$外的有序数组结构中采用二分搜索的方法查找$target-nums[i]$可以使时间复杂度控制在$O(nlogn)$.
  2. (对撞指针)前后各一个位置的指针,计算
    $ nums[i] + nums[j] < target ——> i++ $
    $ nums[i] + nums[j] > target ——> j++ $
    只需要一次遍历即可找到答案
    Runtime: 4 ms, faster than 96.98% of C++ online submissions for Two Sum II - Input array is sorted.
    Memory Usage: 9.5 MB, less than 82.35% of C++ online submissions for Two Sum II - Input array is sorted.
vector<int> twoSum(vector<int>& numbers, int target) {
    
    assert(numbers.size() >= 2);
    int l = 0, r = numbers.size() - 1;
    while (l < r)
    {
        if (numbers[l] + numbers[r] == target) {
            int res[2] = { l + 1, r + 1 };
            return vector<int>(res, res + 2);
        }
        else if (numbers[l] + numbers[r] < target) l++;
        else r--; //numbers[l] + numbers[r] > target
    }

    throw invalid_argument("The input has no solution.");
}

Leetcode-215. Kth Largest Element in an Array()
Method:
快速排序)运用快排partition操作 返回标定点位置,我们能够知道,标定点的位置是每一轮快排过后确定的最终位置因此只要 标定点位置p与想要查找的位置k相重则说明标定点已经排序完成,只需要返回标定点在数组中的值就好了。

Runtime: 28 ms, faster than 29.31% of C++ online submissions for Kth Largest Element in an Array.
Memory Usage: 11 MB, less than 6.06% of C++ online submissions for Kth Largest Element in an Array.

int _partition(vector<int>& arr, int l, int r) {
        int temp = arr[l];
        int i = l + 1, j = r;
        while (true)
        {
            while (i <= r && arr[i] > temp)i++;
            while (j >= l+1 && arr[j] < temp)j--;
            if (i > j) break;
            swap(arr[i], arr[j]);
            i++;
            j--;
        }

        swap(arr[l], arr[j]);

        return j;
    }
    int __quickSort(vector<int>& arr, int l, int r, int k) {
        int p = _partition(arr, l, r);
        if (p != k-1) {
            if(p > k-1)
                return __quickSort(arr, l, p - 1, k);
            else return __quickSort(arr, p + 1, r, k);
        }
        else
            return arr[p];
    }

    int findKthLargest(vector<int>& nums, int k) {
        return __quickSort(nums, 0, nums.size()-1, k);
    }

Leetcode-283 Move Zeroes

Method:主要用了一个标定点** $i$ 来标识一轮遍历过程中的非零数的个数,然后将每个非零数按$i$的位置存放。时间复杂度$O(n)$;空间复杂度$O(1)$。
Others Method:** 使用交换思想,只需要一轮遍历.
Runtime: 12 ms, faster than 96.77% of C++ online submissions for Move Zeroes.
Memory Usage: 9.3 MB, less than 100.00% of C++ online submissions for Move Zeroes.

void moveZeroes(vector<int>& nums) {
    int i = 0;
    for (int j = 0;j < nums.size();j++) {
        if (nums[j] != 0) {
            nums[i++] = nums[j];
        }
    }
    for (;i < nums.size();i++) {
        nums[i] = 0;
    }
}

//交换方法
void moveZeroes(vector<int>& nums) {
    int i = 0;
    for (int j = 0;j < nums.size();j++) {
        if (nums[j] != 0) {
            if (i != j) {
                int temp = nums[i];
                nums[i++] = nums[j];
                nums[j] = temp;
            }
            else //i==k
                i++;
        }
    }
}

Leetcode 344. Reverse String

Method:(对撞指针)简单用法。
Runtime: 44 ms, faster than 92.20% of C++ online submissions for Reverse String.
Memory Usage: 15.2 MB, less than 86.59% of C++ online submissions for Reverse String.

    void reverseString(vector<char>& s) {
        int p = 0;
        int r = s.size()-1;
        while (p < r)
        {
            swap(s[p++], s[r--]);

        }
    }

Leetcode 345. Reverse Vowels of a String

Method:(对撞指针)简单用法。
Runtime: 4 ms, faster than 99.53% of C++ online submissions for Reverse Vowels of a String.
Memory Usage: 10 MB, less than 87.88% of C++ online submissions for Reverse Vowels of a String

class Solution {
public:
    string reverseVowels(string s) {
        int p = 0;
        int r = s.length()-1;
        while (p < r)
        {   
            if (s[p] == 'a' || s[p] == 'A' || s[p] == 'e' || s[p] == 'E' || s[p] == 'i' || s[p] == 'I' || s[p] == 'o' || s[p] == 'O'
                || s[p] == 'u' || s[p] == 'U')
                if (s[r] == 'a' || s[r] == 'A' || s[r] == 'e' || s[r] == 'E' || s[r] == 'i' || s[r] == 'I' || s[r] == 'o' || s[r] == 'O'
                    || s[r] == 'u' || s[r] == 'U')
                    swap(s[p++], s[r--]);
                else
                    r--;
            else
                p++;

        }
        return s;
    }
};

转载于:https://www.cnblogs.com/simon-slrn/p/11386553.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值