1. 求和
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
解题思路:
step1: 从前往后,先找到一个值,就得到了余数
step2: 查找余数是否在剩余的数组中
class Solution {
public int[] twoSum(int[] nums, int target) {
int len=nums.length;
int[] arr = new int[2];
for(int i=0;i<len;i++){
int rest=target-nums[i];
for(int j=i+1;j<len;j++){
if(rest==nums[j]){
arr[0]=i;
arr[1]=j;
}
}
}
return arr;
}
}
2. 多维数组取最大正方形面积
问题链接:https://leetcode-cn.com/problems/maximal-square/
取值方法: https://www.cnblogs.com/rrttp/p/7789109.html
int rows = array.length ;
int columns = array[0].length ;
class Solution {
public int maximalSquare(char[][] matrix) {
int maxSquare=0;
int tempMaxSquare=0;
int row=matrix.length;
int col=matrix[0].length;
int combinedShortedLen=0;
int shorterMaxLen=0;
// 取行的值
for(int i=0;i<row;i++){
int tempMaxLen=countMaxLen(matrix[i]);
int tempMaxLen1=countMaxLen(matrix[i+1])
shorterMaxLen = tempMaxLen>tempMaxLen1?tempMaxLen1:tempMaxLen;
shorterMaxLen=shorterMaxLen>combinedShortedLen?shorterMaxLen:combinedShortedLen;
int[] arr = matrix[i];
int[] arr1 = matrix[i+1];
int currentCombinedNum=0;
if(shorterMaxLen>=2){
for(int j=0;j<arr.length;j++){
if(arr[j]==1 || arr1[j]==1){
combinedShortedLen++;
if(combinedShortedLen>currentCombinedNum){
currentCombinedNum=combinedShortedLen; // 累加当前2行的最大的交集数
}
}else{
combinedShortedLen=0;
}
}
shorterMaxLen=shorterMaxLen>currentCombinedNum?shorterMaxLen:currentCombinedNum;
}else if(shorterMaxLen=1){
tempMaxSquare=1;
if(tempMaxSquare>maxSquare){
maxSquare=tempMaxSquare;
}
}else{
continue;
}
}
return shorterMaxLen*int shorterMaxLen;
}
}
// 每一行数组最大的连续数
int countMaxLen(char[] arr){
int maxLen=0;
int tempLen=0;
for(int i=0;i<arr.tempLen;i++){
arr[i]=int(arr[i]);
if(arr[i]==1)){
tempLen++;
if(tempLen>maxLen){
maxLen=tempLen;
}
}else{
tempLen=0;
}
}
return maxLen;
}
}