当前位置:   article > 正文

力扣C++|一题多解之数学题专场(2)

力扣C++|一题多解之数学题专场(2)

目录

50. Pow(x, n)

60. 排列序列

66. 加一

67. 二进制求和

69. x 的平方根


50. Pow(x, n)

实现 pow(x,n),即计算 x 的 n 次幂函数(即x^n)。

示例 1:

输入:x = 2.00000, n = 10
输出:1024.00000

示例 2:

输入:x = 2.10000, n = 3
输出:9.26100

示例 3:

输入:x = 2.00000, n = -2
输出:0.25000
解释:2^(-2) = (1/2)^2 = 1/4 = 0.25

提示:

  • -100.0 < x < 100.0
  • -2^31 <= n <= 2^31-1
  • -10^4 <= x^n <= 10^4

代码1:  

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class Solution
  4. {
  5. public:
  6. double myPow(double x, int n)
  7. {
  8. if (n == 0)
  9. return 1;
  10. if (n % 2 == 1)
  11. {
  12. double temp = myPow(x, n / 2);
  13. return temp * temp * x;
  14. }
  15. else if (n % 2 == -1)
  16. {
  17. double temp = myPow(x, n / 2);
  18. return temp * temp / x;
  19. }
  20. else
  21. {
  22. double temp = myPow(x, n / 2);
  23. return temp * temp;
  24. }
  25. }
  26. };
  27. int main()
  28. {
  29. Solution s;
  30. cout << s.myPow(2.00000, 10) << endl;
  31. cout << s.myPow(2.10000, 3) << endl;
  32. cout << s.myPow(2.0000, -2) << endl;
  33. return 0;
  34. }

代码2:  

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class Solution
  4. {
  5. public:
  6. double helper(double x, int n)
  7. {
  8. if (n == 0)
  9. return 1.0;
  10. double y = helper(x, n / 2);
  11. return n % 2 == 0 ? y * y : y * y * x;
  12. }
  13. double myPow(double x, int n)
  14. {
  15. long long N = static_cast<long long>(n);
  16. if (N == 0)
  17. return 1;
  18. return N > 0 ? helper(x, N) : 1. / helper(x, -N);
  19. }
  20. };
  21. int main()
  22. {
  23. Solution s;
  24. cout << s.myPow(2.00000, 10) << endl;
  25. cout << s.myPow(2.10000, 3) << endl;
  26. cout << s.myPow(2.0000, -2) << endl;
  27. return 0;
  28. }

代码3:  

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class Solution
  4. {
  5. public:
  6. double myPow(double x, int n)
  7. {
  8. if (n == INT_MIN)
  9. {
  10. double t = dfs(x, -(n / 2));
  11. return 1 / t * 1 / t;
  12. }
  13. else
  14. {
  15. return n < 0 ? 1 / dfs(x, -n) : dfs(x, n);
  16. }
  17. }
  18. private:
  19. double dfs(double x, int n)
  20. {
  21. if (n == 0)
  22. {
  23. return 1;
  24. }
  25. else if (n == 1)
  26. {
  27. return x;
  28. }
  29. else
  30. {
  31. double t = dfs(x, n / 2);
  32. return (n % 2) ? (x * t * t) : (t * t);
  33. }
  34. }
  35. };
  36. int main()
  37. {
  38. Solution s;
  39. cout << s.myPow(2.00000, 10) << endl;
  40. cout << s.myPow(2.10000, 3) << endl;
  41. cout << s.myPow(2.0000, -2) << endl;
  42. return 0;
  43. }

输出:

1024
9.261
0.25 


60. 排列序列

给出集合 [1,2,3,...,n],其所有元素共有 n! 种排列。

按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下:

  1. "123"
  2. "132"
  3. "213"
  4. "231"
  5. "312"
  6. "321"

给定 n 和 k,返回第 k 个排列。

示例 1:

输入:n = 3, k = 3
输出:"213"

