# 167-Two Sum II - Input Array Is Sorted ###### tags: `Easy` ## Question https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ ## Key 1. Two pointers ## Reference ## Solution ```cpp= class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { if(nums.size() < 2) return {}; int l = 0, r = nums.size()-1; while(l <= r) { if(nums[l] + nums[r] == target) { // index start from 1, so we need to add 1 return {l+1, r+1}; } else if(nums[l] + nums[r] > target) r--; else if(nums[l] + nums[r] < target) l++; } return {}; } }; ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up