C++ For Hot100
1. 两数之和 - 力扣(LeetCode)
class Solution {
public:vector<int> twoSum(vector<int>& nums, int target) {vector<int> v;for(int i = 0;i<nums.size();i++){for(int j = i+1;j<nums.size();j++){if(target-nums[i]==nums[j]) {v.push_back(i);v.push_back(j);return v;}}}return v;}
};
1512. 好数对的数目 - 力扣(LeetCode)
class Solution {
public:int numIdenticalPairs(vector<int>& nums) {int j, ans = 0;int hash[101];memset(hash,0,sizeof(hash));for(j=0;j<nums.size();++j){ans += hash[nums[j]];++hash[nums[j]];}return ans;}
};
注意:这里的ans是累加的形式,也就是当前时刻的nums[j]会去和之前的配对找到相应的组数,并且加上之前的组数,因此才是ans = ans + hash[nums[j]];
709. 转换成小写字母 - 力扣(LeetCode)
tolower()
这里需要注意的是用引用,因为需要修改字符串s里面元素的值;
class Solution {
public:string toLowerCase(string s) {for(char& ch:s) ch = tolower(ch);return s;}};
258. 各位相加 - 力扣(LeetCode)
这道题指的注意的就是:int temp = num%10; sum+=num;num/=10;就可以得到num的各个位相加的数;
class Solution {
public:int addDigits(int num) {int sum;while(num>=10){sum=0;while(num){int temp=num%10;sum+=temp;num/=10;}num = sum;}return num;}
};
1281. 整数的各位积和之差 - 力扣(LeetCode)
class Solution {
public:int subtractProductAndSum(int n) {int sum = 0;int pro = 1;int ans;int arr[] ={0};while(n>0){int temp = n%10;sum += temp;pro *= temp;n /= 10;}ans = pro - sum;return ans;}
};
231. 2 的幂 - 力扣(LeetCode)
这道题有了二进制的按位与,因为如果是2的幂,减一之后两个数的按位与就是0;
class Solution {
public:bool isPowerOfTwo(int n) {return n>0 && (n&(n-1)) == 0;}
};
867. 转置矩阵 - 力扣(LeetCode)
矩阵转置,最好创建新的vector<int,vector<int>> result(col,vector<int> (row) 二维容器就是
vector<int,vector<int>> result(col//行数,vector<int> (row) //列数);
class Solution {
public:vector<vector<int>> transpose(vector<vector<int>>& matrix) {int row = matrix.size();int col = matrix[0].size();vector<vector<int>> result(col, vector<int>(row));for(int i=0;i<row;i++){for(int j = 0;j<col;j++){result[j][i] = matrix[i][j]; // 转置操作}}return result;}
};
也就是说如果是创建二维的容器,vector<vector<int>> result(col, vector<int>(row)); 这里面的result的两个参数分别是
704. 二分查找 - 力扣(LeetCode)
class Solution {
public:int search(vector<int>& nums, int target) {int mid;int left = 0;int right = nums.size()-1;while(left <= right){mid = (left + right) / 2;if(target<nums[mid]){right = mid-1;}else if(target>nums[mid]){left = mid+1;}else{return mid;}}return -1;}
};
27. 移除元素 - 力扣(LeetCode)
class Solution {
public:int removeElement(vector<int>& nums, int val) {int slowindex = 0;if(nums.size()==0) return 0;for(int i=0;i<nums.size();i++){if(nums[i]!=val){nums[slowindex] = nums[i];slowindex++;}}return slowindex;}
};
977. 有序数组的平方 - 力扣(LeetCode)
class Solution {
public:vector<int> sortedSquares(vector<int>& nums) {for(int i =0;i<nums.size();i++){nums[i] = nums[i]*nums[i];}std::sort(nums.begin(), nums.end());return nums;}
};
第一种方法就是利用了迭代器,sort(nums.begin(),nums.end());
class Solution {
public:vector<int> sortedSquares(vector<int>& nums) {vector<int> ans(nums.size());;int k=nums.size()-1;int left = 0;int right = nums.size()-1;while(left <= right){if(nums[left]*nums[left]<=nums[right]*nums[right]) {ans[k] = (nums[right] * nums[right]);right-=1;}else {ans[k] = nums[left] * nums[left];left+=1;}k--;}return ans;}
};
双指针法,就是right和left两个指针从begin和end开始找