两数之和 Hot100
目录
内容概况:
题目和示例
解题思路
总结
(1)C++变量与数据类型
(2)输入输出流与std命名空间
什么是命名空间
C++中定义namespace
使用场景:
C++中的std命名空间
(3)范围for循环(C++11)
(4)函数定义(重点在参数)
(5)动态数组 vector
(6)对象的定义方式
内容概况:
(1)C++常用变量类型:int、float、double、bool、char、string(#include<string>)、vector<int> (#include<vector>)
(2)命名空间、std命名空间,建议的命名空间引用方式、C++的标准库去掉了.h。
(3)范围for循环:for(int i : nums)
(4)函数定义(引用如何传参)
(5)动态数组vector的初始化、常用函数
(6)对象的定义、创建方式
题目和示例
给定一个整数数组 nums
和一个整数目标值 target
,请你在该数组中找出 和为目标值 target
的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案,并且你不能使用两次相同的元素。
你可以按任意顺序返回答案。
示例 1:
输入:nums = [2,7,11,15], target = 9 输出:[0,1] 解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
示例 2:
输入:nums = [3,2,4], target = 6 输出:[1,2]
示例 3:
输入:nums = [3,3], target = 6 输出:[0,1]
提示:
2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
- 只会存在一个有效答案
解题思路
这道题思路很简单,就不讲解思路了,主要是复习一下语法知识以及函数调用,类的定义和对象的创建。
class Solution {
public:
vector<int> twoSum(vector<int> &nums, int target) {
vector<int> towNum = {};
for (int i = 0; i < nums.size() - 1; i++) {
for (int j = i + 1; j < nums.size(); j++) {
if (nums[i] + nums[j] == target) {
towNum.push_back(i);
towNum.push_back(j);
return towNum;
}
}
}
return towNum;
}
};
总结
(1)C++变量与数据类型
int a = 10; // 整型
float b = 1.0 // 单精度浮点
double pi = 3.14159; // 双精度浮点
bool flag = true; // 布尔值
char c = 'A'; // 字符
string s = "Hello LeetCode";// 字符串(需#include <string>)
vector<int> nums{1,2,3}; // 动态数组(需#include <vector>)
(2)输入输出流与std命名空间
#include <iostream> // 引入输入输出流头文件
using namespace std; // 使用std这个命名空间
int main() {
int x;
cin >> x; // 输入(LeetCode中不需要)
cout << "x=" << x << endl; // 输出调试用
return 0;
}
什么是命名空间
命名空间是用来避免命名冲突的机制。它可以将代码中的标识符(如变量、函数、类等)分组,防止不同库或模块中的同名标识符发生冲突。
C++中定义namespace
namespace foo{
int num=0;
}
namespace A {
int x = 10;
void print() {
cout << "A::x = " << x << endl;
}
}
namespace B {
int x = 20;
void print() {
cout << "B::x = " << x << endl;
}
}
int main() {
A::print(); // 输出:A::x = 10
B::print(); // 输出:B::x = 20
return 0;
}
-
通过
A::x
和B::x
可以区分不同命名空间中的同名变量x
。
使用场景:
-
当多个库或模块定义了相同的函数或变量时,命名空间可以避免冲突。
-
在大型项目中,命名空间可以帮助组织代码。
C++中的std命名空间
std
是 C++ 标准库的命名空间。C++ 标准库中的所有内容(如cout
、vector
、string
等)都定义在std
命名空间中。
(标准库是什么?就是官方给出的标准,这些库是大家公认的,非标准库可以简单看成自定义库)
如下,如果没有写
using namespace std;那么就必须声明单独引用,或者在代码中加上命名空间
尽量避免全局使用std命名空间,有时候如果你有自定义命名空间可能导致冲突。
推荐
局部使用
或者全局
单独对某一个标识符使用(如using std::cout)
#include <iostream>
#include <vector>
//using std::cout;
//using std::endl;
using std::vector;
int main() {
vector<int> nums = {2, 7, 11, 15};
std::string name = "LeetCode";
std::cout << "Hello, " << name << std::endl;
return 0;
}
(3)范围for循环(C++11)
// 范围for循环
for (int num : nums) { /*...*/ }
vector<int> nums = {2, 7, 11, 15};
for (int i : nums) {
std::cout << i << " ";
}
(4)函数定义(重点在参数)
// 传引用避免拷贝,const防止修改
int sum(const vector<int>& arr) {
int res = 0;
for (int num : arr) res += num;
return res;
}
这里传入参数时,比如 vector<int> nums = {2, 7, 11, 15}; 传入:sum(nums)。
(5)动态数组 vector
vector<int> v = {7,5,3};
v.push_back(9); // 末尾添加 O(1)
v.pop_back(); // 删除末尾 O(1)
v[0] = 6; // 随机访问 O(1)
sort(v.begin(), v.end()); // 排序 O(nlogn)
int length = v.size(); // 长度
初始化方法:
(6)对象的定义方式
声明完类再定义对象
class Student stu1, stu2; //这种是从C语言中继承下来的
Student stu3, stu4; //C++独有的方式而且使用也更广泛
声明类的同时定义对象
class CDate
{
int year;
int month;
int day;
public:
void SetDate(int, int, int);
void ShowDate();
}myBirthday; //同时创建对象myBirthday