leetcode-Day1-Array[35,66,119]

10 篇文章 0 订阅

题目:

  1. Search Insert Position

难度:Easy

Given a sorted array and a target value, return the index if the
target is found. If not, return the index where it would be if it were
inserted in order.

You may assume no duplicates in the array.

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

int Solution::searchInsert(vector<int>& nums, int target)
{
    for (int i = 0; i < nums.size(); i++)
    {
        if (nums.at(i) >= target)
        {
            return i;
        }

    }
    return nums.size();
}

收获:
1. vector的初始化方式:vector<int> nums = { 1, 3, 5, 6 }
2. vector的两种方法:size() at(i)得到vector的大小和访问vector的数据
3. 检验函数的调用方式:

Solution s;
int a = s.searchInsert(nums, 3);

题目
66. Plus One

难度:Easy

Given a non-empty array of digits representing a non-negative integer,
plus one to the integer.

The digits are stored such that the most significant digit is at the
head of the list, and each element in the array contain a single
digit.

You may assume the integer does not contain any leading zero, except
the number 0 itself.

Example:
Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.

vector<int> Solution66::plusOne(vector<int>& digits)
{
    vector<int>::iterator it = digits.end() - 1;//从后向前遍历
    while (it >= digits.begin())
    {
        if (*it == 9)//如果是9则进位
        {
            *it = 0;
            if (it != digits.begin())
                --it;//特别注意这里,防止数组越界
            else//如果迭代器到达begin位置,及时跳出循环,否则还将执行下一次循环,产生bug
                break;
        }
        else
        {
            *it += 1;//进位结束
            break;
        }
    }
    if (*it == 0)//第一个元素如果为0,则一定是进位产生,应在数组首位插入1
    {
        digits.insert(it, 1);
    }
    return digits;
}

收获:
1. 用迭代器遍历数组,访问数组元素,并利用迭代器修改数组元素
2. 利用迭代器很好的防止数组越界,一定要仔细确认边界,是否发生数组越界
3. vector的insert(vec.begin(),3,2)方法,在数组首位插入3个2


**题目:**119. Pascal’s Triangle II

难度:Easy

Given a non-negative index k where k ≤ 33, return the kth index row of
the Pascal’s triangle.

Note that the row index starts from 0.

Example:

Input: 3 Output: [1,3,3,1]

class Solution119
{
    int pascal[50][50];
    int pascal_num(int n, int k) {
        if (k == 0 || k == n)return 1;
        if (pascal[n][k] != -1)return pascal[n][k];

            pascal[n][k] = pascal_num(n - 1, k - 1) + pascal_num(n - 1, k);
            return pascal[n][k];

    }
public:
    vector<int> getRow(int rowIndex);
    Solution119();
    ~Solution119();
};
vector<int> Solution119::getRow(int rowIndex)
{
    memset(pascal, -1, sizeof(pascal));//数组初始化,没有被更新的位置为0
    vector<int> nums;//注意这里数组初始化,不要带元素个数
    for (int i = 0; i <= rowIndex; ++i)
    {
        int a = pascal_num(rowIndex, i);
        cout << i << ": " << a << endl;
        nums.push_back(a);

    }
    return nums;
}

收获:
1. 自己采用递归的方式,结果超时,采取讨论区的答案,以空间换时间,将中间计算结果存储到类的私有数组中
2. vector在初始化的时候如果采取一下方法:vector<int> a(3)则认为,初始化数组中前三个元素为0,结合push_back()方法,直接在后面插入
3. 私有函数与公有函数


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值