力扣53 最大自序和

53. 最大子序和

给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。

示例:

输入: [-2,1,-3,4,-1,2,1,-5,4]

输出: 6

解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。

进阶:

如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/maximum-subarray

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这道题官方给了两种解法,一种是动态规划法,是我们通常都可以想到的方法,另外一种就是分治法。

image.png

  1. class Solution {
  2. public:
  3. int maxSubArray(vector<int>& nums) {
  4. int pre = 0, maxAns = nums[0];
  5. for (const auto &x: nums) {
  6. pre = max(pre + x, x);
  7. maxAns = max(maxAns, pre);
  8. }
  9. return maxAns;
  10. }
  11. };
  12. 作者:LeetCode-Solution
  13. 链接:https://leetcode-cn.com/problems/maximum-subarray/solution/zui-da-zi-xu-he-by-leetcode-solution/
  14. 来源:力扣(LeetCode
  15. 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

image.png

image.png

  1. class Solution {
  2. public:
  3. struct Status {
  4. int lSum, rSum, mSum, iSum;
  5. };
  6. Status pushUp(Status l, Status r) {
  7. int iSum = l.iSum + r.iSum;
  8. int lSum = max(l.lSum, l.iSum + r.lSum);
  9. int rSum = max(r.rSum, r.iSum + l.rSum);
  10. int mSum = max(max(l.mSum, r.mSum), l.rSum + r.lSum);
  11. return (Status) {lSum, rSum, mSum, iSum};
  12. };
  13. Status get(vector<int> &a, int l, int r) {
  14. if (l == r) return (Status) {a[l], a[l], a[l], a[l]};
  15. int m = (l + r) >> 1;
  16. Status lSub = get(a, l, m);
  17. Status rSub = get(a, m + 1, r);
  18. return pushUp(lSub, rSub);
  19. }
  20. int maxSubArray(vector<int>& nums) {
  21. return get(nums, 0, nums.size() - 1).mSum;
  22. }
  23. };
  24. 作者:LeetCode-Solution
  25. 链接:https://leetcode-cn.com/problems/maximum-subarray/solution/zui-da-zi-xu-he-by-leetcode-solution/
  26. 来源:力扣(LeetCode
  27. 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

image.png

力扣273 整数转换英文表示

273. 整数转换英文表示

将非负整数转换为其对应的英文表示。可以保证给定输入小于 231 - 1 。

示例 1:

输入: 123

输出: “One Hundred Twenty Three”

示例 2:

输入: 12345

输出: “Twelve Thousand Three Hundred Forty Five”

示例 3:

输入: 1234567

输出: “One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven”

示例 4:

输入: 1234567891

输出: “One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One”

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/integer-to-english-words

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

分治
我们将这个问题分解成一系列子问题。例如,对于数字 1234567890,我们将它从低位开始每三个分成一组,得到 1,234,567,890,它的英文表示为 1 Billion 234 Million 567 Thousand 890。这样我们就将原问题分解成若干个三位整数转换为英文表示的问题了。

接下来,我们可以继续将三位整数分解,例如数字 234 可以分别成百位 2 和十位个位 34,它的英文表示为 2 Hundred 34。这样我们继续将原问题分解成一位整数和两位整数的英文表示。其中一位整数的表示是很容易的,而两位整数中除了 10 到 19 以外,其余整数的的表示可以分解成两个一位整数的表示,这样问题就被圆满地解决了。下面的幻灯片中给出了 1234567890 得到英文表示的例子。

  1. class Solution:
  2. def numberToWords(self, num):
  3. """
  4. :type num: int
  5. :rtype: str
  6. """
  7. def one(num):
  8. switcher = {
  9. 1: 'One',
  10. 2: 'Two',
  11. 3: 'Three',
  12. 4: 'Four',
  13. 5: 'Five',
  14. 6: 'Six',
  15. 7: 'Seven',
  16. 8: 'Eight',
  17. 9: 'Nine'
  18. }
  19. return switcher.get(num)
  20. def two_less_20(num):
  21. switcher = {
  22. 10: 'Ten',
  23. 11: 'Eleven',
  24. 12: 'Twelve',
  25. 13: 'Thirteen',
  26. 14: 'Fourteen',
  27. 15: 'Fifteen',
  28. 16: 'Sixteen',
  29. 17: 'Seventeen',
  30. 18: 'Eighteen',
  31. 19: 'Nineteen'
  32. }
  33. return switcher.get(num)
  34. def ten(num):
  35. switcher = {
  36. 2: 'Twenty',
  37. 3: 'Thirty',
  38. 4: 'Forty',
  39. 5: 'Fifty',
  40. 6: 'Sixty',
  41. 7: 'Seventy',
  42. 8: 'Eighty',
  43. 9: 'Ninety'
  44. }
  45. return switcher.get(num)
  46. def two(num):
  47. if not num:
  48. return ''
  49. elif num < 10:
  50. return one(num)
  51. elif num < 20:
  52. return two_less_20(num)
  53. else:
  54. tenner = num // 10
  55. rest = num - tenner * 10
  56. return ten(tenner) + ' ' + one(rest) if rest else ten(tenner)
  57. def three(num):
  58. hundred = num // 100
  59. rest = num - hundred * 100
  60. if hundred and rest:
  61. return one(hundred) + ' Hundred ' + two(rest)
  62. elif not hundred and rest:
  63. return two(rest)
  64. elif hundred and not rest:
  65. return one(hundred) + ' Hundred'
  66. billion = num // 1000000000
  67. million = (num - billion * 1000000000) // 1000000
  68. thousand = (num - billion * 1000000000 - million * 1000000) // 1000
  69. rest = num - billion * 1000000000 - million * 1000000 - thousand * 1000
  70. if not num:
  71. return 'Zero'
  72. result = ''
  73. if billion:
  74. result = three(billion) + ' Billion'
  75. if million:
  76. result += ' ' if result else ''
  77. result += three(million) + ' Million'
  78. if thousand:
  79. result += ' ' if result else ''
  80. result += three(thousand) + ' Thousand'
  81. if rest:
  82. result += ' ' if result else ''
  83. result += three(rest)
  84. return result
  85. 作者:LeetCode
  86. 链接:https://leetcode-cn.com/problems/integer-to-english-words/solution/zheng-shu-zhuan-huan-ying-wen-biao-shi-by-leetcode/
  87. 来源:力扣(LeetCode
  88. 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。