1. 求和

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。

解题思路:
step1: 从前往后,先找到一个值,就得到了余数
step2: 查找余数是否在剩余的数组中

  1. class Solution {
  2. public int[] twoSum(int[] nums, int target) {
  3. int len=nums.length;
  4. int[] arr = new int[2];
  5. for(int i=0;i<len;i++){
  6. int rest=target-nums[i];
  7. for(int j=i+1;j<len;j++){
  8. if(rest==nums[j]){
  9. arr[0]=i;
  10. arr[1]=j;
  11. }
  12. }
  13. }
  14. return arr;
  15. }
  16. }

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 ;

  1. class Solution {
  2. public int maximalSquare(char[][] matrix) {
  3. int maxSquare=0;
  4. int tempMaxSquare=0;
  5. int row=matrix.length;
  6. int col=matrix[0].length;
  7. int combinedShortedLen=0;
  8. int shorterMaxLen=0;
  9. // 取行的值
  10. for(int i=0;i<row;i++){
  11. int tempMaxLen=countMaxLen(matrix[i]);
  12. int tempMaxLen1=countMaxLen(matrix[i+1])
  13. shorterMaxLen = tempMaxLen>tempMaxLen1?tempMaxLen1:tempMaxLen;
  14. shorterMaxLen=shorterMaxLen>combinedShortedLen?shorterMaxLen:combinedShortedLen;
  15. int[] arr = matrix[i];
  16. int[] arr1 = matrix[i+1];
  17. int currentCombinedNum=0;
  18. if(shorterMaxLen>=2){
  19. for(int j=0;j<arr.length;j++){
  20. if(arr[j]==1 || arr1[j]==1){
  21. combinedShortedLen++;
  22. if(combinedShortedLen>currentCombinedNum){
  23. currentCombinedNum=combinedShortedLen; // 累加当前2行的最大的交集数
  24. }
  25. }else{
  26. combinedShortedLen=0;
  27. }
  28. }
  29. shorterMaxLen=shorterMaxLen>currentCombinedNum?shorterMaxLen:currentCombinedNum;
  30. }else if(shorterMaxLen=1){
  31. tempMaxSquare=1;
  32. if(tempMaxSquare>maxSquare){
  33. maxSquare=tempMaxSquare;
  34. }
  35. }else{
  36. continue;
  37. }
  38. }
  39. return shorterMaxLen*int shorterMaxLen;
  40. }
  41. }
  42. // 每一行数组最大的连续数
  43. int countMaxLen(char[] arr){
  44. int maxLen=0;
  45. int tempLen=0;
  46. for(int i=0;i<arr.tempLen;i++){
  47. arr[i]=int(arr[i]);
  48. if(arr[i]==1)){
  49. tempLen++;
  50. if(tempLen>maxLen){
  51. maxLen=tempLen;
  52. }
  53. }else{
  54. tempLen=0;
  55. }
  56. }
  57. return maxLen;
  58. }
  59. }

3. 回文

https://leetcode-cn.com/problems/valid-palindrome/solution/