Number of islands
Give a 01 matrix to find the number of different islands.
0 represents the sea, 1 represents the island, if two 1 are adjacent, then the two 1 belong to the same island. We only consider the next and next left and right adjacent. Sample Example
[
[1, 1, 0, 0, 0],
[0, 1, 0, 0, 1],
[0, 0, 0, 1, 1],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 1]
]
As on the matrix there are 3 islands. Algorithmic Solutions
This problem is more complex and requires a deep traversal, using recursion to convert the identified islands from true to false. And recursively around his other islands.
public class Solution {/** * @param grid a Boolean 2D matrix * @return An integer */public int numislands (bo
Olean[][] grid) {//Write your code here if (grid.length==0) return 0;
int row = Grid.length;
int column = Grid[0].length;
int count = 0;
for (int i = 0;i < row;i++) {for (int j = 0;j < column;j++) {if (grid[i][j]==true) {
Dfs_islands (GRID,I,J);
count++;
}}} return count; } private void Dfs_islands (boolean[][] grid, int i, int j) {if (i<0| | j<0| | i>=grid.length| |
J>=grid[0].length) return;
if (grid[i][j]==true) {grid[i][j]=false;
Dfs_islands (Grid, I-1, J);
Dfs_islands (Grid, I+1, J);
Dfs_islands (grid, I, j-1);
Dfs_islands (grid, I, j+1); }
}
}
Effective palindrome string
Given a string, determine whether it is a palindrome string. Contains only letters and numbers, ignoring the case.
Precautions
Have you ever considered that a string might be an empty string. This is a question that interviewers often ask during an interview.
In this topic, we determine an empty string as a valid palindrome. Sample Example
"A man, a plan, a Canal:panama" is a palindrome.
"Race a Car" is not a palindrome. algorithm
public class Solution {/* * @param s:a String * @return: Whether The string is A valid Palindrome */public Stati
C Boolean ispalindrome (String s) {//Write your code here Boolean index = true;
if (S.length () ==0) return index; string[] str = s.
Tolowercase.split ("");
int low = 0;
int high = S.length ()-1; while (Low
The above algorithm is completely I gather out, embarrassed AH.
The following is written by others, give reference to:
public class Solution {/** * @param s a string * @return Whether The string is a valid Palindrome */public Boolea
N Ispalindrome (String s) {//Write your code here if (S.equals ("")) return true;
int len = S.length ();
int left = 0;
int right = len-1;
s = s.tolowercase ();
while (left < right) {char Leftchar = S.charat (left);
Char Rightchar = S.charat (right);
while (!isvalid (Leftchar)) {left + +;
Leftchar = S.charat (left);
if (left>=right) return true;
} while (!isvalid (Rightchar)) {right--;
Rightchar = S.charat (right);
if (Right<=left) return true;
} if (Leftchar! = Rightchar) return false;
Left + +;
Right--;
} return true; } public boolean isValid (char ch) {if (cH>= ' a ' && ch <= ' z ') return true;
if (ch >= ' 0 ' && ch <= ' 9 ') return true;
return false;
}
}
Longest ascending subsequence
Given an array of integers (subscript from 0 to N-1, n for the entire array size), find the longest ascending continuous subsequence in the array. (The longest ascending continuous subsequence can be defined as a right-to-left or left-to-right sequence.) )
Precautions
Time Sample
Given [5, 4, 2, 1, 3], its longest ascending continuous subsequence (LICs) is [5, 4, 2, 1] and returns 4.
Given [5, 1, 2, 3, 4], its longest ascending continuous subsequence (LICs) is [1, 2, 3, 4] and returns 4. algorithm
public class Solution {/* * @param a:an array of Integer * @return: an Integer */public int long Estincreasingcontinuoussubsequence (int[] A) {//write your code here if (a.length==0) return 0
;
int up = 1;
int _up = 1;
int _down=1;
int down=1;
for (int i=1;i<a.length;i++) {if (a[i]>a[i-1]) {_up = _up+1;
if (_down>=down) {down = _down;
_down = 1;
}else _down = 1;
} if (A[i]<a[i-1]) {_down = _down+1;
if (_up>=up) {up = _up;
_UP = 1;
}else _up=1;
}} if (_up>=up) up = _up;
if (_down>=down) down = _down;
if (down>=up) return down; else RetuRN up;
}
}