文章目录
子矩阵的最大累加和
整体代码
java
package 每日算法学习打卡.算法打卡.七月份.七月三十一号;
import java.util.Arrays;
public class test1 {
//子矩阵的最大累加和
public static void main(String[] args) {
int[][] matrix = {
{}
};
System.out.println(maxSum(matrix));
}
public static int maxSum(int[][] matrix) {
int beginRow = 0;
if(matrix.length == 0 || matrix[0].length == 0){
return 0;
}
int M = matrix.length;
int N = matrix[0].length;
int[] sums = new int[N];
int max = 0;
while (beginRow < M) {
for (int i = beginRow; i < M; i++) {
for (int j = 0; j < N; j++) {
sums[j] += matrix[i][j];
}
int t = findByDp(sums);
if (t > max) {
max = t;
}
}
//快速将sums的每个元素都设为0
Arrays.fill(sums, 0);
beginRow++;
}
return max;
}
//使用递推法 来求子数组的最大累加和
static int findByDp(int[] arr) {
int sumI = arr[0];
int maxSum = sumI;
for (int i = 1; i < arr.length; i++) {
if(sumI >= 0){
sumI+= arr[i];
}else{
sumI = arr[i];
}
if(sumI > maxSum){
maxSum = sumI;
}
}
return maxSum;
}
}
矩阵运算-乘法
整体代码
java
package 每日算法学习打卡.算法打卡.七月份.七月三十一号;
public class test2 {
public static void main(String[] args) {
}
/*
* 矩阵乘法
* 矩阵1为n*m矩阵,矩阵2为m*p矩阵
* 结果为n*p矩阵
*
* */
public static long[][] matrixMultiply(long[][] m1, long[][] m2){
final int n = m1.length;
final int m = m1[0].length;
if (m != m2.length) throw new IllegalArgumentException();
final int p = m2[0].length;
long[][] result = new long[n][p]; //新矩阵的行数为m1的行数,列数为m2的列数
for (int i = 0; i < n; i++){//m1的每一行
for (int j = 0; j < p; j++){//m2的每一列
for (int k = 0; k < m; k++){
result[i][j] += m1[i][k] * m2[k][j];
}
}
}
return result;
}
}
检测字符串是否有重复字符
整体代码
java
package 每日算法学习打卡.算法打卡.七月份.七月三十一号;
import com.sun.xml.internal.ws.util.StringUtils;
public class test3 {
//检测字符串是否有重复字符
public static void main(String[] args) {
String s = "asdff";
System.out.println(checkDifferent(s));
}
public static boolean checkDifferent(String s) {
if (s == null || s.isEmpty()) {
return true;
}
int[] flag = new int[128];
for (int i = 0; i < s.length(); i++) {
int c = (int) s.charAt(i);
if (flag[c] > 0) return false;
else flag[c]++;
}
return true;
}
}
反转字符串
整体代码
java
package 每日算法学习打卡.算法打卡.七月份.七月三十一号;
public class test4 {
public static void main(String[] args) {
String s = "adfa";
System.out.println(reverseString(s));
}
public static String reverseString(String s){
int len = s.length();
char[] c = new char[len];
for(int i =0;i<len;i++){
c[i] = s.charAt(len-1-i);
}
return new String(c);
}
//使用java中自带的api来进行翻转
public static String reverseString1(String a){
StringBuffer sb = new StringBuffer(a);
return sb.reverse().toString();
}
}
变形词
整体代码
java
package 每日算法学习打卡.算法打卡.七月份.七月三十一号;
import java.util.Arrays;
public class test4 {
public boolean checkSam(String A,String B){
int len1=A.length();
int len2=B.length();
if(len1 != len2){
return false;
}
char[] arr1 = A.toCharArray();
char[] arr2 = B.toCharArray();
Arrays.sort(arr1);
Arrays.sort(arr2);
return Arrays.equals(arr1,arr2);
}
}
替换字符串中的空格
整体代码
java
package 每日算法学习打卡.算法打卡.七月份.七月三十一号;
import java.sql.SQLOutput;
public class test5 {
public static void main(String[] args) {
System.out.println(replaceSpace("hahah adfad adsfas"));
}
//第一种方式
public static String replaceSpace(String s){
return s.replaceAll("\\s","%20");
}
//第二种方式
public static String replaceSpace1(char[] s){
int count = s.length;
for(int i =0;i<s.length;i++){
if(s[i] == ' '){
count+=2;
}
}
int p1 = s.length-1;
int p2 = count-1;
for(;p1>=0;p1--){
if(s[p1] == ' '){
s[p2--] = '0';
s[p2--] = '2';
s[p2--] = '%';
}else{
s[p2--] = s[p1];
}
}
return new String(s);
}
}