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 | 5ee9a2871769503099a5fab0a4ec70a109295d15 (patch) | |
tree | b583b602e6ea03bd7daacb0a8ee8488d1190ffaf | |
parent | 558ba2d0adf8ad52a2c222e4722d5b71159a3764 (diff) | |
download | solutions-5ee9a2871769503099a5fab0a4ec70a109295d15.tar.gz solutions-5ee9a2871769503099a5fab0a4ec70a109295d15.tar.bz2 solutions-5ee9a2871769503099a5fab0a4ec70a109295d15.zip |
Add problem 167 .
-rw-r--r-- | cpp/167.cpp | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/cpp/167.cpp b/cpp/167.cpp new file mode 100644 index 0000000..05317b4 --- /dev/null +++ b/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};
+ }
+ }
+ }
+};
|