682. Baseball Game

682. Baseball Game

class Solution {
			public int calPoints(String[] ops) {
		int currentSum = 0;
		ArrayList valid = new ArrayList();
		for (int i = 0; i < ops.length; i++) {

			if (ops[i].equals("C")) {
				currentSum -= new Integer(valid.get(valid.size() - 1));
				valid.remove(valid.size() - 1);
			} else if (ops[i].equals("D")) {
				int newScore = (2 * valid.get(valid.size() - 1));
				currentSum += newScore;
				valid.add(newScore);
			} else if (ops[i].equals("+")) {
				int newScore = new Integer(valid.get(valid.size() - 1)) + new Integer((valid.get(valid.size() - 2)));
				currentSum += newScore;
				valid.add(newScore);
			} else {
				valid.add(new Integer(ops[i]));
				currentSum += new Integer(ops[i]);
			}
		}
		return currentSum;

	}
}
Advertisements

500. Keyboard Row

500. Keyboard Row

class Solution {
	public static String[] findWords(String[] words) {
		String[] strs = { "QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM" };
		HashMap map = new HashMap();
		for (int i = 0; i < strs.length; i++) {
			for (char c : strs[i].toCharArray()) {
				map.put(c, i);// put  pair into the map
			}
		}
		ArrayList outArrList = new ArrayList();
		for (int j = 0; j < words.length; j++) {
			HashSet row = new HashSet();
			for (int i = 0; i < words[j].length(); i++) {
				Character current = words[j].charAt(i);
				row.add(map.get(Character.toUpperCase(current)));

			}
			if (row.size() == 1)
				outArrList.add(words[j]);
		}
		String[] out = new String[outArrList.size()];

		for (int i = 0; i < out.length; i++)
			out[i] = outArrList.get(i);
		return out;
	}
}

476. Number Complement

476. Number Complement

import java.math.BigInteger;

class Solution {
  	public  int findComplement(int num) {
		String input = Integer.toBinaryString(num);

		StringBuilder outBuilder = new StringBuilder();
		for (int i = 0; i < input.length(); i++) {
			if (input.charAt(i) == '0')
				outBuilder.append(1);
			else
				outBuilder.append(0);
		}

		BigInteger out = new BigInteger(outBuilder.toString(), 2);
		return out.intValue();
	}
}

806. Number of Lines To Write String

806. Number of Lines To Write String

class Solution {	public int[] numberOfLines(int[] widths, String S) {

		int currentChar = 0;
		if (S.length() == 0) return new int[]{0,0};
		int lines = 1;

		for (int i = 0; i < S.length(); i++) {
			currentChar += widths[S.charAt(i) - 97];
			if( currentChar > 100){
				currentChar -= widths[S.charAt(i) - 97];
				currentChar = widths[S.charAt(i) - 97];
				lines++;
			}else if( currentChar == 99 && i < S.length() - 1 /*at least there is one following char*/){
				currentChar = 0;
				lines++;
			}
		}

		return new int[] { lines, currentChar};

	}}

811. Subdomain Visit Count

811. Subdomain Visit Count

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