一、1两数之和

二、15三数之和

C++ Soution 1:

 class Solution {
 public:
     vector<vector<int>> threeSum(vector<int>& nums)
     {
         vector<vector<int>> res;
         sort(nums.begin(), nums.end());
         ; k < nums.size(); ++k)
         {
             )
                 break;
              && nums[k] == nums[k - ])
                 continue;
              - nums[k];
             , j = nums.size() - ;
             while (i < j)
             {
                 if (nums[i] + nums[j] == target)
                 {
                     res.push_back({nums[k], nums[i], nums[j]});
                     ])
                         ++i;
                     ])
                         --j;
                     ++i;
                     --j;
                 }
                 else if (nums[i] + nums[j] < target)
                     ++i;
                 else
                     --j;
             }
         }
         return res;
     }
 };

  

三、16最接近的三数之和

题目:3Sum Closest

C++ Soution 1:

 class Solution {
 public:
     int threeSumClosest(vector<int>& nums, int target)
     {
         ] + nums[] + nums[];
         int diff = abs(closest - target);
         sort(nums.begin(), nums.end());
         ; i < nums.size() - ; ++i)
         {
             , right = nums.size() - ;
             while (left < right)
             {
                 int sum = nums[i] + nums[left] + nums[right];
                 int newDiff = abs(sum - target);
                 if (diff > newDiff)
                 {
                     diff = newDiff;
                     closest = sum;
                 }
                 if (sum < target)
                     ++left;
                 else
                     --right;
             }
         }
         return closest;
     }
 };

四、18四数之和

题目:4Sum

C++ Soution 1:

 class Solution {
 public:
     vector<vector<int>> fourSum(vector<int>& nums, int target)
     {
         vector<vector<int>> res;
         sort(nums.begin(), nums.end());
         if(nums.empty())
             return {};
         ; k < nums.size(); ++k)
         {
              && nums[k] == nums[k-])
                 continue;
             ; m < nums.size(); ++m)
             {
                 int sum = target - nums[k] -nums[m];
                  && nums[m] == nums[m -])
                     continue;
                 , j = nums.size() -;
                 while(i < j)
                 {
                     if(sum == nums[i] + nums[j])
                     {
                         res.push_back({nums[k], nums[m], nums[i], nums[j]});
                         ])
                             ++i;
                         ])
                             --j;
                         ++i;
                         --j;
                     }
                     else if(sum > nums[i] + nums[j])
                         ++i;
                     else
                         --j;
                 }
             }
         }
         return res;
     }
 };

五. 平方数之和

题目:633. Sum of Square Numbers

C++ Soution 1:双指针

 class Solution {
 public:
     bool judgeSquareSum(int c)
     {
         ;
         long long high = sqrt(c);
         while (low < high)
         {
             if (low*low + high * high == c)
                 return true;
             else if (low*low + high * high > c)
                 high--;
             else
                 low++;
         }
         return false;
     }
 };

367. 有效的完全平方数

题目:367. Valid Perfect Square

C++ Soution 1:

 class Solution {
 public:
     bool isPerfectSquare(int num)
     {
         ) return true;

         , right = num;//高低指针
         long long mid;
         while (left <= right)
         {
             mid = (left + right) / ;
             long long target = mid  * mid ; //防止超出int
             if (target == num)
                 return true;
             else if (target > num)
                 right = mid - ;  //大了,就降低高指针
             else
                 left = mid + ; //小了,升高低指针
         }
         return false;
     }
 };

【Leetcode | 5】求和问题-LMLPHP

50. Pow(x, n)

题目:Pow(x, n)

C++ Soution 1:

分析:迭代的解法,我们让i初始化为n,然后看i是否是2的倍数,是的话x乘以自己,否则res乘以x,i每次循环缩小一半,直到为0停止循环。最后看n的正负,如果为负,返回其倒数

 class Solution {
     public:
     double myPow(double x, int n)
     {
         double res = 1.0;
         ; i /= )
         {
              != )
                 res *= x;
             x *= x;
         }
          ?  / res : res;
     }
 };

【Leetcode | 5】求和问题-LMLPHP

05-11 13:57