leetcode 990. 等式方程的可满足性

给定一个由表示变量之间关系的字符串方程组成的数组,每个字符串方程 equations[i] 的长度为 4,并采用两种不同的形式之一:"a==b" 或 "a!=b"。在这里,a 和 b 是小写字母(不一定不同),表示单字母变量名。

只有当可以将整数分配给变量名,以便满足所有给定的方程时才返回 true,否则返回 false。 

 

题解:

1.一个数组,元素是变量之间关系的字符串方程

2.每个元素固定长度为4

3.元素只有两种形式:"a==b" 或 "a!=b"

4.将整数分配给变量名时,满足所有方程关系,返回true,否则false

 

示例 1:

输入:["a==b","b!=a"]

输出:false

解释:如果我们指定,a = 1 且 b = 1,那么可以满足第一个方程,但无法满足第二个方程。没有办法分配变量同时满足这两个方程。

示例 2:

输出:["b==a","a==b"]

输入:true

解释:我们可以指定 a = 1 且 b = 1 以满足满足这两个方程。

示例 3:

输入:["c==c","b==d","x!=z"]

输出:true

 

提示:

1 <= equations.length <= 500

equations[i].length == 4

equations[i][0] 和 equations[i][3] 是小写字母

equations[i][1] 要么是 '=',要么是 '!'

equations[i][2] 是 '='

 

解题思路:

  • 等式只有26个小写字母

  • 用一个数组hash这些小写字母,字母相对a的偏移量即数组中下标相对0的偏移量,映射字母成数字

  • 等式只有两种形式:“==”或“!=”,由于等式固定长度为4,可以通过第二个字符区分=或!

  • “==”时,提取出等式左右两边的字母,以偏移量在数组中建立等于映射关系,等号左边字母下标位置放置等号右边字母在数组中的下标,这样相等的字母根绝<index,value>都会连成串

  • "!="时,也是提取出等式左右两边的字母,判断数组中这两个字母是否有等于关系,如果有等于关系又因为是“!=”,即false

  • 如果判断完“!=”的情况,没有false,则一定为ture

C/C++题解:

class Solution {

public:

    bool equationsPossible(vector<string>& equations) {

        int length = equations.size();

        vector<int> parent(26);

        for (int i = 0; i < 26; i++) {

            parent[i] = i; } //字母到数字映射

 

        for (string str : equations) {

            if (str[1] == '=') {//等号的情况,因为长度固定为4

                int index1 = str[0] - 'a';//等式左

                int index2 = str[3] - 'a';//等式右

                //相等关系做映射

                unionVec(parent, index1, index2); } }

        for (string str : equations) {

            if (str[1] == '!') { //不等号的情况

                int index1 = str[0] - 'a';

                int index2 = str[3] - 'a';

                if (findVec(parent, index1) == findVec(parent, index2)) {

                    return false; }}//因为是不等号,如果发现有相等映射关系即错误

        }//没有错误的话即为正确等式

        return true;}

    //并,idx1所在位置上的数是idx2在parent里的位置下标,本身下标代表原值在26个字母中的偏移量

    void unionVec(vector<int>& parent, int index1, int index2) {

        parent[findVec(parent, index1)] = findVec(parent, index2); }

    //查询,在parent数组中找index下标

    int findVec(vector<int>& parent, int index) {

        while (parent[index] != index) {

            parent[index] = parent[parent[index]];

            index = parent[index]; }

        return index; }};//返回下标索引

Debug结果:

Java题解:

class Solution {

    public boolean equationsPossible(String[] equations) {

        int length = equations.length;

        int[] parent = new int[26];

        for (int i = 0; i < 26; i++) {

            parent[i] = i; }//字母到数字映射

        for (String str : equations) {

            if (str.charAt(1) == '=') {//等号的情况,因为长度固定为4

                int index1 = str.charAt(0) - 'a';//等式左

                int index2 = str.charAt(3) - 'a';//等式右

                //相等关系做映射,

                union(parent, index1, index2);}}

        for (String str : equations) {

            if (str.charAt(1) == '!') { //不等号的情况

                int index1 = str.charAt(0) - 'a';

                int index2 = str.charAt(3) - 'a';

                if (find(parent, index1) == find(parent, index2)) {

                    return false;  }}//因为是不等号,如果发现有相等映射关系即错误

        }//没有错误的话即为正确等式

        return true;}

    //并,idx1所在位置上的数是idx2在parent里的位置下标,本身下标代表原值在26个字母中的偏移量

    public void union(int[] parent, int index1, int index2) {

        parent[find(parent, index1)] = find(parent, index2); }

    //查询,在parent数组中找index下标

    public int find(int[] parent, int index) {

        while (parent[index] != index) {

            parent[index] = parent[parent[index]];

            index = parent[index];}

        return index;}}//返回下标索引

Debug结果:

Python题解:

class Solution(object):

    def equationsPossible(self, equations):

        """:type equations: List[str]:rtype: bool"""

        #并,idx1所在位置上的数是idx2在parent里的位置下标,本身下标代表原值在26个字母中的偏移量

        def unionVec(parent, index1, index2):

            parent[findVec(parent, index1)] = findVec(parent, index2)

        #查询,在parent数组中找index下标

        def findVec(parent, index):

            while parent[index] != index:

                parent[index] = parent[parent[index]]

                index = parent[index]

            return index#返回下标索引

        length = len(equations)

        parent = [i for i in range(26)]#字母到数字映射

        for s in equations:

            if s[1] == '=': #等号的情况,因为长度固定为4

                index1 = ord(s[0]) - ord('a') #等式左

                index2 = ord(s[3]) - ord('a') #等式右

                #相等关系做映射

                unionVec(parent, index1, index2)

        for s in equations:

            if s[1] == '!': #不等号的情况

                index1 = ord(s[0]) - ord('a') #等式左

                index2 = ord(s[3]) - ord('a') #等式右

                if  findVec(parent, index1) == findVec(parent, index2):

                    return False #因为是不等号,如果发现有相等映射关系即错误

        #没有错误的话即为正确等式

        return True

Debug结果:

更多题解移步公众号免费获取

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值