代码随想录算法训练营第六天 | LeetCode242有效的字母异位词 、LeetCode349两个数组的交集、LeetCode202快乐数、LeetCode1两数之和

代码随想录算法训练营第六天 | LeetCode242有效的字母异位词 、LeetCode349两个数组的交集、LeetCode202快乐数、LeetCode1两数之和

时长:大约2小时

242. Valid Anagram

Given two strings s and t, return true if t is an anagram of s, and false otherwise.

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Example 1:

Input: s = "anagram", t = "nagaram"

Output: true

Example 2:

Input: s = "rat", t = "car"

Output: false

Constraints:

  • 1 <= s.length, t.length <= 5 * 104

  • s and t consist of lowercase English letters.

Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?

解题思路:

  • 先判读字符串长度,不一样直接false

  • 对s中的字符进行统计,基于unordered_map,key是字符,value是字频

  • 根据t中的字符对unordered_map进行修改,t中每出现一个词,则哈希表中对应字频减1

若出现

  • t中的字符在unordered_map中没有

  • unordered_map中字频减到<0了,则返回false

否则返回true

需要注意的要点:

  • 这一方法需要先对字符串长度进行判读

  • 否则需要对更新后的unordered_map在检查一遍是不是所有元素为0

349. Intersection of Two Arrays

Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2]

Output: [2]

Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]

Output: [9,4]

Explanation: [4,9] is also accepted.

Constraints:

  • 1 <= nums1.length, nums2.length <= 1000

  • 0 <= nums1[i], nums2[i] <= 1000

解题思路:

  • 先把nums1放入set1中

  • 新建一个存放结构的set的res

  • 遍历nums2,在set1中的元素加入res

需要注意的要点:

  • vector和set的转换操作

  • 遍历数组元素

202. Happy Number

Write an algorithm to determine if a number n is happy.

A happy number is a number defined by the following process:

  • Starting with any positive integer, replace the number by the sum of the squares of its digits.

  • Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.

  • Those numbers for which this process ends in 1 are happy.

Return true if n is a happy number, and false if not.

Example 1:

Input: n = 19

Output: true

Explanation:

12 + 92 = 82

82 + 22 = 68

62 + 82 = 100

12 + 02 + 02 = 1

Example 2:

Input: n = 2

Output: false

Constraints:

  • 1 <= n <= 231 - 1

解题思路:

  • 关键在于一点:题目中说会无限循环,说明平方和会重复出现,因此只要利用哈希表存储出现过的结果,再次遇到出现过的结果就不是快乐数

需要注意的要点:

  • 关键在于想到无限循环对应数字重复出现吧

1. Two Sum

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

Example 1:

Input: nums = [2,7,11,15], target = 9

Output: [0,1]

Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

Example 2:

Input: nums = [3,2,4], target = 6

Output: [1,2]

Example 3:

Input: nums = [3,3], target = 6

Output: [0,1]

Constraints:

  • 2 <= nums.length <= 104

  • -109 <= nums[i] <= 109

  • -109 <= target <= 109

  • Only one valid answer exists.

Follow-up: Can you come up with an algorithm that is less than O(n2) time complexity?

解题思路:

  • 采用unordered_map,key是值,value是数组下标

  • 对数组遍历:

  • 先查看在unordered_map中能不能找到数与当前数之和为target

  • 找到即返回

  • 否则,将当前的数和下标计入unordered_map

需要注意的要点:

  • 由于先查在存,因此example3的设置也没有问题,第二个3存入之前会找到第一个3

声明:

文章中LeetCode的题目来自LeetCode官网:https://leetcode.cn/problems

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值