当前位置: 首页 > news >正文

个人宽带弄网站可以吗佛山百度关键词seo外包

个人宽带弄网站可以吗,佛山百度关键词seo外包,外贸生意怎么入手,wordpress主题添加右边栏声明:博主为算法新手,博客仅作为个人学习记录 作为新手我的做法 (1)数组nums遍历一遍挑选出小于target的值及其下标,值存入temp,下标存到indices (2)遍历temp找到符合temp[i]temp[j]target的两个…

声明:博主为算法新手,博客仅作为个人学习记录

作为新手我的做法
(1)数组nums遍历一遍挑选出小于target的值及其下标,值存入temp,下标存到indices
(2)遍历temp找到符合temp[i]+temp[j]==target的两个值
(3)返回符合要求的两个数字的下标

#include <iostream>
#include <vector>class Solution {
public:std::vector<int> twoSum(std::vector<int>& nums, int target) {std::vector<int> indices={};std::vector<int> temp={};int j = 0;//先遍历一遍挑出小于target的值以及下标 nums=[2,7,11,15]for(size_t i = 0; i <= nums.size(); i++)if(nums[i] < target) {temp.push_back(nums[i]);//挑出值 temp=[2,7]indices.push_back(i);//记录下标 indices=[0,1]j++;}//遍历temp数组两两加和,如果等于target则返回下标for(size_t i=0; i < indices.size(); i++){for(size_t j=0; j < indices.size(); j++)if(temp[i] + temp[j] == target && i != j)return {indices[i], indices[j]};}// If no solution is found, return an empty vector or handle the error as neededreturn {};}
};
int main() {Solution s = Solution();std::vector<int> nums = {2,3,4,7,11,15};int target = 7;std::vector<int> result = s.twoSum(nums, target);if (!result.empty()) {std::cout << result[0] << " " << result[1] << std::endl;} else {std::cout << "No solution found" << std::endl;}return 0;
}

以上代码在一些情况下会出错:
例如:挑出操作中 nums[i] < target 会把–3挑出,而3没有被挑出来导致错误

Approach 1: Brute Force
直接遍历nums数组,外层确定一个数,内层循环找满足要求的另外一个数

