zoukankan      html  css  js  c++  java
  • 面试之哈希表leetcode

    1 案例1 leetcode-----242

    给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。

    示例 1:

    输入: s = "anagram", t = "nagaram"
    输出: true

    python版本

    方法1:直接排序

    方法二:API map计数

    方法三:数组模拟hash

     1 '''
     2 方法1 按照字母序排序 如果一样则是 时间复杂度是nlogN 快排
     3 
     4 方法2 数字符串每个字符串字符的个数,也就是使用map来计算{letter:count}
     5 a---->3
     6 n---->1
     7 r---->1
     8 时间复杂度O(N) *1 为O(N)
     9 '''
    10 
    11 class Solution(object):
    12     def isAnagram(self, s, t):
    13         """
    14         :type s: str
    15         :type t: str
    16         :rtype: bool
    17         """
    18         #方法一
    19         #return sorted(s)==sorted(t)
    20         
    21         #方法二
    22         '''
    23         dic1,dic2={},{}
    24         for item in s:
    25             dic1[item]=dic1.get(item,0)+1
    26         for item in t:
    27             dic2[item]=dic2.get(item,0)+1
    28         return dic1==dic2
    29         '''
    30         #方法三
    31         dic1,dic2=[0]*26,[0]*26
    32         for item in s:
    33             dic1[ord(item)-ord('a')]+=1
    34         for item in t:
    35             dic2[ord(item)-ord('a')]+=1
    36         return dic1==dic2
    37         
    View Code

    2 案例2 leetcode------1

    给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

    你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

    示例:

    给定 nums = [2, 7, 11, 15], target = 9

    因为 nums[0] + nums[1] = 2 + 7 = 9
    所以返回 [0, 1]

     1 class Solution(object):
     2     def twoSum(self, nums, target):
     3         """
     4         :type nums: List[int]
     5         :type target: int
     6         :rtype: List[int]
     7         
     8         dict1={}
     9         for i in range(0,len(nums)):
    10             num=target-nums[i]
    11             if num not in dict1:
    12                 dict1[nums[i]]=i
    13             else:
    14                 return [dict1[num],i]
    15         """
    16         hash_map=dict()
    17         for i,x in enumerate(nums):
    18             if target-x in hash_map:
    19                 return [i,hash_map[target-x]];
    20             hash_map[x]=i
    View Code

    3 案例3 leetcode---15

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。

    方法1:暴力求解 三层循环 时间复杂度n*n*n

    方法2:c= -(a+b)枚举ab两层循环,然后在set中查询-(a+b) 时间复杂度n*n 多了空间复杂度n

    方法3:sort find: 整个数组排序o(n*logn),首先先第一层循环取出第一个元素a,然后后面使用二分查找bc两个值,如果存在b+c=a则找到,时间复杂度O(N*N)

    python版本:

     1 class Solution(object):
     2     def threeSum(self, nums):
     3         nums.sort()#先排序
     4         n = len(nums)
     5         res = []
     6         #print(nums)
     7         for i in range(n):
     8             if i > 0 and nums[i] == nums[i-1]:
     9                 continue
    10             #左右边界
    11             left = i + 1
    12             right = n - 1
    13             while left < right:
    14                 cur_sum = nums[i] + nums[left] + nums[right]
    15                 if cur_sum == 0:
    16                     tmp = [nums[i],nums[left],nums[right]]
    17                     res.append(tmp)
    18                     #去掉重复的解
    19                     while left < right and nums[left] == nums[left+1]:
    20                         left += 1
    21                     while left < right and nums[right] == nums[right-1]:
    22                         right -= 1
    23                     left += 1
    24                     right -= 1
    25                 elif cur_sum > 0:
    26                     right -= 1
    27                 else:
    28                     left += 1
    29         return res
    View Code

    c++版本

     1 class Solution {
     2 public:
     3     vector<vector<int>> threeSum(vector<int>& nums) {
     4          int target;
     5         vector<vector<int>> ans;
     6         sort(nums.begin(), nums.end());
     7         for (int i = 0; i < nums.size(); i++) {
     8             if (i > 0 && nums[i] == nums[i - 1]) continue;
     9             if ((target = nums[i]) > 0) break;
    10             int l = i + 1, r = nums.size() - 1;
    11             while (l < r) {
    12                 if (nums[l] + nums[r] + target < 0) ++l;
    13                 else if (nums[l] + nums[r] + target > 0) --r;
    14                 else {
    15                     ans.push_back({target, nums[l], nums[r]});
    16                     ++l, --r;
    17                     while (l < r && nums[l] == nums[l - 1]) ++l;
    18                     while (l < r && nums[r] == nums[r + 1]) --r;
    19                 }
    20             }
    21         }
    22         return ans; 
    23     }
    24 };
    View Code
  • 相关阅读:
    Android接入WebView
    james邮件服务器部署
    防止网络攻击的方式
    vue开发遇到的问题及解决方式
    jekins和docker的作用
    设计模式(2)[JS版]---JavaScript如何实现单例模式?
    黑客帝国中代码雨如何实现?用 canvas 轻松实现代码雨炫酷效果!
    什么是JavaScript 的闭包???
    纯CSS实现iOS风格打开关闭选择框
    纯CSS实现自定义单选框和复选框
  • 原文地址:https://www.cnblogs.com/lanjianhappy/p/11802219.html
Copyright © 2011-2022 走看看