import java.util.Hashtable;
public static List subdomainVisits(String[] cpdomains) {
List out = new ArrayList();
Hashtable totals = new Hashtable();
for (int i = 0; i < cpdomains.length; i++) {
String[] cpDomainsSplit = cpdomains[i].split(" ");
String visits = cpDomainsSplit[0];
String cpdomain = cpDomainsSplit[1];
String[] parts = cpdomain.split("\\.");
for (int x = 0; x = 0; j--) {
String currentPart = "";
for (int k = j; k < parts.length; k++) {
currentPart += (parts[k]);
}
if (totals.get(currentPart) != null) {
int exist = totals.get(currentPart);
totals.replace(currentPart, totals.get(currentPart) + new Integer(visits));
} else
totals.put(currentPart, new Integer(visits));
}
}
for (String key : totals.keySet()) {
out.add(totals.get(key) + " " + key);
}
return out;
}
Monthly Archives: April 2018
561. Array Partition I
class Solution {
public int arrayPairSum(int[] nums) {
Arrays.sort(nums);
int result = 0;
for (int i = 0; i < nums.length; i += 2) {
result += nums[i];
}
return result;
}
}
728. Self Dividing Numbers
class Solution {
public static boolean isSelfDividing( int i){
for ( int x = i ; x > 0 ; x /=10){
int m = x % 10;
if( m == 0 /*to avoid i%0*/|| i % m != 0) return false;
}
return true;
}
public List selfDividingNumbers(int left, int right) {
List out = new ArrayList();
for ( int i = left ; i <= right ; i++ ){
if ( isSelfDividing(i)) out.add(i);
}
return out;
}
}
657. Robot Return to Origin
class Solution {
public boolean judgeCircle(String moves) {
int x = 0;
int y = 0;
for (int i = 0; i &lt; moves.length(); i++) {
char c = moves.charAt(i);
if (c == 'R') {
x++;
} else if (c == 'L') {
x--;
} else if (c == 'U') {
y--;
} else if (c == 'D') {
y++;
}
}
if (x == 0 &amp;&amp; y == 0)
return true;
else
return false;
}
}