【Find if all numbers appear an even number of times】会挽雕弓如满月,西北望,射天狼。这篇文章主要讲述Find if all numbers appear an even number of times相关的知识,希望能为你提供帮助。
package _interview_question/** * Given a list/array of integers, return a boolean that states whether all numbers appear an even number of times.Example 1: Input: [3, 1, 2, 1, 2, 3] Output: trueExample 2: Input: [1, 2, 1] Output: falseExample 3: Input: [1, 2, 3] Output: false * */ class Solution4 { fun findAllIfAppearEvenTime(nums: IntArray): Boolean { /* method 1: Time complexity: O(n), Space complexity: O(n) * */ val map = HashMap< Int, Int> () for (num in nums) { map.put(num, map.getOrDefault(num, 0) + 1) } for (item in map) { if (item.value % 2 != 0) { return false } } return true/* * method 2: Time complexity: O(nlogn), Space complexity: O(1) * */ if (nums.size % 2 != 0) { return false } nums.sort() var sameNumAppearCount = 1 for (i in 0 until nums.size - 1) { if (nums[i] == nums[i + 1]) { sameNumAppearCount++ } else { if (sameNumAppearCount % 2 != 0) { return false } else { sameNumAppearCount = 1 } } } return true } }
推荐阅读
- 提交APP到三星应用市场一直提示不符合 Galaxy特色体验的类别条件
- 安卓到底是不是Linux
- 无法加载文件 C:UsersAdministratorAppDataRoamingpm sc.ps1,因为在此系统上禁止运行脚本
- 机器学习实战基础(十八)(sklearn中的数据预处理和特征工程特征选择 之 Wrapper包装法)
- spring设计模式之applicationContext.getBean("beanName")思想
- Android Back Home键监听
- 《全能去水印》APP详细介绍和下载
- LeetCode 202. 快乐数 Happy Number
- leetcode1415. The k-th Lexicographical String of All Happy Strings of Length n