leetcode Day32----array.easy

本文介绍了LeetCode中的三个数组题目:有效山脉数组、有序数组的平方和查询数组中偶数和的变化。提供了Java和C两种语言的解题思路及运行效率分析。
摘要由CSDN通过智能技术生成

Valid Mountain Array

Given an array A of integers, return true if and only if it is a valid mountain array.

Recall that A is a mountain array if and only if:

A.length >= 3
There exists some i with 0 < i < A.length - 1 such that:
A[0] < A[1] < … A[i-1] < A[i]
A[i] > A[i+1] > … > A[B.length - 1]

Example 1:

Input: [2,1]
Output: false
Example 2:

Input: [3,5,5]
Output: false
Example 3:

Input: [0,3,2,1]
Output: true

Note:

0 <= A.length <= 10000
0 <= A[i] <= 10000

JAVA

class Solution {
    public boolean validMountainArray(int[] A) {
        if(A.length<3) return false;
        int max_index = 0;
        for(int i=1;i<A.length;i++){
            if(A[i]>A[max_index]) max_index = i;
        }
        if(max_index == 0 || max_index == A.length-1) return false;
        for(int i=0;i<max_index;i++){
           if(A[i]<A[i+1]) continue;
           else return false; 
        }   
        for(int i=max_index;i<A.length-1;i++){
            if(A[i]>A[i+1]) continue;
            else return false;
        }   
        return true;
    }
}

Success
Details
Runtime: 1 ms, faster than 100.00% of Java online submissions for Valid Mountain Array.
Memory Usage: 39.3 MB, less than 98.50% of Java online submissions for Valid Mountain Array.

C

bool validMountainArray(int* A, int ASize){
    if(ASize<3||A[0]>=A[1]){
        return false;
    }
    int i=0;
    for(;i<ASize-1;i++){
        if(A[i]>=A[i+1]){
            break;
        }
    }
    if(i>=ASize-1){
        return false;
    }
    for(;i<ASize-1;i++){
        if(A[i]<=A[i+1]){
            return false;
        }
    }
    return true;
}


Success
Details
Runtime: 32 ms, faster than 9.09% of C online submissions for Valid Mountain Array.
Memory Usage: 8.4 MB, less than 55.56% of C online submissions for Valid Mountain Array.

Squares of a Sorted Array

Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.

Example 1:

Input: [-4,-1,0,3,10]
Output: [0,1,9,16,100]
Example 2:

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

Note:

1 <= A.length <= 10000
-10000 <= A[i] <= 10000
A is sorted in non-decreasing order.

JAVA

class Solution {
    public int[] sortedSquares(int[] A) {
       return Arrays.stream(A).map(a -> a*a).sorted().toArray(); 
    }
}
class Solution {
    public int[] sortedSquares(int[] A) {
       int i = 0, j = A.length -1, k=j;
        int[] B = new int[A.length];
        while(k>=0){
            int x = A[i]*A[i], y = A[j]*A[j];
            if(x >= y){
                B[k] = x;
                i++;
                k--;
            } else{
                B[k] = y;
                j--;
                k--;
            }
        }
        return B;
    }
}

Success
Details
Runtime: 1 ms, faster than 100.00% of Java online submissions for Squares of a Sorted Array.
Memory Usage: 40.2 MB, less than 98.65% of Java online submissions for Squares of a Sorted Array.

C

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* sortedSquares(int* A, int ASize, int* returnSize){
    int* ans = calloc(ASize, sizeof(int));
    *returnSize = ASize;
    int i = 0, j = ASize - 1, index = ASize - 1;
    while(i <= j){
        if(-A[i] > A[j]){
            ans[index--] = A[i] * A[i];
            i++;
        }
        else{
            ans[index--] = A[j] * A[j];
            j--;
        }
    }
    return ans;
}

Success
Details
Runtime: 100 ms, faster than 84.17% of C online submissions for Squares of a Sorted Array.
Memory Usage: 21.4 MB, less than 68.80% of C online submissions for Squares of a Sorted Array.

