#leetcode 1

This is my the second writing, but i use the method of brute force.

I have write this problem in the afternoon, and i begin writing the solution

in the afternoon, but i want to learn the second way to handle this problem

by using hashing, and i learn this topic by watching houjie stl course video

and search stl topic by google.Now, it's half of hour to tomorrow, and i do

not use stl, because i search something like recurtion, and dive it deeply.

I want to say, there is not new way to learn new things, what is the best w-

ay to learn something faster and better?

Let me look the description of the problem.

It is very similar to leetcode 167, and this problem's array is not increas-

ing, and not to use meeting-two-pointer, and to use fast and low pointers to

solve this problem, By using two loops, and then complexity of time is O(n^2).

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> s(2,-1);
        for (int i = 0; i < nums.size(); ++i)
            for (int j = i + 1; j < nums.size(); ++j)
                if (nums[i] + nums[j] == target)
                {
                    s[0] = i;
                    s[1] = j;
                    return s;
                }
        return s;
    }
};
posted @ 2022-02-01 23:47  越菜越自信  阅读(23)  评论(0)    收藏  举报