diff options
| author | crupest <crupest@outlook.com> | 2020-05-11 23:42:04 +0800 | 
|---|---|---|
| committer | crupest <crupest@outlook.com> | 2020-05-11 23:42:04 +0800 | 
| commit | 7a25e555122e7f5ddbb225dde8f66fa312552eee (patch) | |
| tree | 4ee2a2b509d9d8440631d5d13342b79cc0d70ae4 /works | |
| parent | bd0f87150d82eb4370b8502ff2d2a221e69545a6 (diff) | |
| download | crupest-7a25e555122e7f5ddbb225dde8f66fa312552eee.tar.gz crupest-7a25e555122e7f5ddbb225dde8f66fa312552eee.tar.bz2 crupest-7a25e555122e7f5ddbb225dde8f66fa312552eee.zip  | |
import(solutions): Add problem 167 .
Diffstat (limited to 'works')
| -rw-r--r-- | works/solutions/cpp/167.cpp | 28 | 
1 files changed, 28 insertions, 0 deletions
diff --git a/works/solutions/cpp/167.cpp b/works/solutions/cpp/167.cpp new file mode 100644 index 0000000..05317b4 --- /dev/null +++ b/works/solutions/cpp/167.cpp @@ -0,0 +1,28 @@ +#include <vector>
 +
 +using std::vector;
 +
 +class Solution
 +{
 +public:
 +    vector<int> twoSum(vector<int> &numbers, int target)
 +    {
 +        int left = 0, right = numbers.size() - 1;
 +        while (true)
 +        {
 +            const auto sum = numbers[left] + numbers[right];
 +            if (sum < target)
 +            {
 +                left++;
 +            }
 +            else if (sum > target)
 +            {
 +                right--;
 +            }
 +            else
 +            {
 +                return {left + 1, right + 1};
 +            }
 +        }
 +    }
 +};
  | 
