# 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();
}
}


## 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;
}
}


## 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;
}
}


## 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;
}
}


©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客