本文记录一下刷的算法题

1.一维数组的动态和
难度:入门
leetcode-1480-1.jpg

代码实现:

//C#
public class Solution {
    public int[] RunningSum(int[] nums) {
        for (int i = 1; i < nums.Length; i++)
        {
            nums[i] = nums[i] + nums[i - 1];
        }
            
        return nums;
    }
}
//C++
class Solution {
public:
    vector<int> runningSum(vector<int>& nums) {
        for (int i = 1; i < nums.size(); i++)
        {
            nums.at(i) = nums.at(i) + nums.at(i - 1);
        }
        return nums;
    }
};

2.将数字变成 0 的操作次数
难度:入门
leetcode-1342-1.jpg

代码实现:

//C#
public class Solution {
    public int NumberOfSteps(int num) {
        int step = 0;
        while (num != 0)
        {
            if (num % 2 == 0)
            {
                num /= 2;
            }
            else
            {
                num --;
            }
            step++;
        }
        return step;
    }
}
//C++
class Solution {
public:
    int numberOfSteps(int num) {
        int count = 0;
        while (num != 0)
        {
            if (num % 2 == 0)
                num /= 2;
            else
                num --;
            count ++;
        }
        return count;
    }
};

3.最富有客户的资产总量
难度:入门
leetcode-1672-1.jpg
代码实现:

//C#
public class Solution {
    public int MaximumWealth(int[][] accounts) {
        int max = 0;
        for (int i = 0; i < accounts.Length; i++)
        {
            int money = 0;
            for (int j = 0; j < accounts[i].Length; j++)
            {
                money += accounts[i][j];
            }
            if (money > max)
                max = money;
        }
        return max;
    }
}
//C++
class Solution {
public:
    int maximumWealth(vector<vector<int>>& accounts) {
        int max = 0;
        for (int i = 0; i < accounts.size(); i ++)
        {
            int money = 0;
            for (int j = 0; j < accounts[i].size(); j ++)
            {
                money += accounts[i][j];
            }
            if (money > max)
                max = money;
        }
        return max;
    }
};

4.Fizz Buzz
难度:入门
leetcode-412-1.jpg
代码实现:

//C#
public class Solution {
    public IList<string> FizzBuzz(int n) {
        string[] result = new string[n];
        for (int i = 1; i <= n; i++)
        {
            bool multipleOf3 = i % 3 == 0;
            bool multipleOf5 = i % 5 == 0;
            if (multipleOf3 && multipleOf5)
            {
                result[i - 1] = "FizzBuzz";
            }
            else if (multipleOf3)
            {
                result[i - 1] = "Fizz";
            }
            else if (multipleOf5)
            {
                result[i - 1] = "Buzz";
            }
            else
            {
                result[i - 1] = i.ToString();
            }
        }
        return result;
    }
}
//C++
class Solution {
public:
    vector<string> fizzBuzz(int n) {
        vector<string> result(n);
        for (int i = 0; i < n; i ++)
        {
            if ((i + 1) % 3 == 0 && (i + 1) % 5 == 0)
                result[i] = "FizzBuzz";
            else if ((i + 1) % 3 == 0)
                result[i] = "Fizz";
            else if ((i + 1) % 5 == 0)
                result[i] = "Buzz";
            else
                result[i] = to_string(i + 1);
        }
        return result;
    }
};