• 力扣 1480. 一维数组的动态和 383. 赎金信412. Fizz Buzz


     1480. 一维数组的动态和 

    题目:给你一个数组 nums 。数组「动态和」的计算公式为:runningSum[i] = sum(nums[0]…nums[i]) 。

    请返回 nums 的动态和。

    题解:如果不想破坏源数据,可以再定义一个新数组。计算一维数组长度C++可以借助sizeof() 、begin()、end()间接的计算其长度,使用容器的话可以使用nums.size(); Java中使用nums.length;

    C++ 

    1. class Solution {
    2. public:
    3. vector<int> runningSum(vector<int>& nums) {
    4. for(int i=1;isize();i++){
    5. nums[i]=nums[i-1]+nums[i];
    6. }
    7. return nums;
    8. }
    9. };

    Java

    1. class Solution {
    2. public int[] runningSum(int[] nums) {
    3. for(int i=1;i
    4. nums[i]+=nums[i-1];
    5. }
    6. return nums;
    7. }
    8. }

    383. 赎金信

    给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。

    如果可以,返回 true ;否则返回 false 。

    magazine 中的每个字符只能在 ransomNote 中使用一次。

     题解:统计每个字符出现的次数即可。需要把string类型转为char型

    1. class Solution {
    2. public boolean canConstruct(String ransomNote, String magazine) {
    3. //String[] arr1 = ransomNote.split(""); //定义分隔符,字符串数组
    4. //String[] arr2 = magazine.split("");
    5. char[] arr1 = ransomNote.toCharArray(); //一个字符即为一个元素,字符数组
    6. char[] arr2 = magazine.toCharArray();
    7. if(arr1.length>arr2.length){
    8. return false;
    9. }
    10. int[] alphabet=new int[26];
    11. for(int i=0;i
    12. alphabet[arr2[i]-'a']++; //''字符,“”字符串
    13. }
    14. for(int i=0;i
    15. if(--alphabet[arr1[i]-'a']<0){
    16. return false;
    17. }
    18. }
    19. return true;
    20. }
    21. }

    使用for(:)语法格式

    for(variable : collection) statement

    1. class Solution {
    2. public boolean canConstruct(String ransomNote, String magazine) {
    3. if(ransomNote.length() > magazine.length()) {
    4. return false;
    5. }
    6. int[] cnt = new int[26];
    7. for(char c : magazine.toCharArray()) {
    8. cnt[c - 'a'] ++;
    9. }
    10. for(char c : ransomNote.toCharArray()) {
    11. cnt[c - 'a'] --;
    12. if(cnt[c - 'a'] < 0) {
    13. return false;
    14. }
    15. }
    16. return true;
    17. }
    18. }

    412. Fizz Buzz

    给你一个整数 n ,找出从 1 到 n 各个整数的 Fizz Buzz 表示,并用字符串数组 answer(下标从 1 开始)返回结果,其中:

    answer[i] == "FizzBuzz" 如果 i 同时是 3 和 5 的倍数。
    answer[i] == "Fizz" 如果 i 是 3 的倍数。
    answer[i] == "Buzz" 如果 i 是 5 的倍数。
    answer[i] == i (以字符串形式)如果上述条件全不满足。

    题解:C++ 中用to_string()将数字转为字符串

    Java中数字、字符串转换

    1. // 数字转字符串 method1
    2. int number = 5;
    3. String str = String.valueOf(number);
    4. System.out.println(str);
    5. // 数字转字符串 method2
    6. int number = 5;
    7. Integer itr = number; //int装箱为对象,再调用对象的toString方法
    8. String str = itr.toString(); //或者直接 String str = Integer.toString(number);
    9. System.out.println(str);
    10. // 数字转字符串 method3
    11. int number = 5;
    12. String str = number + "";
    13. System.out.println(str);
    14. // 字符串转数字
    15. String str = "123";
    16. int number = Integer.parseInt(str);
    17. System.out.println(number);

    Java

    1. class Solution {
    2. public List fizzBuzz(int n) {
    3. List answer=new ArrayList(n);
    4. for(int i=1;i<=n;i++){
    5. if(i%3==0&&i%5==0){
    6. answer.add("FizzBuzz");
    7. }
    8. else if(i%3==0){
    9. answer.add("Fizz");
    10. }
    11. else if(i%5==0){
    12. answer.add("Buzz");
    13. }
    14. else{
    15. answer.add(i+"");
    16. }
    17. }
    18. return answer;
    19. }
    20. }

    C++

    1. class Solution {
    2. public:
    3. vector fizzBuzz(int n) {
    4. vector ans;
    5. for(int i=1;i<=n;i++){
    6. string s="";
    7. if(i%3==0) s+="Fizz";
    8. if(i%5==0) s+="Buzz";
    9. if(s.size()==0)s=to_string(i);
    10. ans.push_back(s);
    11. }
    12. return ans;
    13. }
    14. };

    876. 链表的中间结点

    给定一个头结点为 head 的非空单链表,返回链表的中间结点。

    如果有两个中间结点,则返回第二个中间结点。

    题解:计算链表中结点个数,去中间结点位置。方法二:快慢结点,快结点一次走两个,慢结点一次走一个。

    C++

    1. /**
    2. * Definition for singly-linked list.
    3. * struct ListNode {
    4. * int val;
    5. * ListNode *next;
    6. * ListNode() : val(0), next(nullptr) {}
    7. * ListNode(int x) : val(x), next(nullptr) {}
    8. * ListNode(int x, ListNode *next) : val(x), next(next) {}
    9. * };
    10. */
    11. class Solution {
    12. public:
    13. ListNode* middleNode(ListNode* head) {
    14. vector v;
    15. while(head!=nullptr){
    16. v.push_back(head);
    17. head=head->next;
    18. }
    19. return v[v.size()/2];
    20. }
    21. };

     Java

    1. /**
    2. * Definition for singly-linked list.
    3. * public class ListNode {
    4. * int val;
    5. * ListNode next;
    6. * ListNode() {}
    7. * ListNode(int val) { this.val = val; }
    8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    9. * }
    10. */
    11. class Solution {
    12. public ListNode middleNode(ListNode head) {
    13. ListNode ans=head;
    14. int len=1;
    15. while(ans.next!=null){
    16. len++;
    17. ans=ans.next;
    18. }
    19. ans=head;
    20. System.out.println(len);
    21. for(int i=0;i2;i++){
    22. ans=ans.next;
    23. }
    24. return ans;
    25. }
    26. }

    C++ 利用快慢指针

    1. /**
    2. * Definition for singly-linked list.
    3. * struct ListNode {
    4. * int val;
    5. * ListNode *next;
    6. * ListNode() : val(0), next(nullptr) {}
    7. * ListNode(int x) : val(x), next(nullptr) {}
    8. * ListNode(int x, ListNode *next) : val(x), next(next) {}
    9. * };
    10. */
    11. class Solution {
    12. public:
    13. ListNode* middleNode(ListNode* head) {
    14. ListNode* fast=head;
    15. ListNode* slow=head;
    16. while(fast!=NULL&&fast->next!=NULL){
    17. slow=slow->next;
    18. fast=fast->next->next;
    19. }
    20. return slow;
    21. }
    22. };

    1342. 将数字变成 0 的操作次数

    给你一个非负整数 num ,请你返回将它变成 0 所需要的步数。 如果当前数字是偶数,你需要把它除以 2 ;否则,减去 1 。

    题解:直接根据题目流程计算即可。根据位运算可以提高执行效率。

    C++

    1. class Solution {
    2. public:
    3. int numberOfSteps(int num) {
    4. int temp=num;
    5. int step=0;
    6. while(temp!=0){
    7. if(temp%2==0) temp/=2;
    8. else temp--;
    9. step++;
    10. }
    11. return step;
    12. }
    13. };

    C++利用位运算

    1. class Solution {
    2. public:
    3. int numberOfSteps(int num) {
    4. int step=0;
    5. while(num>0){
    6. step+=(num&1)+1;//temp是even,step+1;temp是uneven,step+2
    7. num>>=1;//÷2操作
    8. }
    9. return max(step-1,0);
    10. }
    11. };

  • 相关阅读:
    Codeforces Round #816 (Div. 2) 题解 A,B,C
    (6)Flink CEP SQL模拟账号短时间内异地登录风控预警
    JavaScript的事件监听
    天天爱跑步【NOIP2016 T4】
    说说你对slot的理解?
    unity自定义着色器基础
    CentOS 6服务器升级Git时, 下载依赖包失败,需配置国外Yum源
    Node核心模块之Stream
    Java面向对象(基础)--方法应用
    YOLO目标检测——红白细胞血小板数据集【含对应voc、coco和yolo三种格式标签】
  • 原文地址:https://blog.csdn.net/m0_55955474/article/details/125990476