1.求一个整数序列的最长递增子序列。
2.编辑距离算法。即从一个字符串转换成另一个字符串的最少操作次数。允许添加,删除或是替换字母。
3.两个整数数组,从每个数组中有序取m个数,两两相乘后的和最大。求最大和。
4.求两个字符串的最长公共子串。
2.编辑距离算法。即从一个字符串转换成另一个字符串的最少操作次数。允许添加,删除或是替换字母。
3.两个整数数组,从每个数组中有序取m个数,两两相乘后的和最大。求最大和。
4.求两个字符串的最长公共子串。
java 代码
- public int getDistance(String s1, String s2) {
- int[][] array = new int[s1.length() + 1][s2.length() + 1];
- for (int i = 0; i <= s1.length(); i++) {
- array[i][0] = i;
- }
- for (int j = 0; j <= s2.length(); j++) {
- array[0][j] = j;
- }
- for (int i = 1; i <= s1.length(); i++) {
- for (int j = 1; j <= s2.length(); j++) {
- array[i][j] = Integer.MAX_VALUE;
- array[i][j] = Math.min(array[i][j], array[i - 1][j] + 1);
- array[i][j] = Math.min(array[i][j], array[i][j - 1] + 1);
- int c = s1.charAt(i - 1) == s2.charAt(j - 1) ? 0 : 1;
- array[i][j] = Math.min(array[i][j], array[i - 1][j - 1] + c);
- }
- }
- return array[s1.length()][s2.length()];
- }
java 代码
- public int getMaxMulti(int[] a, int[] b, int N) {
- int[] a1 = new int[a.length + 1];
- int[] a2 = new int[b.length + 1];
- for (int i = 0; i < a.length; i++) {
- a1[i + 1] = a[i];
- }
- for (int i = 0; i < b.length; i++) {
- a2[i + 1] = b[i];
- }
- int[][][] m = new int[N + 1][a1.length][a2.length];
- for (int k = 1; k <= N; k++) {
- for (int i = k; i < a1.length; i++) {
- for (int j = k; j < a2.length; j++) {
- int max = Integer.MIN_VALUE;
- if (i - 1 >= k) {
- max = Math.max(max, m[k][i - 1][j]);
- }
- if (j - 1 >= k) {
- max = Math.max(max, m[k][i][j - 1]);
- }
- max = Math.max(max, m[k - 1][i - 1][j - 1] + a1[i] * a2[j]);
- m[k][i][j] = max;
- }
- }
- }
- return m[N][a.length][b.length];
- }
java 代码
- //最长公共字串
- public class LongestCommonSequence {
- /**
- * @param args
- */
- public static void main(String[] args) {
- System.out
- .println(new LongestCommonSequence().getLCS("pailbyujcdfefdghijk", "abcaduefsgdhdijdfk"));
- }
- public String getLCS(String s1, String s2) {
- int[][] lens = new int[s1.length() + 1][s2.length() + 1];
- // 0 for [i][j], 1 for [i][j-1], -1 for [i-1][j]
- int[][] directions = new int[s1.length() + 1][s2.length() + 1];
- for (int i = 1; i <= s1.length(); i++) {
- for (int j = 1; j <= s2.length(); j++) {
- if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
- lens[i][j] = lens[i - 1][j - 1] + 1;
- directions[i][j] = 0;
- } else if (lens[i][j - 1] >= lens[i - 1][j]) {
- lens[i][j] = lens[i][j - 1];
- directions[i][j] = 1;
- } else {
- lens[i][j] = lens[i - 1][j];
- directions[i][j] = -1;
- }
- }
- }
- return getCommon(s1, directions, s1.length(), s2.length());
- }
- private String getCommon(String s1, int[][] directions, int len1, int len2) {
- if (len1 == 0 || len2 == 0) {
- return "";
- }
- if (directions[len1][len2] == 0) {
- return getCommon(s1, directions, len1 - 1, len2 - 1) + s1.charAt(len1 - 1);
- } else if (directions[len1][len2] == 1) {
- return getCommon(s1, directions, len1, len2 - 1);
- } else {
- return getCommon(s1, directions, len1 - 1, len2);
- }
- }
- }
本文提供了几种常见算法的Java实现,包括编辑距离算法、求两个数组中两两相乘的最大和及寻找两个字符串的最长公共子串。
4万+

被折叠的 条评论
为什么被折叠?