示例 2:

输入:n = 4, k = 9
输出:"2314"

示例 3:

输入:n = 3, k = 1
输出:"123"

提示:

  • 1 <= n <= 9
  • 1 <= k <= n!

代码1:   

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class Solution
  4. {
  5. public:
  6. string getPermutation(int n, int k)
  7. {
  8. string ans;
  9. vector<bool> st(n + 1);
  10. for (int i = 1; i <= n; i++)
  11. {
  12. int f = 1;
  13. for (int j = n - i; j >= 1; j--)
  14. f *= j;
  15. for (int j = 1; j <= n; j++)
  16. {
  17. if (!st[j])
  18. {
  19. if (k <= f)
  20. {
  21. ans += to_string(j);
  22. st[j] = 1;
  23. break;
  24. }
  25. k -= f;
  26. }
  27. }
  28. }
  29. return ans;
  30. }
  31. };
  32. int main()
  33. {
  34. Solution s;
  35. cout << s.getPermutation(3, 3) << endl;
  36. cout << s.getPermutation(4, 9) << endl;
  37. cout << s.getPermutation(3, 1) << endl;
  38. return 0;
  39. }

代码2:   

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class Solution
  4. {
  5. public:
  6. vector<string> res;
  7. string getPermutation(int n, int k)
  8. {
  9. string track;
  10. traverse(track, n);
  11. return res[k - 1];
  12. }
  13. void traverse(string &track, int n)
  14. {
  15. if (track.size() == n)
  16. {
  17. res.push_back(track);
  18. return;
  19. }
  20. for (int i = 1; i <= n; i++)
  21. {
  22. char c = i + '0';
  23. if (find(track.begin(), track.end(), c) != track.end())
  24. continue;
  25. track.push_back(c);
  26. traverse(track, n);
  27. track.pop_back();
  28. }
  29. }
  30. };
  31. int main()
  32. {
  33. Solution s;
  34. cout << s.getPermutation(3, 3) << endl;
  35. cout << s.getPermutation(4, 9) << endl;
  36. cout << s.getPermutation(3, 1) << endl;
  37. return 0;
  38. }

代码3:   

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class Solution
  4. {
  5. public:
  6. int th;
  7. string ans;
  8. string getPermutation(int n, int k)
  9. {
  10. string s;
  11. vector<bool> vec(9, false);
  12. this->th = 0;
  13. backtrack(n, k, s, vec);
  14. return ans;
  15. }
  16. bool backtrack(int n, int k, string &s, vector<bool> &vec)
  17. {
  18. if (s.length() == n)
  19. {
  20. if (++th == k)
  21. {
  22. ans = s;
  23. return true;
  24. }
  25. }
  26. for (char c = '1'; c <= '1' + n - 1; c++)
  27. {
  28. if (vec[c - '1'])
  29. continue;
  30. s.push_back(c);
  31. vec[c - '1'] = true;
  32. if (backtrack(n, k, s, vec))
  33. return true;
  34. s.pop_back();
  35. vec[c - '1'] = false;
  36. }
  37. return false;
  38. }
  39. };
  40. int main()
  41. {
  42. Solution s;
  43. cout << s.getPermutation(3, 3) << endl;
  44. cout << s.getPermutation(4, 9) << endl;
  45. cout << s.getPermutation(3, 1) << endl;
  46. return 0;
  47. }

输出:

213
2314
123 


66. 加一

给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一。

最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。

你可以假设除了整数 0 之外,这个整数不会以零开头。

示例 1:

输入:digits = [1,2,3]
输出:[1,2,4]
解释:输入数组表示数字 123。

示例 2:

输入:digits = [4,3,2,1]
输出:[4,3,2,2]
解释:输入数组表示数字 4321。

示例 3:

输入:digits = [0]
输出:[1]

提示:

  • 1 <= digits.length <= 100
  • 0 <= digits[i] <= 9

