290. 单词规律 532. 数组中的 k-diff 数对

290. 单词规律

在这里插入图片描述

解法

在这里插入图片描述

python
class Solution:
    def wordPattern(self, pattern: str, s: str) -> bool:
        #字典,一一映射的关系
        wordS=dict()
        wordP=dict()
        #split,遇到空格分开单词
        words=s.split()
        #如果长度不相等,则返回false
        if len(wordP)!=len(wordS):
            print(1)
            return False
        # 双向连接
        for ss,p in zip(words,pattern) :
            if ( ss in wordS and wordS[ss]!=p) or (p in wordP and wordP[p]!=ss ):
                return False
            wordS[ss]=p
            wordP[p]=ss
        return True
    

c++
//std::string 具有丰富的字符串操作和功能,如连接、拆分、查找、替换等。
//char 主要用于表示和存储单个字符。
class Solution {
public:
    bool wordPattern(string pattern, string s) {
        unordered_map<string,char>str2h;
        unordered_map<car,string>char2h;
        int m =s.length();
        int i =0;
        for (auto ch:pattern){
            if(i>=m){
                return false;
            }
            int j = i
            while (j<m && s[j] !=" ")j++;
            //str.substr() 是C++中用于从一个字符串中提取子字符串的函数
            const string &tmp =s.substr(i,j-1);
            //str2h.count(tmp) 是一种在C++中使用 std::unordered_map 或 std::map 数据结构的方法,用于计算某个键(tmp)在映射中出现的次数
            if (str2h.count(tmp) && str2h[tmp] !=ch){
                return false;
            }
             if (char2h.count(ch) && char2h[ch] !=tmp){
                return false;
            }
            str2h[tmp]=ch;
            char2h[ch]=tmp;
            //跳过空格
            i=j+1;
        }
        return true;
    }
};

532. 数组中的 k-diff 数对

在这里插入图片描述

解法

在这里插入图片描述

python
#自己的解法
class Solution:
    def findPairs(self, nums: List[int], k: int) -> int:
        #两个for循环
        k_diff=0
        n=0
        #将数组去重,a是去重以后的
        a=set(nums)
        a=list(a)
        #如果k是0,则返回去重前和去重后的值
        if k==0:
            b=nums
            #列表删除元素,选remove
            for i in a:
                b.remove(i)
            b=set(b)
            b=list(b)
            print(a,nums,b)
            return len(b)
        #k不等于0可以这么算
        for i in a:
            over_n=i+k
            low_n=i-k
            n+=1    
            for j in a[n:len(a)]:
                if j ==over_n or j ==low_n:
                    
                    k_diff+=1
        return k_diff

c++

//哈希表的解法

class Solution {
public:
    int findPairs(vector<int>& nums, int k) {
        //创建两个集合
        unordered_set<int> a;
        unordered_set<int> b;
        for (int i : nums){
            if(a.count(i-k)){
                //给集合添加元素
                b.emplace(i-k);
                // cout<<i;
            }
            if(a.count(i+k)){
                //给集合添加元素
                b.emplace(i);
            }
            a.emplace(i);
        }
        for (int num :b) {
        std::cout << num << " ";
    }
   
        return b.size();
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值