<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>leetcode on 华说记录我的生活</title>
    <link>http://www.huasay.com/tags/leetcode/</link>
    <description>Recent content in leetcode on 华说记录我的生活</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>zh</language>
    <lastBuildDate>Wed, 29 Jul 2020 14:26:49 +0800</lastBuildDate>
    <atom:link href="http://www.huasay.com/tags/leetcode/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Leetcode 26 最长公共前缀</title>
      <link>http://www.huasay.com/2020/07/29/leetcode-14/</link>
      <pubDate>Wed, 29 Jul 2020 14:26:49 +0800</pubDate>
      <guid>http://www.huasay.com/2020/07/29/leetcode-14/</guid>
      <description>编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀，返回空字符串 &amp;ldquo;&amp;quot;。
示例 1:
1 2 输入: flower&amp;#34;,&amp;#34;flow&amp;#34;,&amp;#34;flight&amp;#34;] 输出: &amp;#34;fl&amp;#34; 示例 2:
1 2 3 输入: [&amp;#34;dog&amp;#34;,&amp;#34;racecar&amp;#34;,&amp;#34;car&amp;#34;] 输出: &amp;#34;&amp;#34; 解释: 输入不存在公共前缀。 说明:
所有输入只包含小写字母 a-z 。
解题思路:
leetcode官方提供了不少解决方案. 主要解决方案有: 横向扫描法, 纵向扫描法,分治法, 二分查找法, 字典树. 个人觉得纵向扫描法是最容易想到的,而且效率也不错.
纵向扫描法:
纵向扫描时即依次遍历所有字符串的每一列，比较相同列上的字符是否相同，如果相同则继续对下一列进行比较，如果不相同则当前列不再属于公共前缀，当前列之前的部分为最长公共前缀。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 func longestCommonPrefix(strs []string) string { strsLen := len(strs) if strsLen == 0 { return &amp;#34;&amp;#34; } for i := 0; i &amp;lt; len(strs[0]); i++ { for j := 1; j &amp;lt; strsLen; j++ { if i == len(strs[j]) || strs[0][i] !</description>
    </item>
    <item>
      <title>Leetcode 350 两个数组的交集 II</title>
      <link>http://www.huasay.com/2020/07/28/leetcode-350/</link>
      <pubDate>Tue, 28 Jul 2020 23:19:42 +0800</pubDate>
      <guid>http://www.huasay.com/2020/07/28/leetcode-350/</guid>
      <description>给定两个数组，编写一个函数来计算它们的交集。
示例 1：
1 2 输入：nums1 = [1,2,2,1], nums2 = [2,2] 输出：[2,2] 示例 2:
1 2 输入：nums1 = [4,9,5], nums2 = [9,4,9,8,4] 输出：[4,9] 说明：
输出结果中每个元素出现的次数，应与元素在两个数组中出现次数的最小值一致。 我们可以不考虑输出结果的顺序。 进阶：
如果给定的数组已经排好序呢？你将如何优化你的算法？ 如果 nums1 的大小比 nums2 小很多，哪种方法更优？ 如果 nums2 的元素存储在磁盘上，磁盘内存是有限的，并且你不能一次加载所有的元素到内存中，你该怎么办？ 解题思路【哈希法】：
将其中一个数组转换成哈希Map的类型，key为数组的值，value为数组中该数出现的次数。
循环遍历另一个数组，通过Map中是否有该值来判断，如果有相同的值，则记录该值，同时将Map对应key的value值减1.
时间复杂度：O(max(n,m))
空间复杂度：O(min(n,m))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 func intersect(nums1 []int, nums2 []int) []int { m := map[int]int{} ret := []int{} for _, v := range nums1 { m[v]++ } for _, v := range nums2 { if m[v] !</description>
    </item>
    <item>
      <title>Leetcode 26 删除排序数组中的重复项</title>
      <link>http://www.huasay.com/2020/07/27/leetcode-26/</link>
      <pubDate>Mon, 27 Jul 2020 15:18:01 +0800</pubDate>
      <guid>http://www.huasay.com/2020/07/27/leetcode-26/</guid>
      <description>给定一个排序数组，你需要在 原地 删除重复出现的元素，使得每个元素只出现一次，返回移除后数组的新长度。不要使用额外的数组空间，你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。
示例 1:
1 2 3 给定数组 nums = [1,1,2], 函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。 你不需要考虑数组中超出新长度后面的元素。 示例 2:
1 2 3 给定 nums = [0,0,1,1,1,2,2,3,3,4], 函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。 你不需要考虑数组中超出新长度后面的元素。 说明:
为什么返回数值是整数，但输出的答案是数组呢? 请注意，输入数组是以“引用”方式传递的，这意味着在函数里修改输入数组对于调用者是可见的。 你可以想象内部操作如下:
1 2 3 4 5 6 7 // nums 是以“引用”方式传递的。也就是说，不对实参做任何拷贝 int len = removeDuplicates(nums); // 在函数里修改输入数组对于调用者是可见的。 // 根据你的函数返回的长度, 它会打印出数组中该长度范围内的所有元素。 for (int i = 0; i &amp;lt; len; i++) { print(nums[i]); } 思路:</description>
    </item>
  </channel>
</rss>
