[日常刷题]leetcode第十九天

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

204. Count Primes

Count the number of prime numbers less than a non-negative number, n.
Example:

Input: 10
Output: 4
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.

Solution in C++:

关键点:

  • 不能暴力

思路:

  • 最开始感觉自己想不到什么好方法了就直接采取了暴力方法,我以为还能过一下没想到TLE了,后来想可能是需要计算值然后存起来再去数的方法,但是碍于智商受限就去看人家的讨论了
  • 优化方法就是通过一个数组去存下标为i的数是否是素数,通过我们都知道的除了1的倍数外,其他的倍数都是合数来修改数组,再通过数true的个数来判断总数
int countPrimes(int n) {
        
        int count = 0;
        
        vector<bool> flag(n, true);
        
        for(int i = 2; i < n; ++i)
        {
            if (flag[i] == true)
            {
                ++count;
                for(int j = 2; i * j < n; ++j)
                    flag[i * j] = false;
            }
        }
        
        return count;
    }

205. Isomorphic Strings

Given two strings s and t, determine if they are isomorphic.

Two strings are isomorphic if the characters in s can be replaced to get t.

All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.

Example 1:

Input: s = "egg", t = "add"
Output: true

Example 2:

扫描二维码关注公众号,回复: 3550297 查看本文章
Input: s = "foo", t = "bar"
Output: false

Example 3:

Input: s = "paper", t = "title"
Output: true

Note:
You may assume both s and t have the same length.

Solution in C++:

关键点:

  • 单向映射

思路:

  • 最开始以为只有26个字母,还手撸了个maps出来放在那里,后来出来个"13""24"的案例,吓死我。先说说最开始的想法,拿到手里一脸懵,感觉自己人脑就判断了一会儿,我要怎么写程序呢,就不知不觉在纸上写下了“怎么记住单词之间的映射”,哎呦我去,这不就是map吗,说着就出现了手撸26个字母的场景,撸完之后,就开始判断,如果已经映射过就不是初始值,然后再判断映射一致性。发现这样好像很棒,自己也很开心写出来了,后来发现忘记考虑已经被映射的value也不能被其他key映射的情况,就又开了个flags数组记录。发现嗯,字母没什么问题了吧,我去还有数字,我去好像没有限制string里面装什么,立马换成往map中添加的方式及能否找到key的方式去替换上面的判断,嗯这样很完美的AC了。
  • 去看了一下讨论,我去python一行代码就完成了?看不懂。。。待以后python基础上来了一起刷题。然后看到C++代码一个思路记录两个字母相遇的位置是否一致来判断单向映射问题,也很机智的把数组大小设置为字符的最大数256,nice。

方法一 我的解决方案

bool isIsomorphic(string s, string t) {
        map<char,char> maps;
        map<char,bool> flags;
        
        int i = 0;
        size_t size = s.size();
        
        for( i = 0; i < size; ++i)
        {
            map<char,char>::iterator it1 = maps.find(s[i]);
            if (it1 == maps.end())
            {
                map<char,bool>::iterator it2 = flags.find(t[i]);
                if (it2 != flags.end())
                    return false;
                maps[s[i]] = t[i];
                flags[t[i]] = true;
            } else{
                if (maps[s[i]] == t[i])
                    continue;
                else
                    return false;
            } 
        }
        
        return true;
    }

方法二 记录相遇位置

bool isIsomorphic(string s, string t) {
        int s1[256] = {0};
        int s2[256] = {0};
        size_t size = s.size();
        for(int i = 0; i < size; ++i)
        {
            if(s1[s[i]] != s2[t[i]])
                return false;
            s1[s[i]] = i + 1;
            s2[t[i]] = i + 1;
        }
        return true;
    }

小结

今天的收获就是大佬们的想法都好棒啊。我好像稍微优化还是处于暴力解题状态,不过给自己,看完题解都坚持自己写一遍AC的坚持点赞。

知识点

  • map用法
  • 素数判断新角度
  • 字母映射新思路

猜你喜欢

转载自blog.csdn.net/wait_for_taht_day5/article/details/82891921
今日推荐