程序员面试金典1-5

01.确定字符串是否唯一

【题目】https://leetcode.cn/problems/is-unique-lcci/description/
【考察】位运算
【思路】利用一个32位的整数判断26个字母是否出现过
【题解】

class Solution 
{
public:
    bool isUnique(string astr) 
    {
        // 如果字符串长度超过26,返回false,因为英文字母只有26个
        if (astr.size() > 26)
            return false;
        
        int ret = 0; // 初始化一个整数ret为0,用来记录字符出现情况
        for (auto i : astr) // 遍历字符串中的每个字符
        {
            int x = i - 'a'; // 将字符转换成相应的整数值('a'为0, 'b'为1, ...)
            
            // 检查对应位是否为1,即字符是否已出现过
            if ((ret >> x) & 1 == 1)
                return false; // 如果已出现过,返回false
            
            ret |= (1 << x); // 将字符对应的位置1,记录该字符已出现过
        }
        return true; // 遍历完所有字符,没有重复字符则返回true
    }
};

02.判定是否为字符重排

【题目】https://leetcode.cn/problems/check-permutation-lcci/description/
【考察】排序、哈希表
【思路1】直接排序判断结果

class Solution 
{
public:
    bool CheckPermutation(string s1, string s2) 
    {
        if (s1.size() != s2.size())
            return false;
        sort(s1.begin(), s1.end());
        sort(s2.begin(), s2.end());
        return s1 == s2;
    }
};

【思路2】用哈希表存储s1,然后遍历s2,并用s2中的字符作为key在哈希表中查询

class Solution {
public:
    bool CheckPermutation(string s1, string s2) {
        if (s1.size() != s2.size())
            return false;
        unordered_map<char,int> map;
        for (auto c : s1)
        {
            map[c]++;
        }
        for (auto c : s2)
        {
            if (map[c] == 0)
                return false;
            else
            {
                map[c]--;
            }
        }
        return true;
    }
};

03.URL化

【题目】https://leetcode.cn/problems/string-to-url-lcci/description/
【考察】字符串
【思路】直接遍历

class Solution 
{
public:
    string replaceSpaces(string S, int length) 
    {
        string res;
        for(int i = 0; i < length; ++i)
        {
            if (S[i] == ' ')
                res += "%20";           
            else
                res += S[i];
        }
        return res;
    }
};

04.回文排列

【题目】https://leetcode.cn/problems/palindrome-permutation-lcci/?envType=study-plan-v2&envId=cracking-the-coding-interview
【考察】哈希表
【思路】如果字符串长度为偶数,回文排列中每个字符只能出现偶数次;如果长度为奇数,那么只能有一个字符出现奇数次。

class Solution 
{
public:
    bool canPermutePalindrome(string s) 
    {
        unordered_map<char,int> map;
        for (auto c : s)
            map[c]++;

        int odd = 0; // 记录字符出现奇数的个数,只能为1
        for (auto pair : map)
        {
            if (pair.second % 2 == 1)
            {
                if (++odd > 1)
                    return false;
            }
        }
        return true;
    }
};

05.一次编辑

【题目】https://leetcode.cn/problems/one-away-lcci/?envType=study-plan-v2&envId=cracking-the-coding-interview
【考察】双指针
【思路】
记录两个字符串长度差值:

  1. 长度差值大于等于2,直接返回false;
  2. 差值为-1,此时first如果为second的子串,如果是子串则满足条件,否则不满足;
  3. 差值为0,检查first与second是否只有一个字符不同,如果是则满足条件,否则不满足;
  4. 差值为1,检查second是否是first的子串,如果是子串则满足条件,否则不满足。

【题解】

class Solution {
public:
    bool oneEditAway(string first, string second) 
    {
        int l1 = first.size(), l2 = second.size();
        int d = l1 - l2; // 记录两个字符串长度差值
        if (d >= 2 || d <= -2)
            return false;
        if (d == -1)
        {
            int i = 0, j = 0, n = 0;
            while (i < l1 && j < l2)
            {
                if (first[i] != second[j])
                {
                    n++;j++;
                }
                else
                {
                    i++;j++;
                }
                if (n >= 2)
                    return false;
            }
            return true;
        }
        else if (d == 1)
        {
            int i = 0, j = 0, n = 0;
            while (i < l1 && j < l2)
            {
                if (first[i] != second[j])
                {
                    n++;i++;
                }
                else
                {
                    i++;j++;
                }
                if (n >= 2)
                    return false;
            }
            return true;
        }
        else
        {
            int n = 0, i = 0;
            while (i < l1)
            {
                if(first[i] != second[i])
                    n++;
                i++;
                if (n >= 2)
                    return false;
            }
            return true;
        }
    }
};
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值