力扣刷题:第一天

力扣刷题:第一天,有我的题解

1480.一维数组的动态和

在这里插入图片描述

我的:循环相加

class Solution {
    public int[] runningSum(int[] nums) {
        int a = 0;
        int[] b = new int[nums.length];
        for(int i = 0;i<nums.length;i++){
            a+=nums[i];
            b[i]=a;
        }
        return b;
    }
}

在这里插入图片描述

题解:

在这里插入图片描述

class Solution {
    public int[] runningSum(int[] nums) {
        int n = nums.length;
        for (int i = 1; i < n; i++) {
            nums[i] += nums[i - 1];
        }
        return nums;
    }
}

在这里插入图片描述

383.赎金信

在这里插入图片描述

我的:跑的通但很差

思路:每次遍历String中的每个字符,一样的话让y++,然后删掉split1数组里面的一个元素,然后再循环,如果ransomNote的长度和y相等就是true否则为false

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        String[] split = ransomNote.split("");
        String[] split1 = magazine.split("");
        int x = ransomNote.length();
        int y = 0;
        for (String s : split) {
            for (int i = 0; i < split1.length; i++) {
                if (s.equals(split1[i])){
                    y++;
                    // 数组转成List集合
                    List<String> collect = Arrays.stream(split1).collect(Collectors.toList());
                    // 使用list里面的API,remove删掉指定值
                    collect.remove(split1[i]);
                    // List转成数组,且赋值给split1
                    split1 = collect.toArray(new String[collect.size()]);
                    break;
                }
            }
        }
        return x == y;
    }
}

在这里插入图片描述

题解:使用到了ASCII码值

在这里插入图片描述

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        //如果ransomNote的长度大于magazine的话,那么肯定为false
        if (ransomNote.length() > magazine.length()) {
            return false;
        }
        //小写字母有26个
        int[] cnt = new int[26];
        //提取magazine的字符char
        for (char c : magazine.toCharArray()) {
            //使用char类型的'c'-'a',根据ASCII码值,得到0-26的值,0是指'a'-'a',26指'z'-'a'
            // 用到了一个就再原有值自加1,因为int类型的初始值为0所以从0开始加
            cnt[c - 'a']++;
        }
        for (char c : ransomNote.toCharArray()) {
            // 遍历ransomNote,得到的char类型的字符减'a',把之前存的内容减1
            cnt[c - 'a']--;
            //小于0的话,就说明magazine比ransomNote少一个字母,所以返回false
            if(cnt[c - 'a'] < 0) {
                return false;
            }
        }
        return true;
    }
}

在这里插入图片描述

412.Fizz Buzz

在这里插入图片描述

我的:就通过%运算循环和判断(我这个没有可取之处)

class Solution {
    public List<String> fizzBuzz(int n) {
        String[] a = new String[n];
        for (int i = 0; i < a.length; i++) {
            if (i!=0){
                if ((i+1)%3==0){
                    if ((i+1)%5==0){
                        a[i] = "FizzBuzz";
                        continue;
                    }
                    a[i] = "Fizz";
                    continue;
                }else if ((i+1)%5==0){
                    a[i] = "Buzz";
                    continue;
                }
            }
            a[i] = String.valueOf(i+1);
        }
        return Arrays.asList(a);
    }
}

在这里插入图片描述

我的答案2:

class Solution {
    public List<String> fizzBuzz(int n) {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 1; i <= n; i++) {
            if (i%3 == 0 && i%5 ==0 ) list.add("FizzBuzz");
            else if (i%3 == 0) list.add("Fizz");
            else if (i%5 == 0) list.add("Buzz");
            else list.add(String.valueOf(i));
        }
        return list;
    }
}

在这里插入图片描述

题解:他这个思路就很简单,通过StringBuffer字符串拼接Fizz和Buzz,每次循环的时候,判断一遍,没有3或5的倍数时,把数字i放进去;

在这里插入图片描述

class Solution {
    public List<String> fizzBuzz(int n) {
        List<String> answer = new ArrayList<String>();
        for (int i = 1; i <= n; i++) {
            StringBuffer sb = new StringBuffer();
            if (i % 3 == 0) {
                sb.append("Fizz");
            }
            if (i % 5 == 0) {
                sb.append("Buzz");
            }
            if (sb.length() == 0) {
                sb.append(i);
            }
            answer.add(sb.toString());
        }
        return answer;
    }
}

在这里插入图片描述

876.链表的中间节点

在这里插入图片描述

我的:还行

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode a = head;
        List<Integer> list = new ArrayList<>();
        while (head!=null){
            list.add(head.val);
            head=head.next;
        }
        for (int i = 0; i < list.size()/2; i++) {
            a = a.next;
        }
        return a;
    }
}

在这里插入图片描述

题解:

在这里插入图片描述

class Solution {
    public ListNode middleNode(ListNode head) {
        // 题目说的 `给定链表的结点数介于 1 和 100 之间。` 所以定义一个在1和100之间的链表
        ListNode[] A = new ListNode[100];
        int t = 0;
        //判断head为不为空,单向链表的特点就是最后一个指针指向的空(null)
        while (head != null) {
            // t++ 先执行后自增 所以从A[0] 开始塞入链表的值
            A[t++] = head;
            //更新链表,单向链表的特点就是最后一个指针指向的空(null)
            head = head.next;
        }
        //int类型的数的特点就是,相除只取整数,如3/2=1
        return A[t / 2];
    }
}

在这里插入图片描述

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

在这里插入图片描述

我的:就简单的if判断,然后一直循环

class Solution {
    public int numberOfSteps(int num) {
         if (num==0) {
             return 0;
         }
         int nums = 0;
         while (num>0){
             if (num%2==0){
                 num=num/2;
                 nums++;
             }else {
                 num=(num-1)/2;
                 if (num!=0){
                     nums+=2;
                 }else nums+=1;
             }
         }
         return nums;
    }
}

在这里插入图片描述

题解:他这个用了位运算,不是很懂

class Solution {
    public int numberOfSteps(int num) {
        int ret = 0;
        while (num > 0) {
            ret += (num > 1 ? 1 : 0) + (num & 0x01);
            num >>= 1;
        }
        return ret;
    }
}

在这里插入图片描述

1672.最富有客户的资产总量

在这里插入图片描述

我的:

class Solution {
    public int maximumWealth(int[][] accounts) {
         int num = 0;
         for (int[] account : accounts) {
             int a = 0;
             for (int i : account) {
                 a+=i;
             }
             if (a>num) num = a;
         }
         return num;
    }
}

在这里插入图片描述

题解:不是很推荐

class Solution {
    public int maximumWealth(int[][] accounts) {
        int maxWealth = Integer.MIN_VALUE;
        for (int[] account : accounts) {
            maxWealth = Math.max(maxWealth, Arrays.stream(account).sum());
        }
        return maxWealth;
    }
}

在这里插入图片描述

不推荐的原因:他跑了两秒

class Solution {
public int maximumWealth(int[][] accounts) {
int maxWealth = Integer.MIN_VALUE;
for (int[] account : accounts) {
maxWealth = Math.max(maxWealth, Arrays.stream(account).sum());
}
return maxWealth;
}
}


[外链图片转存中...(img-UupqpFYw-1665323058908)]

### 不推荐的原因:他跑了两秒

![\[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-urBJGmPC-1665323058909)(C:\Users\18358\AppData\Roaming\Typora\typora-user-images\image-20221009214254724.png)\]](https://img-blog.csdnimg.cn/cb1e8d72cfb947f6b42640bbca704080.png)


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