题目:给你一个数组 nums 。数组「动态和」的计算公式为:runningSum[i] = sum(nums[0]…nums[i]) 。
请返回 nums 的动态和。
题解:如果不想破坏源数据,可以再定义一个新数组。计算一维数组长度C++可以借助sizeof() 、begin()、end()间接的计算其长度,使用容器的话可以使用nums.size(); Java中使用nums.length;
C++
- class Solution {
- public:
- vector<int> runningSum(vector<int>& nums) {
- for(int i=1;i
size();i++){ - nums[i]=nums[i-1]+nums[i];
- }
- return nums;
- }
- };
Java
- class Solution {
- public int[] runningSum(int[] nums) {
-
- for(int i=1;i
- nums[i]+=nums[i-1];
- }
- return nums;
-
- }
- }
383. 赎金信
给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。
如果可以,返回 true ;否则返回 false 。
magazine 中的每个字符只能在 ransomNote 中使用一次。

题解:统计每个字符出现的次数即可。需要把string类型转为char型
- class Solution {
- public boolean canConstruct(String ransomNote, String magazine) {
-
- //String[] arr1 = ransomNote.split(""); //定义分隔符,字符串数组
- //String[] arr2 = magazine.split("");
- char[] arr1 = ransomNote.toCharArray(); //一个字符即为一个元素,字符数组
- char[] arr2 = magazine.toCharArray();
- if(arr1.length>arr2.length){
- return false;
- }
- int[] alphabet=new int[26];
- for(int i=0;i
- alphabet[arr2[i]-'a']++; //''字符,“”字符串
-
- }
- for(int i=0;i
- if(--alphabet[arr1[i]-'a']<0){
- return false;
- }
- }
- return true;
-
- }
- }
使用for(:)语法格式
for(variable : collection) statement
- class Solution {
- public boolean canConstruct(String ransomNote, String magazine) {
-
- if(ransomNote.length() > magazine.length()) {
- return false;
- }
-
- int[] cnt = new int[26];
-
- for(char c : magazine.toCharArray()) {
- cnt[c - 'a'] ++;
- }
- for(char c : ransomNote.toCharArray()) {
- cnt[c - 'a'] --;
- if(cnt[c - 'a'] < 0) {
- return false;
- }
- }
- return true;
- }
- }
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中数字、字符串转换
- // 数字转字符串 method1
- int number = 5;
- String str = String.valueOf(number);
- System.out.println(str);
-
- // 数字转字符串 method2
- int number = 5;
- Integer itr = number; //int装箱为对象,再调用对象的toString方法
- String str = itr.toString(); //或者直接 String str = Integer.toString(number);
- System.out.println(str);
-
- // 数字转字符串 method3
- int number = 5;
- String str = number + "";
- System.out.println(str);
-
- // 字符串转数字
- String str = "123";
- int number = Integer.parseInt(str);
- System.out.println(number);
-
Java
- class Solution {
- public List
fizzBuzz(int n) { - List
answer=new ArrayList(n); - for(int i=1;i<=n;i++){
- if(i%3==0&&i%5==0){
- answer.add("FizzBuzz");
- }
- else if(i%3==0){
- answer.add("Fizz");
- }
- else if(i%5==0){
- answer.add("Buzz");
- }
- else{
- answer.add(i+"");
- }
- }
- return answer;
- }
- }
C++
- class Solution {
- public:
- vector
fizzBuzz(int n) { - vector
ans; - for(int i=1;i<=n;i++){
- string s="";
- if(i%3==0) s+="Fizz";
- if(i%5==0) s+="Buzz";
- if(s.size()==0)s=to_string(i);
- ans.push_back(s);
- }
- return ans;
- }
- };
876. 链表的中间结点
给定一个头结点为 head 的非空单链表,返回链表的中间结点。
如果有两个中间结点,则返回第二个中间结点。

题解:计算链表中结点个数,去中间结点位置。方法二:快慢结点,快结点一次走两个,慢结点一次走一个。
C++
- /**
- * Definition for singly-linked list.
- * struct ListNode {
- * int val;
- * ListNode *next;
- * ListNode() : val(0), next(nullptr) {}
- * ListNode(int x) : val(x), next(nullptr) {}
- * ListNode(int x, ListNode *next) : val(x), next(next) {}
- * };
- */
- class Solution {
- public:
- ListNode* middleNode(ListNode* head) {
- vector
v; - while(head!=nullptr){
- v.push_back(head);
- head=head->next;
- }
- return v[v.size()/2];
- }
- };
Java
- /**
- * 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 ans=head;
- int len=1;
- while(ans.next!=null){
- len++;
- ans=ans.next;
- }
- ans=head;
- System.out.println(len);
- for(int i=0;i
2;i++){ - ans=ans.next;
- }
-
- return ans;
- }
- }
C++ 利用快慢指针
- /**
- * Definition for singly-linked list.
- * struct ListNode {
- * int val;
- * ListNode *next;
- * ListNode() : val(0), next(nullptr) {}
- * ListNode(int x) : val(x), next(nullptr) {}
- * ListNode(int x, ListNode *next) : val(x), next(next) {}
- * };
- */
- class Solution {
- public:
- ListNode* middleNode(ListNode* head) {
- ListNode* fast=head;
- ListNode* slow=head;
- while(fast!=NULL&&fast->next!=NULL){
- slow=slow->next;
- fast=fast->next->next;
- }
- return slow;
- }
- };
1342. 将数字变成 0 的操作次数
给你一个非负整数 num ,请你返回将它变成 0 所需要的步数。 如果当前数字是偶数,你需要把它除以 2 ;否则,减去 1 。
题解:直接根据题目流程计算即可。根据位运算可以提高执行效率。
C++
- class Solution {
- public:
- int numberOfSteps(int num) {
- int temp=num;
- int step=0;
- while(temp!=0){
- if(temp%2==0) temp/=2;
- else temp--;
- step++;
- }
- return step;
- }
- };
C++利用位运算
- class Solution {
- public:
- int numberOfSteps(int num) {
- int step=0;
- while(num>0){
- step+=(num&1)+1;//temp是even,step+1;temp是uneven,step+2
- num>>=1;//÷2操作
- }
- return max(step-1,0);
- }
- };
-
相关阅读:
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