solution.json 4.9 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
{
  "type": "code_options",
  "author": "https://github.com/qiyuangong/leetcode",
  "source": "solution.md",
  "exercise_id": "af25ce9a4f4c425b8c900b3345aef661",
  "keywords": "数组,双指针",
  "title": "移除元素",
  "desc": [
    {
      "content": "\n<div class=\"notranslate\">\n<p>给你一个数组 <code>nums</code><em>&nbsp;</em>和一个值 <code>val</code>,你需要 <strong><a\nhref=\"https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\">原地</a></strong>\n移除所有数值等于&nbsp;<code>val</code><em>&nbsp;</em>的元素,并返回移除后数组的新长度。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>不要使用额外的数组空间,你必须仅使用 <code>O(1)</code> 额外空间并 <strong><a\nhref=\"https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\">原地 </a>修改输入数组</strong>。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>&nbsp;</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>说明:</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>为什么返回数值是整数,但输出的答案是数组呢?</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>请注意,输入数组是以<strong>「引用」</strong>方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>你可以想象内部操作如下:</p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre>// <strong>nums</strong> 是以“引用”方式传递的。也就是说,不对实参作任何拷贝\nint len = removeElement(nums, val);",
      "language": "markdown"
    },
    {
      "content": "\n// 在函数里修改输入数组对于调用者是可见的。\n// 根据你的函数返回的长度, 它会打印出数组中<strong> 该长度范围内</strong> 的所有元素。\nfor (int i = 0; i &lt; len; i++) {\n&nbsp; &nbsp; print(nums[i]);\n}\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p>&nbsp;</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例 1:</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入:</strong>nums = [3,2,2,3], val = 3\n<strong><br />输出:</strong>2, nums = [2,2]\n<strong><br />解释:</strong>函数应该返回新的长度 <strong>2</strong>, 并且 nums<em> </em>中的前两个元素均为 <strong>2</strong>。你不需要考虑数组中超出新长度后面的元素。例如,函数返回的新长度为 2 ,而 nums = [2,2,3,3] 或 nums = [2,2,0,0],也会被视作正确答案。\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例 2:</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入:</strong>nums = [0,1,2,2,3,0,4,2], val = 2\n<strong><br />输出:</strong>5, nums = [0,1,4,0,3]\n<strong><br />解释:</strong>函数应该返回新的长度 <strong>5</strong>, 并且 nums 中的前五个元素为 <strong>0</strong>, <strong>1</strong>, <strong>3</strong>, <strong>0</strong>, <strong>4</strong>。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面的元素。\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p>&nbsp;</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>提示:</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<ul>\n<li><code>0 &lt;= nums.length &lt;= 100</code></li>\n<li><code>0 &lt;= nums[i] &lt;= 50</code></li>\n<li><code>0 &lt;= val &lt;= 100</code></li>\n</ul>\n</div>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "python"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ]
  ],
  "template": {
    "content": "class Solution(object):\n\tdef removeElement(self, nums, val):\n\t\tls = len(nums)\n\t\tif ls == 0:\n\t\t\treturn ls\n\t\tcount = 0\n\t\tindex = 0\n\t\twhile index < ls - count:\n\t\t\tif nums[index] == val:\n\t\t\t\tnums[index] = nums[ls - 1 - count]\n\t\t\t\tcount += 1\n\t\t\telse:\n\t\t\t\tindex += 1\n\t\treturn ls - count\nif __name__ == '__main__':\n\ts = Solution()\n\tprint(s.removeElement([3,2,2,3], 3))",
    "language": "python"
  },
  "node_id": "dailycode-f46ab61031ab4af6926cc6db067d1b78",
  "license": "csdn.net",
  "created_at": 1637894161,
  "topic_link": "https://bbs.csdn.net/topics/600470219"
}