This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int minDistance(String word1, String word2) { | |
int len1 = word1.length(); | |
int len2 = word2.length(); | |
// len1+1, len2+1, because finally return dp[len1][len2] | |
int[][] dp = new int[len1 + 1][len2 + 1]; | |
for (int i = 0; i <= len1; i++) { | |
dp[i][0] = i; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class InversionCount { | |
public static void main(String[] args) { | |
int[] input = { 2, 4, 1, 3, 5 }; | |
System.out.println("Inversion count :" + invCount(input)); | |
} | |
private static int invCount(int[] arr) { | |
if (arr.length < 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class MatrixRotation { | |
public static void main(String[] args) { | |
System.out.println("Matrix rotation"); | |
int[][] a = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } }; | |
print2DArray(a); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MajorityElement { | |
public static void main(String[] args) { | |
System.out.println("Majority Element"); | |
int a[] = { 1, 3, 3, 1, 3 }; | |
printMajority(a); | |
} | |
private static void printMajority(int[] a) { | |
int size = a.length; | |
int element = findCandidate(a, size); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ITOA { | |
public static String convert(int x, int base) { | |
boolean negative = false; | |
String s = ""; | |
if (x == 0) | |
return "0"; | |
negative = (x < 0); | |
if (negative) | |
x = -1 * x; | |
while (x != 0) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class KthLargest { | |
public static int findKLargest(int[] ar, int low, int high, int k) { | |
int pivotIndex = partition(ar, low, high); | |
if (pivotIndex == k) { | |
return ar[pivotIndex]; | |
} | |
else if (pivotIndex > k) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class tsum { | |
public static void main(String[] args) { | |
int A[] = { 1, 4, 45, 6, 10, 8 }; | |
int sum = 19; | |
boolean t = threeSumTo(A, sum); | |
System.out.println(t); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ArrayRotate { | |
static void leftRotate(int arr[], int d, int n) { | |
rvereseArray(arr, 0, d - 1); | |
rvereseArray(arr, d, n - 1); | |
rvereseArray(arr, 0, n - 1); | |
} | |
static void rvereseArray(int arr[], int start, int end) { | |
int i; | |
int temp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StringPermutations { | |
public static void main(String[] args) { | |
permuteString("abc"); | |
} | |
private static void permuteString(String str) { | |
permutation("", str); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cccc; | |
public class PatternInMatrix { | |
static boolean used[][]; | |
public static boolean findPattern(char[][] matrix, int nRow, int nCol, char[] pattern) { | |
used = new boolean[nRow][nCol]; | |
for (int i = 0; i < nRow; i++) | |
for (int j = 0; j < nCol; j++) { |