Skip to content

Instantly share code, notes, and snippets.

class Solution{
/*
* Space: O(n); Time(n^2)
*/
public static void setZero(int[][] matrix){
if(matrix == null) return;
int row_num = martix.length;
int col_num = martix[0].length;
class Solution{
/*
* Space: O(1); Time: O(n^2).
*/
public static int[][] rotate(int[][] image){
if(image == null) return image;
int n = image[0].length;
if(n != image.length) return image;
class Solution{
public static int[][] rotate(int[][] image){
int n = image[0].length;
int layer = 0;
int temp;
for(;layer< n/2; layer++){
for(int i = layer; i< n - layer - 1; i++){
temp = image[layer][i];
class Solution{
/*
* Space complexity: O(n); Time complexity: O(n).
*/
public static String compression(String str){
if(str == null || str.isEmpty()) return str;
char[] chars = str.toCharArray();
StringBuffer result = new StringBuffer();
class Solution{
// Space complexity: O(n);
// Time complexity: O(n);
public static char[] replaceSpaces(char[] str){
if(str == null) return null;
if(str.length == 0 ) return str;
int length = 3 * str.length; // Triple its original size to ensure enough space to hold the result.
int i = 0; // The index of current character.
char[] result = new char[length]; // Create a new array to store the result string.
class Solution{
public static boolean isPermutation(String s1, String s2){
if(s1 === null || s2 == null ) return false;
if(s1.length() != s2.length()) return false;
if(s1.length() == 0) return true; // If s1.length() == s2.length() and s1 is a empty string, s2 is the permutation of s1.
char[] sc1 = s1.toCharArray();
char[] sc2 = s2.toCharArray();
HashMap<String, int> temp = new HashMap<String, boolean>();
/*
* Space complexity: O(1); Time complexity: O(n).
*/
void reverseString(char* str){
if(str == NULL) return; // Validation checking.
if(*str == "\0") return; // Empty string.
char temp; // For swapping tail and head.
char* head=str, tail=str; // Pointers for the beginning and the end of the string.