代码1:   

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class Solution
  4. {
  5. public:
  6. vector<int> plusOne(vector<int> &digits)
  7. {
  8. int len = digits.size() - 1;
  9. for (int i = len; i >= 0; i--)
  10. {
  11. if ((digits[i] + 1 == 10 && i == len) || digits[i] >= 10)
  12. {
  13. digits[i] = 0;
  14. if (i == 0)
  15. {
  16. digits.insert(digits.begin(), 1);
  17. }
  18. else
  19. {
  20. digits[i - 1] += 1;
  21. }
  22. }
  23. else
  24. {
  25. if (i == len)
  26. {
  27. digits[i] += 1;
  28. }
  29. break;
  30. }
  31. }
  32. return digits;
  33. }
  34. };
  35. int main()
  36. {
  37. Solution s;
  38. vector<int> sum;
  39. vector<vector<int>> digits = {{1,2,3},{4,3,2,1},{0},{9,9,9}};
  40. for (auto digit:digits){
  41. sum = s.plusOne(digit);
  42. copy(sum.begin(), sum.end(), ostream_iterator<int>(cout, " "));
  43. cout << endl;
  44. }
  45. return 0;
  46. }

代码2:   

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class Solution
  4. {
  5. public:
  6. vector<int> plusOne(vector<int> &digits)
  7. {
  8. int i = 0;
  9. int size = digits.size();
  10. for (i = size - 1; i >= 0; i--)
  11. {
  12. digits[i]++;
  13. digits[i] = digits[i] % 10;
  14. if (digits[i] != 0)
  15. return digits;
  16. }
  17. if (i == -1)
  18. {
  19. digits.insert(digits.begin(), 1);
  20. digits[size] = 0;
  21. }
  22. return digits;
  23. }
  24. };
  25. int main()
  26. {
  27. Solution s;
  28. vector<int> sum;
  29. vector<vector<int>> digits = {{1,2,3},{4,3,2,1},{0},{9,9,9}};
  30. for (auto digit:digits){
  31. sum = s.plusOne(digit);
  32. copy(sum.begin(), sum.end(), ostream_iterator<int>(cout, " "));
  33. cout << endl;
  34. }
  35. return 0;
  36. }

代码3:   

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class Solution
  4. {
  5. public:
  6. vector<int> plusOne(vector<int> &digits)
  7. {
  8. int len = digits.size() - 1;
  9. for (; len > 0 && digits[len] == 9; --len)
  10. {
  11. digits[len] = 0;
  12. }
  13. if (len == 0 && digits[0] == 9)
  14. {
  15. digits[0] = 0;
  16. digits.insert(digits.begin(), 1);
  17. }
  18. else
  19. {
  20. ++digits[len];
  21. }
  22. return digits;
  23. }
  24. };
  25. int main()
  26. {
  27. Solution s;
  28. vector<int> sum;
  29. vector<vector<int>> digits = {{1,2,3},{4,3,2,1},{0},{9,9,9}};
  30. for (auto digit:digits){
  31. sum = s.plusOne(digit);
  32. copy(sum.begin(), sum.end(), ostream_iterator<int>(cout, " "));
  33. cout << endl;
  34. }
  35. return 0;
  36. }

输出:

1 2 4
4 3 2 2
1
1 0 0 0 


67. 二进制求和

给你两个二进制字符串,返回它们的和(用二进制表示)。

输入为 非空 字符串且只包含数字 1 和 0

示例 1:

输入: a = "11", b = "1"
输出: "100"

示例 2:

输入: a = "1010", b = "1011"
输出: "10101"

提示:

  • 每个字符串仅由字符 '0' 或 '1' 组成。
  • 1 <= a.length, b.length <= 10^4
  • 字符串如果不是 "0" ,就都不含前导零。

