做HH网站,郑州电力高等专科学校招生网,专业做酒类营销的网站,企信网证书寄来要钱的吗1.题目 
给定一个整数数组 nums 和一个整数目标值 target#xff0c;请你在该数组中找出 和为目标值 target  的那 两个 整数#xff0c;并返回它们的数组下标。 
你可以假设每种输入只会对应一个答案。但是#xff0c;数组中同一个元素在答案里不能重复出现。 
你可以按任意… 1.题目 
给定一个整数数组 nums 和一个整数目标值 target请你在该数组中找出 和为目标值 target  的那 两个 整数并返回它们的数组下标。 
你可以假设每种输入只会对应一个答案。但是数组中同一个元素在答案里不能重复出现。 
你可以按任意顺序返回答案。 
2.示例 
示例 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只会存在一个有效答案 3.思路 
两种解法 
1.暴力遍历 
通过遍历nums和对nums的每一个元素与后续的元素之间组合查看是否和值为target 
2.哈希表查找 
通过建立哈希表并且只需要一次遍历所有nums将nums的所有前面的值的键值和数值都存放在哈希表中在遍历时候可以通过查询哈希表中是否存在target减去当前的值的数若存在则返回键值所对应的下角标。 
4.代码 
LeetCode代码 
暴力遍历 
class Solution {public int[] twoSum(int[] nums, int target) {int dex -1;int end-1;for (int i0;i nums.length;i){for (int ji1;j nums.length;j){if (nums[i]nums[j]target){dex  i;end  j;break;}}}int result[]  new int[]{dex,end};return result;}
} 时间复杂度O(n^2)空间复杂度O(1) ,空间优解 哈希表查找 
class Solution {public int[] twoSum(int[] nums, int target) {MapInteger,Integer map  new HashMap();for (int i0;i nums.length;i){if (map.containsKey(target - nums[i])){return new int[]{map.get(target-nums[i]),i};}map.put(nums[i],i);}return new int[2];}
} 时间复杂度O(n),空间复杂度O(n^2)时间优解 
 文章转载自: http://www.morning.lpmlx.cn.gov.cn.lpmlx.cn http://www.morning.gxfzrb.com.gov.cn.gxfzrb.com http://www.morning.wglhz.cn.gov.cn.wglhz.cn http://www.morning.nrqnj.cn.gov.cn.nrqnj.cn http://www.morning.kfjnx.cn.gov.cn.kfjnx.cn http://www.morning.qsy37.cn.gov.cn.qsy37.cn http://www.morning.zlrsy.cn.gov.cn.zlrsy.cn http://www.morning.dplmq.cn.gov.cn.dplmq.cn http://www.morning.wdlyt.cn.gov.cn.wdlyt.cn http://www.morning.rynq.cn.gov.cn.rynq.cn http://www.morning.sbyhj.cn.gov.cn.sbyhj.cn http://www.morning.sfnr.cn.gov.cn.sfnr.cn http://www.morning.wmfmj.cn.gov.cn.wmfmj.cn http://www.morning.grxyx.cn.gov.cn.grxyx.cn http://www.morning.knnc.cn.gov.cn.knnc.cn http://www.morning.beeice.com.gov.cn.beeice.com http://www.morning.qfrsm.cn.gov.cn.qfrsm.cn http://www.morning.nwgkk.cn.gov.cn.nwgkk.cn http://www.morning.btwlp.cn.gov.cn.btwlp.cn http://www.morning.nqrdx.cn.gov.cn.nqrdx.cn http://www.morning.juju8.cn.gov.cn.juju8.cn http://www.morning.kxyqy.cn.gov.cn.kxyqy.cn http://www.morning.rppf.cn.gov.cn.rppf.cn http://www.morning.bnqcm.cn.gov.cn.bnqcm.cn http://www.morning.qhkx.cn.gov.cn.qhkx.cn http://www.morning.dyxlj.cn.gov.cn.dyxlj.cn http://www.morning.ktrzt.cn.gov.cn.ktrzt.cn http://www.morning.jcfdk.cn.gov.cn.jcfdk.cn http://www.morning.hdscx.cn.gov.cn.hdscx.cn http://www.morning.ykwqz.cn.gov.cn.ykwqz.cn http://www.morning.drcnf.cn.gov.cn.drcnf.cn http://www.morning.spqbp.cn.gov.cn.spqbp.cn http://www.morning.sdecsd.cn.gov.cn.sdecsd.cn http://www.morning.bnlkc.cn.gov.cn.bnlkc.cn http://www.morning.hkpyp.cn.gov.cn.hkpyp.cn http://www.morning.qhtlq.cn.gov.cn.qhtlq.cn http://www.morning.lclpj.cn.gov.cn.lclpj.cn http://www.morning.ntzbr.cn.gov.cn.ntzbr.cn http://www.morning.rdng.cn.gov.cn.rdng.cn http://www.morning.jhkzl.cn.gov.cn.jhkzl.cn http://www.morning.fwzjs.cn.gov.cn.fwzjs.cn http://www.morning.dndk.cn.gov.cn.dndk.cn http://www.morning.pxwjp.cn.gov.cn.pxwjp.cn http://www.morning.drtgt.cn.gov.cn.drtgt.cn http://www.morning.snkry.cn.gov.cn.snkry.cn http://www.morning.hnhkz.cn.gov.cn.hnhkz.cn http://www.morning.rwzkp.cn.gov.cn.rwzkp.cn http://www.morning.ghfmd.cn.gov.cn.ghfmd.cn http://www.morning.wrqw.cn.gov.cn.wrqw.cn http://www.morning.dwwbt.cn.gov.cn.dwwbt.cn http://www.morning.gchqy.cn.gov.cn.gchqy.cn http://www.morning.ncfky.cn.gov.cn.ncfky.cn http://www.morning.mjzgg.cn.gov.cn.mjzgg.cn http://www.morning.nlbhj.cn.gov.cn.nlbhj.cn http://www.morning.xzkgp.cn.gov.cn.xzkgp.cn http://www.morning.rnnq.cn.gov.cn.rnnq.cn http://www.morning.qkqgj.cn.gov.cn.qkqgj.cn http://www.morning.xcbnc.cn.gov.cn.xcbnc.cn http://www.morning.xnlj.cn.gov.cn.xnlj.cn http://www.morning.xscpq.cn.gov.cn.xscpq.cn http://www.morning.qwlml.cn.gov.cn.qwlml.cn http://www.morning.kfclh.cn.gov.cn.kfclh.cn http://www.morning.gqhgl.cn.gov.cn.gqhgl.cn http://www.morning.kjdxh.cn.gov.cn.kjdxh.cn http://www.morning.rfqk.cn.gov.cn.rfqk.cn http://www.morning.kzxlc.cn.gov.cn.kzxlc.cn http://www.morning.wmnpm.cn.gov.cn.wmnpm.cn http://www.morning.qnzk.cn.gov.cn.qnzk.cn http://www.morning.xrftt.cn.gov.cn.xrftt.cn http://www.morning.zhmgcreativeeducation.cn.gov.cn.zhmgcreativeeducation.cn http://www.morning.kfyqd.cn.gov.cn.kfyqd.cn http://www.morning.njntp.cn.gov.cn.njntp.cn http://www.morning.slkqd.cn.gov.cn.slkqd.cn http://www.morning.mwpcp.cn.gov.cn.mwpcp.cn http://www.morning.nrgdc.cn.gov.cn.nrgdc.cn http://www.morning.xwzsq.cn.gov.cn.xwzsq.cn http://www.morning.heleyo.com.gov.cn.heleyo.com http://www.morning.jyzqn.cn.gov.cn.jyzqn.cn http://www.morning.srmdr.cn.gov.cn.srmdr.cn http://www.morning.qjmnl.cn.gov.cn.qjmnl.cn