# 766. Toeplitz Matrix

766. Toeplitz Matrix

```class Solution {
public boolean isToeplitzMatrix(int[][] matrix) {

for ( int i = 1 ; i < matrix.length ; i++ ){
for ( int j = 1; j < matrix[i].length ; j++ ){
if ( matrix [i-1][j-1] != matrix[i][j]) return false;
}
}

return true;
}
}```

# 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;
} else if (ops[i].equals("+")) {
int newScore = new Integer(valid.get(valid.size() - 1)) + new Integer((valid.get(valid.size() - 2)));
currentSum += newScore;
} else {
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);

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

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

# 557. Reverse Words in a String III

557. Reverse Words in a String III

```class Solution {
public static String reversWord(String s) {
return new StringBuffer(s).reverse().toString();
}

public static String reverseWords(String s) {

int endOfFirst = s.indexOf(" ");
if (endOfFirst == -1)
return reversWord(s);
return reversWord(s.substring(0, endOfFirst)) + " " + reverseWords(s.substring(endOfFirst + 1));
}
}```

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

# 771. Jewels and Stones

771. Jewels and Stones

```class Solution {
public int numJewelsInStones(String J, String S) {
int count = 0;
for (int i = 0; i < S.length(); i++)
for (int j = 0; j < J.length(); j++)
if (S.charAt(i) == J.charAt(j))
count++;
return count;
}
}```

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

}}
```