leetcode-DAY3-2018.7.24

10 篇文章 0 订阅

题目:27. Remove Element

Given an array nums and a value val, remove all instances of that value in-place and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

The order of elements can be changed. It doesn’t matter what you leave beyond the new length.

Example 1:

Given nums = [3,2,2,3], val = 3,

Your function should return length = 2, with the first two elements of nums being 2.

It doesn’t matter what you leave beyond the returned length.

/*
可以不考虑原来元素的顺序
时间复杂度:O(N)空间复杂度:O(1)
*/
int Solution27::removeElement(vector<int>& nums, int val)
{
    int n = nums.size();
    int i = 0;
    while (i < n)
    {
        if (nums[i] == val)
        {
            nums[i] = nums[n - 1];
            n--;
        }
        else
            i++;
    }
    return n;
}

收获:
1. 打破固有思维,不单单从前开始,也可以考虑对后面的元素进行操作
2. 题目按时可以不考虑元素顺序,就可以将后面的元素插入
3. 时间复杂度:O(n),空间复杂度:o(1)

题目:121. Best Time to Buy and Sell Stock

Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one.

Example 1:

Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Not 7-1 = 6, as selling price needs to be larger than buying price.

int Solution121::maxProfit(vector<int>& prices)
{
    if (prices.empty())
        return 0;
    int maxProfit = 0;//初始化相减的最大值为0
    int minPrice = prices[0];//初始化最小值得起始位置
    for (int i = 1; i < prices.size(); i++)
    {
        if (prices[i] > minPrice)//每遇到一个比最小值大得元素比较一次
        {
            maxProfit = std::max(maxProfit, prices[i] - minPrice);//更新最大利益值
        }
        else
            minPrice = prices[i];//最小值更新并没有影响,因为最大值利益已经存储在max中,如果更换最小值即使没有更大得差值,返回值仍然使原来得
    }
    return maxProfit;
}

收获:
1. 这个题面试的时候遇见过,解决方法使报错计算得到的最大值,和记录当前的最小值,只有满足最大值索引大于最小值索引的时候再判断max

题目:26. Remove Duplicates from Sorted Array

Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

Example 1:

Given nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.

It doesn’t matter what you leave beyond the returned length.

int Solution26::removeDuplicates(vector<int>& nums)
{
    if (nums.size() < 2)
        return nums.size();
    int i = 0;
    int j = 1;
    while (j < nums.size())
    {
        if (nums[j] == nums[i])
        {
            ++j;
        }
        else
        {
            nums[i + 1] = nums[j];
            ++i;
            ++j;
        }
    }
    return i+1;
}

收获:
1. 两个指针遍历数组,相同元素前一个指针前移,不相同时,将后一个指针的下一个位置赋值

题目:88. Merge Sorted Array

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

Note:

The number of elements initialized in nums1 and nums2 are m and n respectively.
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.
Example:

Input:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3

Output: [1,2,2,3,5,6]

void Solution88::merge(vector<int>& nums1, int m, vector<int>& nums2, int n)
{
    int x = m + n - 1;
    --m; --n;
    while (m >= 0 && n >= 0)
    {
        nums1[x--] = (nums1[m] > nums2[n]) ? nums1[m--] : nums2[n--];
    }
    while (n >= 0)
    {
        nums1[x--] = nums2[n--];
    }
}

收获:
1. 从最后一个位置开始赋值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值