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

开发高端网站开发长沙网站推广

开发高端网站开发,长沙网站推广,国内手机网站建设,网站开发基本流程ppt声明:博主为算法新手,博客仅作为个人学习记录 作为新手我的做法 (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://www.hkea.cn/news/952009/

相关文章:

  • 重庆网站设计人员外贸网站搭建推广
  • 局域网内的网站建设西安网站建设公司排名
  • 普通网站报价多少中南建设集团有限公司
  • 蚌埠做网站哪家好全网营销国际系统
  • 沈阳市网站制作谷歌香港google搜索引擎入口
  • 做美食网站的背景高端网站建设制作
  • 文件什么上传到wordpress泉州seo技术
  • 网站地址地图怎么做网页制作的软件有哪些
  • 如何用万网建设网站口碑营销策划方案
  • 做网站的基础架构东莞seo建站公司
  • 嘉兴做网站的哪家好龙岗网站制作
  • 论坛做网站好吗百度官方网页
  • 微信开发者工具获取系统日期seo优化一般包括
  • 怎么用文本做网站百度排行榜风云榜
  • 未来网站开发需求多搜索网站有哪几个
  • 网站建设 成都郑州高端网站制作
  • 快站怎么做淘客网站深圳关键词
  • 做网站时如何去掉网站横条小红书软文案例
  • 图虫南宁百度快速排名优化
  • 上城网站建设app推广文案
  • 网站建设特点宁波seo搜索引擎优化公司
  • 地产商网站建设网球新闻最新消息
  • 做爰全过程网站免费的视频谷歌seo搜索引擎
  • 怎么架设网站seo推广培训
  • 自己网站做问卷调查网页设计学生作业模板
  • 清远企业网站排名深圳网站建设系统
  • 互助平台网站建设费用卡点视频免费制作软件
  • 上海做b2b国际网站公司排名优化公司电话
  • 裙晖wordpress重庆seo整站优化
  • 乌克兰网站后缀谷歌浏览器下载电脑版