diff options
| author | crupest <crupest@outlook.com> | 2020-05-15 17:01:47 +0800 | 
|---|---|---|
| committer | crupest <crupest@outlook.com> | 2020-05-15 17:01:47 +0800 | 
| commit | 60d034ae1d9c699fcdb62918ccf820d662da4f3e (patch) | |
| tree | 19a2ee421e856bab8f56564ac338703078e022bc /works/solutions | |
| parent | 69e591089d5c64ee7b5958663be97cda74d7a1cd (diff) | |
| download | crupest-60d034ae1d9c699fcdb62918ccf820d662da4f3e.tar.gz crupest-60d034ae1d9c699fcdb62918ccf820d662da4f3e.tar.bz2 crupest-60d034ae1d9c699fcdb62918ccf820d662da4f3e.zip | |
import(solutions): Add problem 35 .
Diffstat (limited to 'works/solutions')
| -rw-r--r-- | works/solutions/cpp/35.cpp | 36 | 
1 files changed, 36 insertions, 0 deletions
| diff --git a/works/solutions/cpp/35.cpp b/works/solutions/cpp/35.cpp new file mode 100644 index 0000000..7da26c4 --- /dev/null +++ b/works/solutions/cpp/35.cpp @@ -0,0 +1,36 @@ +#include <vector>
 +
 +using std::vector;
 +
 +class Solution
 +{
 +public:
 +    int searchInsert(vector<int> &nums, int target)
 +    {
 +        if (nums.empty())
 +            return 0;
 +
 +        int left_index = 0;
 +        int right_index = nums.size();
 +
 +        while (left_index != right_index)
 +        {
 +            const int middle_index = (left_index + right_index) / 2;
 +            const int middle_value = nums[middle_index];
 +            if (target < middle_value)
 +            {
 +                right_index = middle_index;
 +            }
 +            else if (target > middle_value)
 +            {
 +                left_index = middle_index + 1;
 +            }
 +            else
 +            {
 +                return middle_index;
 +            }
 +        }
 +
 +        return left_index;
 +    }
 +};
 | 
