Leetcode exercise notes and leetcode exercises
Start with the basic questions, write each question by category, and record questions and gains.
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input wocould haveExactlyOne solution, and you may not useSameElement twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums [0] + nums [1] = 2 + 7 = 9, return [0, 1].
Code:
Class Solution {
Public:
Vector <int> twoSum (vector <int> & nums, int target ){
Int I, j;
Vector <int> indices;
For (I = 0; I <nums. size ()-1; ++ I)
{
For (j = I + 1; j <nums. size (); ++ j)
{
If (I! = J)
{
If (nums [I] + nums [j] = target)
{
Indices. push_back (I );
Indices. push_back (j );
Return indices;
}
}
}
}
}
};
Click it for the first time and pass it first. The speed is not required for the moment. Algorithm knowledge is learned and supplemented.