LeetCode題庫-兩數之和

LeetCode題庫

 兩數之和:如果兩數之和等於target,則返回兩數下標——容器使用、元素檢索

/*
 * @lc app=leetcode.cn id=1 lang=cpp
 *
 * [1] 兩數之和
 * my mind: tagert-a=b,b如果在該數組中,則返回這兩個標籤
 */

// @lc code=start
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) 
    {
        int count = nums.size();
        vector<int> label;
        // tagert-a=b,b如果在該數組中,則返回這兩個標籤
        for (size_t i = 0; i < count; i++)
        {
            /* code: */
            int target_B = target - nums[i];            
            for (size_t j = i + 1; j < count; j++)
            {
                /* code */
                if (target_B == nums[j]/* condition */)
                {
                    label.push_back(i);
                    label.push_back(j);
                    return label; 

                    /* code */
                }                
            } 
        } 
        return label; 
    }
};
// @lc code=end

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章