Sum of Even Numbers After Queries

We have an array A of integers, and an array queries of queries.

For the i-th query val = queries[i][0], index = queries[i][1], we add val to A[index]. Then, the answer to the i-th query is the sum of the even values of A.

(Here, the given index = queries[i][1] is a 0-based index, and each query permanently modifies the array A.)

Return the answer to all queries. Your answer array should have answer[i] as the answer to the i-th query.

Example 1:

Input: A = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]]
Output: [8,6,2,4]
Explanation:
At the beginning, the array is [1,2,3,4].
After adding 1 to A[0], the array is [2,2,3,4], and the sum of even values is 2 + 2 + 4 = 8.
After adding -3 to A[1], the array is [2,-1,3,4], and the sum of even values is 2 + 4 = 6.
After adding -4 to A[0], the array is [-2,-1,3,4], and the sum of even values is -2 + 4 = 2.
After adding 2 to A[3], the array is [-2,-1,3,6], and the sum of even values is -2 + 6 = 4.

Note:

1 <= A.length <= 10000
-10000 <= A[i] <= 10000
1 <= queries.length <= 10000
-10000 <= queries[i][0] <= 10000
0 <= queries[i][1] < A.length

JAVA

class Solution {
    public int[] sumEvenAfterQueries(int[] A, int[][] queries) {
        int[] answers = new int[queries.length];
        int evenSum=0;
        for( int x : A ) if( x%2==0 ) evenSum+=x;
        for( int i=0; i<queries.length; i++ ) {
            int val = queries[i][0];
            int index = queries[i][1];
            if( A[index]%2==0 ) evenSum-=A[index];
            A[index]+=val;
            if( A[index]%2==0 ) evenSum+=A[index];
            answers[i]=evenSum;
        }                   
        return answers;
    }
}

Success
Details
Runtime: 4 ms, faster than 98.84% of Java online submissions for Sum of Even Numbers After Queries.
Memory Usage: 57.2 MB, less than 91.39% of Java online submissions for Sum of Even Numbers After Queries.

class Solution {
    public int[] sumEvenAfterQueries(int[] A, int[][] queries) {
        int[] op =new int[A.length];
        for(int i=0;i<queries.length;i++){
            int sum=0;
            A[queries[i][1]] += queries[i][0];
            for(int j=0;j<A.length;j++){
                if(A[j]%2==0)
                    sum += A[j];
            }
            op[i] = sum;
        }
        return op;
    }
}

Success
Details
Runtime: 1196 ms, faster than 15.51% of Java online submissions for Sum of Even Numbers After Queries.
Memory Usage: 53.6 MB, less than 93.29% of Java online submissions for Sum of Even Numbers After Queries.

C

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* sumEvenAfterQueries(int* A, int ASize, int** queries, int queriesSize, int* queriesColSize, int* returnSize){
    *returnSize = 0;
    int* ans = calloc(queriesSize, sizeof(int));
    int sum = 0;
    
    for(int i = 0; i < ASize; i++){
        if(A[i] % 2 ==0){
            sum += A[i];
        }
    }

    for(int i = 0; i < queriesSize; i++){
        if(A[queries[i][1]] % 2 == 0){
            if(queries[i][0] % 2 == 0){
                A[queries[i][1]] += queries[i][0];
                sum += queries[i][0];
            }
            else{
                sum -= A[queries[i][1]];
                A[queries[i][1]] += queries[i][0];
            }
        }
        else{
            if(queries[i][0] % 2 == 0){
                A[queries[i][1]] += queries[i][0];
            }
            else{
                A[queries[i][1]] += queries[i][0];
                sum += A[queries[i][1]];
            }
        }
        ans[*returnSize] = sum;
        *returnSize += 1;
    }
    return ans;
}


Success
Details
Runtime: 156 ms, faster than 44.38% of C online submissions for Sum of Even Numbers After Queries.
Memory Usage: 22.9 MB, less than 98.46% of C online submissions for Sum of Even Numbers After Queries.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值