[日常刷题]leetcode D25
文章目录
- 242. Valid Anagram
- 258. Add Digits
- 263. Ugly Number
- 小结
- 知识点
242. Valid Anagram
Given two strings s and t , write a function to determine if t is an anagram of s.
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Example 2:
Input: s = "rat", t = "car"
Output: false
Note:
You may assume the string contains only lowercase alphabets.
Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?
Solution in C++:
关键点:
- 字母出现次数相同
思路
- 开始用词典翻译了,是倒序,然后看例子也不是倒序有点懵了,看了解析是乱序就行。那这个就比较简单了,退化为了字母数数或者排序。不过看到解析对于数数有两种方法,一种是一个数一个减,另一个是两个都数。本质差不多。
方法一:排序
bool isAnagram(string s, string t) {sort(s.begin(),s.end());sort(t.begin(),t.end());return s == t;}
方法二:字母数数
bool isAnagram(string s, string t) {if (s.size() != t.size())return false;map<char,long long> maps;map<char,long long> mapt;for(auto str : s){map<char,long long>::iterator it = maps.find(str);if (it != maps.end()) // 找到{++maps[str];} else{maps[str] = 1;}}for(auto str : t){map<char,long long>::iterator it = mapt.find(str);if (it != mapt.end()) // 找到{++mapt[str];} else{mapt[str] = 1;}}if (maps == mapt)return true;elsereturn false;}
258. Add Digits
Given a non-negative integer num
, repeatedly add all its digits until the result has only one digit.
Example:
Input: 38
Output: 2
Explanation: The process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
Solution in C++:
关键点:
- 情况0-9
思路:
- 突然感觉好像没什么关键点一样。。。因为不知道这个数学知识,所以最开始实现的方法就是暴力。循环,将每位加的结果再放入函数中继续加直到结果小于10。另外一个是digital root理论。
方法一:暴力(循环)
int addDigit(int num){int result = 0;while(num){result += num % 10;num /= 10;}return result;}int addDigits(int num) {int result = num;while(result >= 10){result = addDigit(result);}return result;}
方法二:digital root公式
int addDigits(int num) {return 1 + (num-1) % 9; }
263. Ugly Number
Write a program to check whether a given number is an ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5
.
Example 1:
Input: 6
Output: true
Explanation: 6 = 2 × 3
Example 2:
Input: 8
Output: true
Explanation: 8 = 2 × 2 × 2
Example 3:
Input: 14
Output: false
Explanation: 14 is not ugly since it includes another prime factor 7.
Note:
- 1 is typically treated as an ugly number.
- Input is within the 32-bit signed integer range: [ − 2 31 −2^{31} −231, 2 31 2^{31} 231 − 1].
Solution in C++:
关键点:
- 除法性质
思路:
- 这题有可能的数字只能是>=1的数字,所以可以排除一部分数字。然后就是对于只由2,3,5组成的数字,执行除法之后结果应该为1,否则不是。
bool isUgly(int num) {if (num <= 0)return false;else if (num == 1)return true;vector<int> factor = {2,3,5};for(auto i : factor){while(num % i == 0)num /= i;}return num == 1;}
小结
今天主要学习了一些对于数的处理思路,包括 +/-判断相等,然后就是digital root还有就是sort用法。还有一点就是蛮好奇对于unicode string的处理。查了一下了解到C++中string的基础类型是char *,即为一个字节为单位,也code验证了一下,一个字节一个字节读出来的中文是乱码的,所以需要解决的问题就是如何判断当前存储的unicode类型需要几个字节,然后再通过数数的方式来判断。(具体unicode的判断方法暂时还没找到什么好的,欢迎大家补充)
知识点
- digital root [ 1 + (n-1) % 9 ]
- sort用法
[日常刷题]leetcode D25相关推荐
- 面试刷题LeetCode经典100道
准备面试刷题,100道经典LeetCode题目奉上. 题号 英文题名 中文题名 难度 likes 数 标签 1 Two Sum 两数之和 Easy 11712 数组,哈希表 2 Add Two Num ...
- 不止是刷题——leetcode笑死人的评论合集,独乐乐不如众乐乐~~
最近一直在leetcode上刷题,爱学习的我发现了评论区里不只有答疑解惑的大神们,还潜伏着众多神兽和段子手们,截图出来,与诸君共享,程序猿的乐趣不是一般人能懂得~~ --本文由非正经程序猿吴向深独家撰 ...
- 洛谷日常刷题(洛谷官方题单 思路+详解)
目录 前言 非官方题单的题 P1141 01迷宫 1-4 递推与递归 P1255 数楼梯 1002 [ NOIP 2002 普及组]过河卒 P1044 [NOIP2003 普及组] 栈 P1028 [ ...
- LeetCode日常刷题1、657、717、67
1. 两数之和 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], ...
- python刷题+leetcode(第一部分)
1. 设计前中后队列 思路:python代码直接利用list的insert特性 class FrontMiddleBackQueue:def __init__(self):self.queque = ...
- C# LeetCode刷题 - Leetcode 306. 累加数 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- [刷题]leetcode #309 - Best Time to Buy and Sell Stock with Cooldown
题目 代码 class Solution {public int maxProfit(int[] prices) {if (prices.length <= 1) return 0;int l ...
- [刷题]leetcode\977_有序数组的平方
抛开思想不谈,这一题的思想还是很简单的
- 日常刷题_cf_6.26
1.Maximum Subsequence Value 从nnn个数中挑kkk个数,使得∑2i\sum2^i∑2i最大(将这kkk个数用二进制表示,如果二进制第 iii 位为1的数的个数多于max(1 ...
最新文章
- 模拟播放器倒计时效果
- python对财务人员的帮助-帮公司财务妹子写了个“群发工资条”的Python脚本!
- 关于Android消息机制你所需要知道的
- 工作中必须要知道的git高级用法
- python网络编程教学_python网络编程学习初步
- 【机器学习-数据科学】第二节:ipython开发环境搭建以及pandas快速入门
- GTK之任意拖动窗口中的按钮
- Nginx实战|Nginx健康检查
- React-表单处理(受控组件,非受控组件)
- https://blog.csdn.net/gyming/article/details/46611369
- python3中的sorted()函数
- 复合梯形公式matlab代码,复合梯形公式
- 阿里云| 阿里云汇总
- python 三次根号_开3次方根(多次方根)的代码:二分法,python
- JAVA 支付宝支付_史诗级简单教程(SpringBoot)
- 问,你的算法复习计划是什么?
- 申威26010 芯片 服务器,解析申威26010处理器结构(附:国产超算发展史)
- MySQL第三方客户端工具
- 如何在 Java 中实现最小生成树算法
- 插入排序(动图理解)
热门文章
- day06 - Python - 面向对象
- 【琐琐碎碎小知识】Keil5编译时候出现 Error: L6200E: Symbol HAL_MspDeInit multiply defined事故处理
- 计算机web白帽子安全术语
- Linux检测端口是否被防火墙禁止或端口是否被占用
- 论文阅读笔记:为什么深度神经网络的训练无论多少次迭代永远有效?可能类内分布已经坍缩为一个点,模型已经崩溃为线性分类器
- matlab某数组中产生不重复随机数,数组的随机排序,randperm函数numel,X=X(A)函数
- 为什么我们学习一开始就要用utf-8
- 每个人都是独一无二的
- c语言向上取整计算方法
- AVL 树高度和结点数的关系