Pesquisar este blog

Livros Recomendados

Mostrando postagens com marcador Leetcode. Mostrar todas as postagens
Mostrando postagens com marcador Leetcode. Mostrar todas as postagens

quinta-feira, 12 de novembro de 2020

Leetcode 1470 - Shuffle the Array - c++

Linguagem: C++


Solução:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
public:
    vector<int> shuffle(vector<int>& nums, int n) {
        vector<int> result;
        for(int i=0; i<n;i++){
            result.push_back(nums.at(i));
            result.push_back(nums.at(i+n));
        }
        return result;
    }
};

quarta-feira, 11 de novembro de 2020

Leetcode 1431 - Kids With the Greatest Number of Candies - C++

Linguagem: C++


Solução:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
    vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {
        int max = 0;
        vector<bool> result;
        for(int i = 0; i < candies.size(); i++){
            if(candies.at(i)>max){
                max = candies.at(i);
            }
        }
        for(int i = 0; i < candies.size(); i++){
            if(candies.at(i) + extraCandies >= max){
                result.push_back(true);
            }
            else{
                result.push_back(false);
            }
        }
        return result;
    }
};

terça-feira, 10 de novembro de 2020

Leetcode 1480 - Running Sum of 1d Array - C++

Linguagem: C++


Solução:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
public:
    vector<int> runningSum(vector<int>& nums) {
        vector<int> result;
        int sum = 0;
        for(int i=0;i<nums.size();i++){
            sum += nums.at(i);
            result.push_back(sum);
        }
        return result;
    }
};

segunda-feira, 9 de novembro de 2020

Leetcode 605 - Can Place Flowers - C++

 Linguagem: C++


Solução:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public:
    bool canPlaceFlowers(vector<int>& flowerbed, int n) {
        int planted = 0;
        if(flowerbed.size() == 1 and flowerbed.at(0)==0){
            return true;
        }
        if(flowerbed.size() > 1 and flowerbed.at(0) == 0 and flowerbed.at(1) == 0){
            planted++;
            flowerbed[0] = 1;
        }
        for(int i =1;i<flowerbed.size()-1;i++){
            if(flowerbed.at(i-1) == 0 and flowerbed.at(i) == 0 and flowerbed.at(i+1) == 0){
                planted++;
                flowerbed[i] = 1;
            }
        }
        if(flowerbed.size() > 1 and flowerbed.at(flowerbed.size()-2) == 0 and flowerbed.at(flowerbed.size()-1) == 0){
            planted++;
        }
        if(planted >= n){
            return true;
        }
        else{
            return false;
        }
    }
};

Postagem em destaque

URI (BEECROWD) - 2158 - Helping Uncle Cláudio (Ajudando o Tio Cláudio) - Matemática - C, C++ e Haskell

Buenas! Estou aqui mais uma vez para resolver um problema de Matemática! Agora tenho resolvido alguns dessa categoria, pra que vocês possam ...

Postagens mais visitadas