代码1:

  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. using namespace std;
  5. class Solution
  6. {
  7. public:
  8. string addBinary(string a, string b) {
  9. string result;
  10. int carry = 0;
  11. int i = a.length() - 1, j = b.length() - 1;
  12. while (i >= 0 || j >= 0 || carry != 0) {
  13. int sum = carry;
  14. if (i >= 0) {
  15. sum += a[i--] - '0';
  16. }
  17. if (j >= 0) {
  18. sum += b[j--] - '0';
  19. }
  20. result.push_back(sum % 2 + '0');
  21. carry = sum / 2;
  22. }
  23. reverse(result.begin(), result.end());
  24. return result;
  25. }
  26. };
  27. int main()
  28. {
  29. Solution s;
  30. cout << s.addBinary("11", "1") << endl;
  31. cout << s.addBinary("1010", "1011") << endl;
  32. cout << s.addBinary("1111", "11111") << endl;
  33. cout << s.addBinary("1100", "110111") << endl;
  34. return 0;
  35. }

代码2:  

  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. using namespace std;
  5. class Solution
  6. {
  7. public:
  8. string addBinary(string a, string b)
  9. {
  10. int sum = 0;
  11. string res;
  12. int p = 0;
  13. int i = a.length() - 1, j = b.length() - 1;
  14. while (i >= 0 || j >= 0 || sum != 0)
  15. {
  16. if (i >= 0) {
  17. sum += a[i--] - '0';
  18. }
  19. if (j >= 0) {
  20. sum += b[j--] - '0';
  21. }
  22. p = sum % 2;
  23. sum /= 2;
  24. res += to_string(p);
  25. }
  26. reverse(res.begin(), res.end());
  27. return res;
  28. }
  29. };
  30. int main()
  31. {
  32. Solution s;
  33. cout << s.addBinary("11", "1") << endl;
  34. cout << s.addBinary("1010", "1011") << endl;
  35. cout << s.addBinary("1111", "11111") << endl;
  36. cout << s.addBinary("1100", "110111") << endl;
  37. return 0;
  38. }

代码3:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class Solution
  4. {
  5. public:
  6. string addBinary(string a, string b)
  7. {
  8. if (b.size() > a.size())
  9. {
  10. string temp = b;
  11. b = a;
  12. a = temp;
  13. }
  14. int i = a.size() - 1;
  15. int j = b.size() - 1;
  16. if (i != j)
  17. {
  18. for (int k = 0; k < i - j; k++)
  19. b = "0" + b;
  20. }
  21. int count = 0;
  22. for (int k = i; k >= 0; k--)
  23. {
  24. if (a[k] - '0' + b[k] - '0' + count == 0)
  25. {
  26. a[k] = '0';
  27. count = 0;
  28. }
  29. else if (a[k] - '0' + b[k] - '0' + count == 1)
  30. {
  31. a[k] = '1';
  32. count = 0;
  33. }
  34. else if (a[k] - '0' + b[k] - '0' + count == 3)
  35. {
  36. a[k] = '1';
  37. count = 1;
  38. }
  39. else
  40. {
  41. a[k] = '0';
  42. count = 1;
  43. }
  44. }
  45. if (count == 1)
  46. a = '1' + a;
  47. return a;
  48. }
  49. };
  50. int main()
  51. {
  52. Solution s;
  53. cout << s.addBinary("11", "1") << endl;
  54. cout << s.addBinary("1010", "1011") << endl;
  55. cout << s.addBinary("1111", "11111") << endl;
  56. cout << s.addBinary("1100", "110111") << endl;
  57. return 0;
  58. }

