[日常刷题]leetcode D25

版权声明:希望各位多提意见多多互相交流哦~ https://blog.csdn.net/wait_for_taht_day5/article/details/82941328

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;
        else
            return 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. 1 is typically treated as an ugly number.
  2. Input is within the 32-bit signed integer range: [ 2 31 −2^{31} , 2 31 2^{31} − 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用法

猜你喜欢

转载自blog.csdn.net/wait_for_taht_day5/article/details/82941328