优选算法专题6:模拟

模拟

目录

模拟

试题1:替换所有的问号

算法原理

代码编写

试题2:提莫攻击

算法原理

代码编写

试题3:Z字形变换

算法原理

代码编写

试题4:外观数列

算法原理

代码编写

试题5:数青蛙

算法原理

代码编写


试题1:替换所有的问号

算法原理

解法:模拟

照葫芦画瓢,思路简单,考察代码能力

模拟算法流程,在草稿纸上过流程,将流程转化为代码

代码编写

  • 个人版本
cpp 复制代码
class Solution 
{
public:
    string modifyString(string s) 
    {
        int n = s.size();
        for(int i = 0;i < n;i++)
        {
            if(s[i] == '?')
            {
                if(i == 0)
                {
                    if(s[i + 1] != 'a')
                    {
                        s[i] = 'a';
                    }
                    else
                    {
                        s[i] = 'b';
                    }
                }
                else if(i == n - 1)
                {
                    if(s[i - 1] != 'a')
                    {
                        s[i] = 'a';
                    }
                    else
                    {
                        s[i] = 'b';
                    }
                }
                else
                {
                    if(s[i - 1] != 'a' && s[i + 1] != 'a')
                    {
                        s[i] = 'a';
                    }
                    else if(s[i - 1] == 'a' || s[i + 1] == 'a')
                    {
                        if(s[i - 1] != 'b' && s[i + 1] != 'b')
                        {
                            s[i] = 'b';
                        }
                        else if(s[i - 1] == 'b' || s[i + 1] == 'b')
                        {
                            if(s[i - 1] != 'c' && s[i + 1] != 'c')
                            {
                                s[i] = 'c';
                            }
                        }
                    }
                }
            }
        }
        return s;
    }
};
  • 标准版本
cpp 复制代码
class Solution 
{
public:
    string modifyString(string s) 
    {
        int n = s.size();
        for(int i = 0;i < n;i++)
        {
            if(s[i] == '?')//替换
            {
                for(char ch = 'a';ch <= 'z';ch++)
                {
                    if((i == 0 || ch != s[i - 1]) && (i == n - 1 || ch != s[i + 1]))
                    {
                        s[i] = ch;
                        break;
                    }
                }
            }
        }
        return s;
    }
};

试题2:提莫攻击

算法原理

代码编写

  • 个人版本
cpp 复制代码
class Solution 
{
public:
    int findPoisonedDuration(vector<int>& timeSeries, int duration) 
    {
        int n = timeSeries.size();
        int ret = 0;
        for(int i = 1;i < n;i++)
        {
            if(timeSeries[i] - timeSeries[i - 1] >= duration)
            {
                ret += duration;
            }
            else
            {
                ret += timeSeries[i] - timeSeries[i - 1];
            }
        }
        ret += duration;
        return ret;
    }
};
  • 标准版本
cpp 复制代码
class Solution 
{
public:
    int findPoisonedDuration(vector<int>& timeSeries, int duration) 
    {
        int ret = 0;
        for(int i = 1;i < timeSeries.size();i++)
        {
            int x = timeSeries[i] - timeSeries[i - 1];
            if(x >= duration) ret += duration;   
            else ret += x;
        }
        return ret += duration;
    }
};

试题3:Z字形变换

算法原理

解法一:模拟

时间复杂度:O(len * N)

空间复杂度:O(len * N)

解法二:找规律优化

代码编写

  • 个人版本
cpp 复制代码
class Solution 
{
public:
    string convert(string s, int numRows) 
    {
        if(numRows == 1)
        {
            return s;
        }
        int d = numRows * 2 - 2;
        string ret;
        for(int row = 0; row < numRows; row++)
        {
            if(row == 0)
            {
                for(int len = 0; len < s.size(); len += d)
                {
                    ret += s[len];
                }
            }
            else if(row == numRows - 1)
            {
                for(int len = numRows - 1; len < s.size(); len += d)
                {
                    ret += s[len];
                }
            }
            else
            {
                for(int len = row; len < s.size(); len += d)
                {
                    ret += s[len];
                    int tmp = d + len - 2 * row;
                    if(tmp < s.size())
                    {
                        ret += s[tmp];
                    }
                }
            }
        }
        return ret;
    }
};
  • 标准版本
cpp 复制代码
class Solution 
{
public:
    string convert(string s, int numRows) 
    {
        //处理边界情况
        if(numRows == 1) return s;
        string ret;
        int d = 2 * numRows - 2, n = s.size();
        //#1:先处理第一行
        for(int i = 0; i < n; i += d)
            ret += s[i];
        //#2:处理中间行
        for(int k = 1; k < numRows - 1; k++)//枚举每一行
        {
            for(int i = k, j = d - k; i < n || j < n; i += d, j += d)
            {
                if(i < n) ret += s[i];
                if(j < n) ret += s[j];
            }
        }
        //#3:处理最后一行
        for(int i = numRows - 1; i < n; i += d)
            ret += s[i];
        return ret;
    }
};

