65.json 3.0 KB
Newer Older
每日一练社区's avatar
test  
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
{
   "question_id": 66,
   "question_title": "加一",
   "difficulty": "简单",
   "question_content": "<p>给定一个由 <strong>整数 </strong>组成的<strong> 非空</strong> 数组所表示的非负整数,在该数的基础上加一。</p><p>最高位数字存放在数组的首位, 数组中每个元素只存储<strong>单个</strong>数字。</p><p>你可以假设除了整数 0 之外,这个整数不会以零开头。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>digits = [1,2,3]<strong><br />输出:</strong>[1,2,4]<strong><br />解释:</strong>输入数组表示数字 123。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>digits = [4,3,2,1]<strong><br />输出:</strong>[4,3,2,2]<strong><br />解释:</strong>输入数组表示数字 4321。</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>digits = [0]<strong><br />输出:</strong>[1]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>1 <= digits.length <= 100</code></li>\t<li><code>0 <= digits[i] <= 9</code></li></ul>",
   "topic_link": "https://bbs.csdn.net/topics/600469823",
   "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tvector<int> plusOne(vector<int> &digits)\n\t{\n\t\tint carry = 1;\n\t\tvector<int> res;\n\t\tfor (int i = digits.size() - 1; i >= 0; i--)\n\t\t{\n\t\t\tint d = digits[i] + carry;\n\t\t\tres.push_back(d % 10);\n\t\t\tcarry = d / 10;\n\t\t}\n\t\tif (carry > 0)\n\t\t{\n\t\t\tres.push_back(carry);\n\t\t}\n\t\treverse(res.begin(), res.end());\n\t\treturn res;\n\t}\n};",
   "java": "class Solution {\n\tpublic int[] plusOne(int[] digits) {\n\t\tint i = digits.length - 1;\n\t\twhile (i >= 0 && (digits[i] = digits[i] + 1) == 10) {\n\t\t\tdigits[i] = 0;\n\t\t\ti--;\n\t\t}\n\t\tif (digits[0] == 0) {\n\t\t\tint[] temp = new int[digits.length + 1];\n\t\t\ttemp[0] = 1;\n\t\t\treturn temp;\n\t\t}\n\t\treturn digits;\n\t}\n}",
   "js": "",
   "python": "class Solution(object):\n\tdef plusOne(self, digits):\n\t\tls = len(digits)\n\t\tfor index in reversed(range(ls)):\n\t\t\tif digits[index] < 9:\n\t\t\t\tdigits[index] += 1\n\t\t\t\treturn digits\n\t\t\telse:\n\t\t\t\tdigits[index] = 0\n\t\tdigits.insert(0, 1)\n\t\treturn digits\n# %%\ns = Solution()\nprint(s.plusOne(digits = [1,2,3]))",
   "status": 1,
   "keywords": "数组,数学",
   "license": {
      "cpp": "https://github.com/begeekmyfriend/leetcode",
      "python": "https://github.com/qiyuangong/leetcode",
      "java": "https://github.com/zhangyu345293721/leetcode"
   },
   "notebook": {
      "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/65/65_cpp.ipynb?type=file",
      "python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/65/65_python.ipynb?type=file",
      "java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/65/65_java.ipynb?type=file"
   },
   "notebook_enable": 1
}