463. Island Perimeter

463. Island Perimeter

class Solution {
	public static int islandPerimeter(int[][] grid) {
		int perimeter = 0;
		for (int i = 0; i < grid.length; i++) {
			for (int j = 0; j < grid[i].length; j++) {
				if (grid[i][j] == 1) {
					if (i - 1 < 0 || grid[i - 1][j] == 0)
						perimeter++;
					if (j - 1 = grid.length || grid[i + 1][j] == 0)
						perimeter++;
					if (j + 1 >= grid[i].length || grid[i][j + 1] == 0)
						perimeter++;
				}

			}
		}
		return perimeter;
	}
}

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;

	}
}

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