leetcode_990等式方程的课满足性(并查集)
//数组解法
1 //给定一个由表示变量之间关系的字符串方程组成的数组,每个字符串方程 equations[i] 的长度为 4,并采用两种不同的形式之一:"a==b" 或 "a! 2 //=b"。在这里,a 和 b 是小写字母(不一定不同),表示单字母变量名。 3 // 4 // 只有当可以将整数分配给变量名,以便满足所有给定的方程时才返回 true,否则返回 false。 5 // 6 // 7 // 8 // 9 // 10 // 11 // 示例 1: 12 // 13 // 输入:["a==b","b!=a"] 14 //输出:false 15 //解释:如果我们指定,a = 1 且 b = 1,那么可以满足第一个方程,但无法满足第二个方程。没有办法分配变量同时满足这两个方程。 16 // 17 // 18 // 示例 2: 19 // 20 // 输入:["b==a","a==b"] 21 //输出:true 22 //解释:我们可以指定 a = 1 且 b = 1 以满足满足这两个方程。 23 // 24 // 25 // 示例 3: 26 // 27 // 输入:["a==b","b==c","a==c"] 28 //输出:true 29 // 30 // 31 // 示例 4: 32 // 33 // 输入:["a==b","b!=c","c==a"] 34 //输出:false 35 // 36 // 37 // 示例 5: 38 // 39 // 输入:["c==c","b==d","x!=z"] 40 //输出:true 41 // 42 // 43 // 44 // 45 // 提示: 46 // 47 // 48 // 1 <= equations.length <= 500 49 // equations[i].length == 4 50 // equations[i][0] 和 equations[i][3] 是小写字母 51 // equations[i][1] 要么是 '=',要么是 '!' 52 // equations[i][2] 是 '=' 53 // 54 // Related Topics 并查集 图 55 // 👍 124 👎 0 56 57 58 //leetcode submit region begin(Prohibit modification and deletion) 59 class Solution { 60 private int find(int[] parents,int i){ 61 if(parents[i] == i){ 62 return i; 63 } 64 return find(parents,parents[i]); 65 } 66 private void union(int[] parents,int i,int j){ 67 int x = find(parents,i); 68 int y = find(parents,j); 69 if(x != y){ 70 parents[x] = y; 71 } 72 } 73 public boolean equationsPossible(String[] equations) { 74 int[] parents = new int[26]; 75 for(int i = 0; i < 26; i++){ 76 parents[i] = i; 77 } 78 for(String str : equations){ 79 if(str.charAt(1) == '='){ 80 int indexi = (int)(str.charAt(0)-'a'); 81 int indexj = (int)(str.charAt(3)-'a'); 82 union(parents,indexi,indexj); 83 } 84 } 85 for(String str : equations){ 86 if(str.charAt(1) == '!'){ 87 int indexi = (int)(str.charAt(0)-'a'); 88 int indexj = (int)(str.charAt(3)-'a'); 89 int x = find(parents,indexi); 90 int y = find(parents,indexj); 91 if(x == y){ 92 return false; 93 } 94 } 95 } 96 return true; 97 } 98 } 99 //leetcode submit region end(Prohibit modification and deletion)
100//info 101// 解答成功: 102// 执行耗时:1 ms,击败了100.00% 的Java用户 103// 内存消耗:39.2 MB,击败了87.31% 的Java用户
hashmap解法:
1 //给定一个由表示变量之间关系的字符串方程组成的数组,每个字符串方程 equations[i] 的长度为 4,并采用两种不同的形式之一:"a==b" 或 "a! 2 //=b"。在这里,a 和 b 是小写字母(不一定不同),表示单字母变量名。 3 // 4 // 只有当可以将整数分配给变量名,以便满足所有给定的方程时才返回 true,否则返回 false。 5 // 6 // 7 // 8 // 9 // 10 // 11 // 示例 1: 12 // 13 // 输入:["a==b","b!=a"] 14 //输出:false 15 //解释:如果我们指定,a = 1 且 b = 1,那么可以满足第一个方程,但无法满足第二个方程。没有办法分配变量同时满足这两个方程。 16 // 17 // 18 // 示例 2: 19 // 20 // 输入:["b==a","a==b"] 21 //输出:true 22 //解释:我们可以指定 a = 1 且 b = 1 以满足满足这两个方程。 23 // 24 // 25 // 示例 3: 26 // 27 // 输入:["a==b","b==c","a==c"] 28 //输出:true 29 // 30 // 31 // 示例 4: 32 // 33 // 输入:["a==b","b!=c","c==a"] 34 //输出:false 35 // 36 // 37 // 示例 5: 38 // 39 // 输入:["c==c","b==d","x!=z"] 40 //输出:true 41 // 42 // 43 // 44 // 45 // 提示: 46 // 47 // 48 // 1 <= equations.length <= 500 49 // equations[i].length == 4 50 // equations[i][0] 和 equations[i][3] 是小写字母 51 // equations[i][1] 要么是 '=',要么是 '!' 52 // equations[i][2] 是 '=' 53 // 54 // Related Topics 并查集 图 55 // 👍 124 👎 0 56 57 58 import java.util.HashMap; 59 60 //leetcode submit region begin(Prohibit modification and deletion) 61 class Solution { 62 private char find(HashMap<Character,Character> parents,char key){ 63 if(parents.get(key) == key){ 64 return key; 65 } 66 return find(parents,parents.get(key)); 67 } 68 private void union(HashMap<Character,Character> parents,char key1,char key2){ 69 char pkey1 = find(parents,key1); 70 char pkey2 = find(parents,key2); 71 if(pkey1 != pkey2){ 72 parents.remove(pkey1); 73 parents.put(pkey1,pkey2); 74 } 75 } 76 public boolean equationsPossible(String[] equations) { 77 HashMap<Character,Character> parents = new HashMap<>(); 78 for(int i = 0; i < 26; i++){ 79 parents.put((char)('a'+i),(char)('a'+i)); 80 } 81 for(String str : equations){ 82 if(str.charAt(1) == '='){ 83 char key1 = str.charAt(0); 84 char key2 = str.charAt(3); 85 union(parents,key1,key2); 86 } 87 } 88 for(String str : equations){ 89 if(str.charAt(1) == '!'){ 90 char key1 = str.charAt(0); 91 char key2 = str.charAt(3); 92 char pkey1 = find(parents,key1); 93 char pkey2 = find(parents,key2); 94 if(pkey1 == pkey2){ 95 return false; 96 } 97 } 98 } 99 return true; 100 } 101 } 102 //leetcode submit region end(Prohibit modification and deletion) 103 //info 104 // 解答成功: 105 // 执行耗时:3 ms,击败了20.52% 的Java用户 106 // 内存消耗:39.1 MB,击败了92.64% 的Java用户

浙公网安备 33010602011771号