leetcode题解记录——数组篇

目录

1.Max Consecutive Ones 最大连续的

2.Find Numbers with Even Number of Digits 查找具有偶数位数的数字

3.Squares of a Sorted Array 有序数组的平方 


1.Max Consecutive Ones 连续最大数目

Given a binary array nums, return the maximum number of consecutive 1's in the array.

给定一个二进制数组nums,返回数组中连续最大数目1

Example 1:

Input: nums = [1,1,0,1,1,1]
Output: 3
Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
解释:前两位或后三位是连续的 1。连续 1 的最大数量为 3。

Example 2:

Input: nums = [1,0,1,1,0,1]
Output: 2

Constraints:

  • 1 <= nums.length <= 105
  • nums[i] is either 0 or 1.
class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        int max = 0; 
        int count = 0;
        if(null != nums && nums.length > 0){   
            for(int i=0;i<nums.length;i++){
                if(nums[i] == 1){
                   count += 1;
                }else{ 
                    count = 0;  
                }
                if(count > max){
                    max = count;
                }
            }  
        }
        return max;
    }
}

2.Find Numbers with Even Number of Digits 查找具有偶数位数的数字

Given an array nums of integers, return how many of them contain an even number of digits.

给定一个nums整数数组,返回其中有多少包含偶数位数。

Example 1:

Input: nums = [12,345,2,6,7896]
Output: 2
Explanation: 
12 contains 2 digits (even number of digits). 
345 contains 3 digits (odd number of digits). 
2 contains 1 digit (odd number of digits). 
6 contains 1 digit (odd number of digits). 
7896 contains 4 digits (even number of digits). 
Therefore only 12 and 7896 contain an even number of digits.

Example 2:

Input: nums = [555,901,482,1771]
Output: 1 
Explanation: 
Only 1771 contains an even number of digits.

Constraints:

  • 1 <= nums.length <= 500
  • 1 <= nums[i] <= 105
class Solution {
    public int findNumbers(int[] nums) {
        int count = 0;
        if(null != nums && nums.length > 0){
            for(int i=0;i<nums.length;i++){
                if(String.valueOf(nums[i]).length() % 2 == 0){
                    count += 1;
                } 
            }
        }
        return count; 
    }
}

3.Squares of a Sorted Array 有序数组的平方 

Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.

给定一个按非降序nums排序的整数数组,返回按非降序排序的每个数字的平方组成的数组

Example 1:

Input: nums = [-4,-1,0,3,10]
Output: [0,1,9,16,100]
Explanation: After squaring, the array becomes [16,1,0,9,100].
After sorting, it becomes [0,1,9,16,100].

Example 2:

Input: nums = [-7,-3,2,3,11]
Output: [4,9,9,49,121]

Constraints:

  • 1 <= nums.length <= 104
  • -104 <= nums[i] <= 104
  • nums is sorted in non-decreasing order.

Follow up: Squaring each element and sorting the new array is very trivial, could you find an O(n) solution using a different approach?

class Solution {
    public int[] sortedSquares(int[] nums) {
        int[] newArray = new int[nums.length];
        if(nums.length > 0){
            for(int i=0;i<nums.length;i++){
                newArray[i] = nums[i]*nums[i];
            }
        }
        Arrays.sort(newArray);
        return newArray; 
    }
}


版权声明:本文为yuzongtao原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。