代码4:   

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class Solution
  4. {
  5. public:
  6. string addBinary(string a, string b)
  7. {
  8. string result = "", rr = "";
  9. char aa, bb;
  10. int l1 = a.length(), l2 = b.length(), i = l1 - 1, j = l2 - 1, carry = 0, sum = 0;
  11. while (true)
  12. {
  13. if (i < 0)
  14. aa = '0';
  15. else
  16. aa = a[i];
  17. if (j < 0)
  18. bb = '0';
  19. else
  20. bb = b[j];
  21. sum = (aa - '0') + (bb - '0') + carry;
  22. result += ((sum % 2) + '0');
  23. carry = sum / 2;
  24. j--;
  25. i--;
  26. if (i < 0 && j < 0)
  27. {
  28. if (carry == 1)
  29. result += "1";
  30. break;
  31. }
  32. }
  33. int l3 = result.length();
  34. for (int i = l3 - 1; i >= 0; i--)
  35. rr += result[i];
  36. return rr;
  37. }
  38. };
  39. int main()
  40. {
  41. Solution s;
  42. cout << s.addBinary("11", "1") << endl;
  43. cout << s.addBinary("1010", "1011") << endl;
  44. cout << s.addBinary("1111", "11111") << endl;
  45. cout << s.addBinary("1100", "110111") << endl;
  46. return 0;
  47. }

输出: 

100
10101
101110
1000011 


69. x 的平方根

实现 int sqrt(int x) 函数。

计算并返回 x 的平方根,其中 是非负整数。

由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。

示例 1:

输入: 4
输出: 2

示例 2:

输入: 8
输出: 2
说明: 8 的平方根是 2.82842..., 由于返回类型是整数,小数部分将被舍去。

代码1:   

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class Solution
  4. {
  5. public:
  6. int mySqrt(int x)
  7. {
  8. long long i = 0;
  9. long long j = x / 2 + 1;
  10. while (i <= j)
  11. {
  12. long long mid = (i + j) / 2;
  13. long long res = mid * mid;
  14. if (res == x)
  15. return mid;
  16. else if (res < x)
  17. i = mid + 1;
  18. else
  19. j = mid - 1;
  20. }
  21. return j;
  22. }
  23. };
  24. int main()
  25. {
  26. Solution s;
  27. cout << s.mySqrt(4) << endl;
  28. cout << s.mySqrt(8) << endl;
  29. cout << s.mySqrt(121) << endl;
  30. cout << s.mySqrt(120) << endl;
  31. cout << s.mySqrt(122) << endl;
  32. return 0;
  33. }

代码2:   

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class Solution
  4. {
  5. public:
  6. int mySqrt(int x)
  7. {
  8. if (x == 0)
  9. return 0;
  10. double last = 0;
  11. double res = 1;
  12. while (res != last)
  13. {
  14. last = res;
  15. res = (res + x / res) / 2;
  16. }
  17. return int(res);
  18. }
  19. };
  20. int main()
  21. {
  22. Solution s;
  23. cout << s.mySqrt(4) << endl;
  24. cout << s.mySqrt(8) << endl;
  25. cout << s.mySqrt(121) << endl;
  26. cout << s.mySqrt(120) << endl;
  27. cout << s.mySqrt(122) << endl;
  28. return 0;
  29. }

代码3:   

  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4. class Solution
  5. {
  6. public:
  7. int mySqrt(int x) {
  8. if (x <= 1) {
  9. return x;
  10. }
  11. int left = 1;
  12. int right = x;
  13. while (left <= right) {
  14. int mid = left + (right - left) / 2;
  15. if (mid == x / mid) {
  16. return mid;
  17. } else if (mid < x / mid) {
  18. left = mid + 1;
  19. } else {
  20. right = mid - 1;
  21. }
  22. }
  23. return right;
  24. }
  25. };
  26. int main()
  27. {
  28. Solution s;
  29. cout << s.mySqrt(4) << endl;
  30. cout << s.mySqrt(8) << endl;
  31. cout << s.mySqrt(121) << endl;
  32. cout << s.mySqrt(120) << endl;
  33. cout << s.mySqrt(122) << endl;
  34. return 0;
  35. }

输出:

2
2
11
10
11

另: cmath或者math.h库中有现成的函数 sqrt() 


相关阅读: 力扣C++|一题多解之数学题专场(1)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/488397
推荐阅读
相关标签
  

闽ICP备14008679号