LC刷题3/26 - Facebook Tag - E 1108/E 938/E 953
1108. Defanging an IP Address
class Solution {
public String defangIPaddr(String address) {
StringBuffer buffer = new StringBuffer();
for(int i = 0; i<address.length(); i++) {
char curChar = address.charAt(i);
if(curChar == '.') {
buffer.append("[.]");
} else {
buffer.append(curChar);
}
}
return buffer.toString();
}
}
Comment
用时5分钟。这波lc上手写的第一题。久违的刷题感觉。真正开始之初惯性想着用StringBuffer,并没有朝着要写one-liner的方向想。以后要把简洁性也纳入计划里。
Solution
public String defangIPaddr(String address) {
return address.replace(".", "[.]");
}
public String defangIPaddr(String address) {
return String.join("[.]", address.split("\\."));
}
public String defangIPaddr(String address) {
return address.replaceAll("\\.", "[.]");
}
938. Range Sum of BST
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int rangeSumBST(TreeNode root, int L, int R) {
return dfs(root, L, R);
}
public int dfs(TreeNode root, int L, int R) {
if(root == null) {
return 0;
}
int val = root.val;
int res = 0;
if(val>=L && val <= R) {
res += val;
res += dfs(root.left, L, R);
res += dfs(root.right, L, R);
}
if(val<L) {
res = dfs(root.right, L, R);
}
if (val>R) {
res = dfs(root.left, L, R);
}
return res;
}
}
Comment
用时30分钟。好久没碰ds了。其中复习了bst十分钟。要加油啊。后来主要卡在开始把top-down和bottom-up混在了一起。
和Solution的差不多。不过solution的更简洁。原因是把ans拎出来做global var.
Solution
class Solution {
int ans;
public int rangeSumBST(TreeNode root, int L, int R) {
ans = 0;
dfs(root, L, R);
return ans;
}
public void dfs(TreeNode node, int L, int R) {
if (node != null) {
if (L <= node.val && node.val <= R)
ans += node.val;
if (L < node.val)
dfs(node.left, L, R);
if (node.val < R)
dfs(node.right, L, R);
}
}
}
953. Verifying an Alien Dictionary
class Solution {
public boolean isAlienSorted(String[] words, String order) {
return recurseSearch(words, order, 0);
}
public boolean recurseSearch(String[] words, String order, int index) {
System.out.println("Index: " + index);
int previousOrderIndex = -1;
int currentOrderIndex = previousOrderIndex;
for (String iterWord:words) {
//update current
if (index>=iterWord.length()) {
currentOrderIndex = 0;
} else {
char currentChar = iterWord.charAt(index);
currentOrderIndex = order.indexOf(currentChar);
}
//false conditions
if (currentOrderIndex < previousOrderIndex) {
return false;
} else if (currentOrderIndex == previousOrderIndex) {
if(!recurseSearch(words, order, index+1))
return false;
}
//update previous
previousOrderIndex = currentOrderIndex;
}
return true;
}
}
Comment
本来以为几行搞定的结果越写越多。有些小细节是在反复的test中加上的。比如说char是空的情况和相等的情况。开始做的时候只看了框架,测过普通的情况后,看到相等的情况需要继续下一位char的比较,于是代码移到recursion里面。感觉比solution的readibility好些。>.<
Solution
class Solution {
public boolean isAlienSorted(String[] words, String order) {
int[] index = new int[26];
for (int i = 0; i < order.length(); ++i)
index[order.charAt(i) - 'a'] = i;
search: for (int i = 0; i < words.length - 1; ++i) {
String word1 = words[i];
String word2 = words[i+1];
// Find the first difference word1[k] != word2[k].
for (int k = 0; k < Math.min(word1.length(), word2.length()); ++k) {
if (word1.charAt(k) != word2.charAt(k)) {
// If they compare badly, it's not sorted.
if (index[word1.charAt(k) - 'a'] > index[word2.charAt(k) - 'a'])
return false;
continue search;
}
}
// If we didn't find a first difference, the
// words are like ("app", "apple").
if (word1.length() > word2.length())
return false;
}
return true;
}
}