试题4:外观数列

算法原理

解法:模拟+双指针

代码编写

  • 个人版本
cpp 复制代码
class Solution 
{
public:
    string countAndSay(int n) 
    {
        string prev = "1";
        for(int i = 0;i < n - 1;i++)
        {
            string cur;
            int right = 0,left = 0;
            while(right < prev.size())
            {
                while(prev[right] == prev[left])
                {
                    right++;
                }
                cur += to_string(right - left);
                cur += prev[left];
                left = right;
            }
            prev = cur;
        }
        return prev;    
    }
};
  • 标准版本
cpp 复制代码
class Solution 
{
public:
    string countAndSay(int n) 
    {
        string ret = "1";
        for(int i = 1; i < n; i++)
        {
            string tmp;
            int len = ret.size();
            for(int left = 0,right = 0;right < len;)
            {
                while(right < len && ret[left] == ret[right]) right++;
                tmp += to_string(right - left) + ret[left];
                left = right;
            } 
            ret = tmp;
        }
        return ret;
    }
};

试题5:数青蛙

算法原理

解法:模拟

代码编写

  • 个人版本
cpp 复制代码
class Solution 
{
public:
    int minNumberOfFrogs(string croakOfFrogs) 
    {
        unordered_map<char,int> hash = {
            {'c',0},
            {'r',1},
            {'o',2},
            {'a',3},
            {'k',4},
        };
        int hash_arr[5] = {0};
        int n = croakOfFrogs.size();
        if(n % 5 != 0)
        {
            return -1;
        }
        for(int i = 0;i < n;i++)
        {
            if(hash[croakOfFrogs[i]] == 0)//'c'
            {
                if(hash_arr[4] > 0)
                {
                    hash_arr[4]--;
                    hash_arr[0]++;
                }
                else
                {
                    hash_arr[0]++;
                }
            }
            else//其他字符
            {
                if(hash_arr[hash[croakOfFrogs[i]] - 1] > 0)
                {
                    hash_arr[hash[croakOfFrogs[i]] - 1]--;
                    hash_arr[hash[croakOfFrogs[i]]]++;
                }
                else
                {
                    return -1;
                }
            }
        }
        if(hash_arr[0] || hash_arr[1] || hash_arr[2] || hash_arr[3])
        {
            return -1;
        }
        return hash_arr[4];
    }
};
  • 标准版本
cpp 复制代码
class Solution 
{
public:
    int minNumberOfFrogs(string croakOfFrogs) 
    {
       string t = "croak";
       int n = t.size();
       vector<int> hash(n);//用数组模拟哈希表
    
        unordered_map<char,int> index;//[x,x这个字符对应下标]
        for(int i = 0;i < n;i++)
        {
            index[t[i]] = i;
        }

        for(auto ch : croakOfFrogs)
        {
            if(ch == 'c')
            {
                if(hash[n - 1] != 0) hash[n - 1]--;
                hash[0]++;
            }
            else
            {
                int i = index[ch];
                if(hash[i - 1] == 0) return -1;
                hash[i - 1]--;hash[i]++;
            }
        }
        for(int i = 0;i < n - 1;i++)
            if(hash[i] != 0)
                return -1;
        return hash[n - 1];
    }
};
相关推荐
fpcc2 小时前
c++编程实践—堆和栈越界调试
c++
yi0112 小时前
LeetCode 219:存在重复元素 II——哈希表记录“最近一次出现的位置”
数据结构·人工智能·笔记·python·算法·leetcode·哈希表
YangYang9YangYan3 小时前
2027 秋招|数据科学与大数据技术面试项目价值回答思路,告别纯代码描述
大数据·面试·职场和发展
weiabc3 小时前
MSYS2 UCRT64 + g++ C++ 中文输出乱码 完整全过程
c++
灯澜忆梦5 小时前
【面向对象编程C++】| 基础语法
java·c++·算法
禾小西5 小时前
Redis 数据结构:快速的 Redis 有哪些慢操作?
数据结构·数据库·redis
小蒋观天下5 小时前
专项方案:大场景港口AI安防、多干扰环境下的算法调优与落地实操
人工智能·深度学习·算法·安全·机器学习·计算机视觉·ai大模型
Evand J5 小时前
【电机滤波例程5】渐消因子自适应扩展卡尔曼滤波(EKF),MATLAB。PMSM负载突变下的状态估计。附下载链接
算法·matlab
H.莓飛5 小时前
【数据结构】二叉树_OJ题
linux·开发语言·数据结构·算法
好想像大佬一样能够ak所有6 小时前
Leetcode35.搜索插入位置
数据结构·算法·leetcode