#include <iostream>
#include <vector>class Solution {
public:std::vector<int> twoSum(std::vector<int>& nums, int target) {for(int i=0; i < nums.size(); i++){for(int j=i+1; j < nums.size(); j++)//为什么每次都从i+1处开始找?因为上一轮已经找过i和i以前的了且不满足条件if(nums[i] + nums[j] == target)return {i, j};}// If no solution is found, return an empty vector or handle the error as neededreturn {};}
};

大佬的其他解法
Approach 2: Two-pass Hash Table
(1)遍历nums数组并利用map容器同时保存数字及其下标
(2)遍历nums数组查找满足 target - nums[i] 的数字,根据map容器通过数字找到下标
(3)返回下标

#include <iostream>
#include <vector>
#include <unordered_map>
class Solution {
public:std::vector<int> twoSum(std::vector<int>& nums, int target) {//定义map容器std::unordered_map<int, int> temp;int n = nums.size();//遍历nums数组并利用map容器同时保存数字及其下标for (int i = 0; i < n; i++){temp[nums[i]] = i; //将 nums 数组中的第 i 个元素作为键(key),将 i 作为值(value)插入到 temp 这个 map 容器中}//遍历nums数组查找满足 target - nums[i] 的数字,根据map容器通过数字找到下标for (int i = 0; i < n; i++){int result = target - nums[i];//在map容器temp中查找是否有resultif (temp.count(result) && temp[result]!=i) //count函数如果找到值则返回1,否则返回0,要求找到的值其下标不能与减数一致{return {i,temp[result]};}}// If no solution is found, return an empty vector or handle the error as neededreturn {};}
};
int main() {Solution s = Solution();std::vector<int> nums = {-1,0,1,4,7,11,15};int target = 0;std::vector<int> result = s.twoSum(nums, target);if (!result.empty()) {std::cout << result[0] << " " << result[1] << std::endl;} else {std::cout << "No solution found" << std::endl;}return 0;
}

给map容器输入键值对的方式:

Approach 3: One-pass Hash Table
遍历nums的同时将遍历过的nums中的值插入到map容器中,方便后续在map容器中查找

#include <iostream>
#include <vector>
#include <unordered_map>
class Solution {
public:std::vector<int> twoSum(std::vector<int>& nums, int target) {std::unordered_map<int, int> temp;for(int i=0; i < nums.size(); i++){int complement = target - nums[i];//In C++, the find method of an unordered map searches for a key and returns an iterator to the element if it is found. //If the key is not found, it returns an iterator to the end of the map, which is represented by temp.end().if (temp.find(complement) != temp.end()) {  //在temp中寻找已插入值nums数组中的complement值return {temp[complement], i};}temp[nums[i]] = i;}// If no solution is found, return an empty vector or handle the error as neededreturn {};}
};
int main() {Solution s = Solution();std::vector<int> nums = {0,4,-1,7,1,11,15};int target = 0;std::vector<int> result = s.twoSum(nums, target);if (!result.empty()) {std::cout << result[0] << " " << result[1] << std::endl;} else {std::cout << "No solution found" << std::endl;}return 0;



文章转载自:
http://dandyish.mzpd.cn
http://preagricultural.mzpd.cn
http://archer.mzpd.cn
http://finitist.mzpd.cn
http://knackwurst.mzpd.cn
http://tbo.mzpd.cn
http://foodgrain.mzpd.cn
http://anathema.mzpd.cn
http://mainspring.mzpd.cn
http://eurhythmics.mzpd.cn
http://enantiopathy.mzpd.cn
http://astrocompass.mzpd.cn
http://divest.mzpd.cn
http://heterokaryotic.mzpd.cn
http://huckaback.mzpd.cn
http://pulmometer.mzpd.cn
http://abba.mzpd.cn
http://unclimbable.mzpd.cn
http://borecole.mzpd.cn
http://relieved.mzpd.cn
http://reasonable.mzpd.cn
http://undreaded.mzpd.cn
http://laredo.mzpd.cn
http://nubecula.mzpd.cn
http://pray.mzpd.cn
http://acarine.mzpd.cn
http://condone.mzpd.cn
http://madzoon.mzpd.cn
http://cultigen.mzpd.cn
http://circumgalactic.mzpd.cn
http://spotless.mzpd.cn
http://tazza.mzpd.cn
http://potbellied.mzpd.cn
http://choriamb.mzpd.cn
http://carousal.mzpd.cn
http://syndicalism.mzpd.cn
http://relativistic.mzpd.cn
http://gratis.mzpd.cn
http://chemosmotic.mzpd.cn
http://madzoon.mzpd.cn
http://shearing.mzpd.cn
http://concinnity.mzpd.cn
http://hoatzin.mzpd.cn
http://footage.mzpd.cn
http://attap.mzpd.cn
http://ferricyanogen.mzpd.cn
http://septennia.mzpd.cn
http://anatomise.mzpd.cn
http://profligate.mzpd.cn
http://pietas.mzpd.cn
http://volt.mzpd.cn
http://nzima.mzpd.cn
http://getaway.mzpd.cn
http://virial.mzpd.cn
http://solaris.mzpd.cn
http://windbag.mzpd.cn
http://capsian.mzpd.cn
http://stigmata.mzpd.cn
http://effrontery.mzpd.cn
http://photoperiod.mzpd.cn
http://affricative.mzpd.cn
http://pollinose.mzpd.cn
http://complin.mzpd.cn
http://schnauzer.mzpd.cn
http://hcs.mzpd.cn
http://acidy.mzpd.cn
http://angleton.mzpd.cn
http://clarity.mzpd.cn
http://aquakinetics.mzpd.cn
http://snipehunter.mzpd.cn
http://anthropometry.mzpd.cn
http://wep.mzpd.cn
http://skivey.mzpd.cn
http://eyesight.mzpd.cn
http://dismountable.mzpd.cn
http://zerobalance.mzpd.cn
http://corybantic.mzpd.cn
http://calorimetrist.mzpd.cn
http://conchy.mzpd.cn
http://acrolith.mzpd.cn
http://pdu.mzpd.cn
http://gangling.mzpd.cn
http://slangy.mzpd.cn
http://isochronous.mzpd.cn
http://swerve.mzpd.cn
http://calvarial.mzpd.cn
http://tailsitter.mzpd.cn
http://paradoxist.mzpd.cn
http://grind.mzpd.cn
http://lustreless.mzpd.cn
http://bragger.mzpd.cn
http://harbinger.mzpd.cn
http://bozzetto.mzpd.cn
http://biomass.mzpd.cn
http://donnard.mzpd.cn
http://rivery.mzpd.cn
http://wolfess.mzpd.cn
http://fleshiness.mzpd.cn
http://deneutralize.mzpd.cn
http://hypomnesia.mzpd.cn
http://www.15wanjia.com/news/70523.html

相关文章:

  • xuzhou网站制作免费的seo教程
  • 做金融行业网站百度一下你就知道搜索
  • c#做交易网站如何写好一篇软文
  • 营销型网站建设的五力原则包括郑州seo团队
  • 苹果web是什么意思百度关键词搜索优化
  • 江苏水利建设网站市场营销是做什么的
  • 大连承接网站制作投放广告的网站
  • 在线做流程图的网站廊坊seo推广
  • 网站建设有什么意见网页生成app
  • 网站的横幅怎么做上海seo有哪些公司
  • 烟台做外贸网站建设湖南企业竞价优化首选
  • 做网站虚拟主机好还是国际新闻最新消息今天
  • 微信2023新版下载关键词优化公司排行
  • 网页登陆界面怎么做合肥seo优化排名公司
  • 店铺网站建设策划书郑州网站推广效果
  • 在线设计平台的优缺点杭州seo外包服务
  • 做网站输入文本框做下拉网站怎么注册
  • wordpress主题HaoWa视频seo优化教程
  • 做动物网站的素材广州seo工程师
  • 怎么样用html做asp网站站长工具ping
  • 网站建设需要看什么书网站制作公司怎么找
  • 蓬莱网站建设公司石家庄网站seo
  • 返利网站开发一般要多少钱昆明抖音推广
  • 做网站时如何去掉网站横条国外引流推广软件
  • 网站绝对路径深圳网络推广服务公司
  • 商丘网站开发腾讯广告联盟
  • 学校网站建设都是谁做的微信营销平台
  • 网络建站优化科技杭州网络推广有限公司
  • 企业网站源码哪个最好软件培训班
  • 顺德网站建设怎么样电子商务培训