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

市场调研公司成功案例企业优化推广

市场调研公司成功案例,企业优化推广,仿爱客wordpress,全球疫情最新消息数据题目一:两数之和 给出一个整型数组 numbers 和一个目标值 target,请在数组中找出两个加起来等于目标值的数的下标,返回的下标按升序排列。 (注:返回的数组下标从1开始算起,保证target一定可以由数组里面2…

题目一:两数之和

给出一个整型数组 numbers 和一个目标值 target,请在数组中找出两个加起来等于目标值的数的下标,返回的下标按升序排列。

(注:返回的数组下标从1开始算起,保证target一定可以由数组里面2个数字相加得到)

方法一,双层for遍历:

不过,这种方法在牛客网上执行的时候报超时错误

import java.util.*;
public class Solution {/*** * @param numbers int整型一维数组 * @param target int整型 * @return int整型一维数组*/public int[] twoSum (int[] numbers, int target) {// write code hereint n = numbers.length;int[] res = {-1, -1};//遍历数组for (int i = 0; i < n; ++i) {for (int j = i + 1; j < n; ++j) {//判断相加值是否为targetif (numbers[i] + numbers[j] == target) {res[0] = i+1;res[1] = j+1;//返回值return res;}}}return res;}
}

复杂度分析:

  • 时间复杂度:O(n^2) 遍历两次数组
  • 空间复杂度:O(1) 未申请额外空间

 方法二,hash表

import java.util.*;public class Solution {/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可*** @param numbers int整型一维数组* @param target int整型* @return int整型一维数组*/public int[] twoSum (int[] numbers, int target) {HashMap<Integer, Integer> map = new HashMap<>();//遍历数组for (int i = 0; i < numbers.length; i++) {if (map.containsKey(target - numbers[i])) {// 这里题目要求下标在数组中升序排列,这里这样放直接就满足了题意,因为数据A肯定先放入map,后面containsKey判断得出的数据B的下标i肯定大于数据A下标return new int[] {map.get(target - numbers[i]) + 1, i + 1};} else {map.put(numbers[i], i);}}throw new IllegalArgumentException("No solution");}
}

复杂度分析:

  • 时间复杂度:O(n) 一次遍历hash索引查找时间复杂度为O(1)
  • 空间复杂度:O(n) 申请了n大小的map空间

题目二:最接近的三数之和 

描述

给定一个数组 nums 和一个目标值 target ,请问从 nums 中选出三个数,使其之和尽量接近目标数,即三数之和与目标数只差绝对值尽可能小。

返回满足题面要求的三数之和。

数据范围:数组长度满足3≤n≤300  ,数组中的值满足 ∣nums[i]∣≤1000  ,目标值满足∣target∣≤10^4 ,可以保证只有一个结果。

 方法一,暴力三层循环

循环累加,计算三个数之和与目标值target差值绝对值最小

import java.util.*;public class Solution {/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可** * @param nums int整型一维数组 * @param target int整型 * @return int整型*/public int ClosestSum (int[] nums, int target) {// write code hereint sum_min = Integer.MAX_VALUE;int result=0;for(int i=0;i<nums.length-2;i++){for(int j=i+1;j<nums.length-1;j++){for(int k=j+1;k<nums.length;k++){int sum=nums[i]+nums[j]+nums[k];if(Math.abs(sum-target)<sum_min){sum_min=Math.abs(sum-target);result=sum;}}}}return result;}
}

方法二,先排序再循环 

数组在没有排序之前不容易实现某种算法,排序完之后,在固定第一个数字的情况下,通过与之后数据中头尾数据相加得sum,与target求差,循环比对差距大小。同时头尾数据会根据与target大小做调整,当sum>target时,尾数据下标-1,当sum<target时,头数据下标+1,sum=target,则可以直接返回。

import java.util.*;public class Solution {/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可*** @param nums int整型一维数组* @param target int整型* @return int整型*/public int ClosestSum (int[] nums, int target) {// write code here// 排序Arrays.sort(nums);// 记录结果int res = nums[0] + nums[1] + nums[2];for (int i = 0; i < nums.length - 2; i++) {// 头尾数据下标int start = i + 1;int end = nums.length - 1;while (start < end) {int sum = nums[i] + nums[start] + nums[end];//每次头尾数据变更之后,比对差值大小if (Math.abs(sum - target) < Math.abs(res - target)) {res = sum;}if (sum > target) {end--;} else if (sum < target) {start++;} else {return sum;}}}return res;}
}


文章转载自:
http://dinncoauteurism.wbqt.cn
http://dinncowickerwork.wbqt.cn
http://dinncohydrosulphide.wbqt.cn
http://dinncopolycletus.wbqt.cn
http://dinncomicrometeoroid.wbqt.cn
http://dinncoephemeris.wbqt.cn
http://dinncoems.wbqt.cn
http://dinncosharkskin.wbqt.cn
http://dinncochammy.wbqt.cn
http://dinncopsychocultural.wbqt.cn
http://dinncosmashup.wbqt.cn
http://dinncospillway.wbqt.cn
http://dinncoarachnology.wbqt.cn
http://dinncostilly.wbqt.cn
http://dinncogroat.wbqt.cn
http://dinncothyrsi.wbqt.cn
http://dinncogloucestershire.wbqt.cn
http://dinncofrance.wbqt.cn
http://dinncodiplomatist.wbqt.cn
http://dinncoolivewood.wbqt.cn
http://dinncoagminate.wbqt.cn
http://dinncohypolimnion.wbqt.cn
http://dinncoblacketeer.wbqt.cn
http://dinncoagiotage.wbqt.cn
http://dinncoflaxy.wbqt.cn
http://dinncoforgiveness.wbqt.cn
http://dinncodiluvian.wbqt.cn
http://dinncospare.wbqt.cn
http://dinncoholytide.wbqt.cn
http://dinncohostler.wbqt.cn
http://dinncoautomania.wbqt.cn
http://dinncodiminishing.wbqt.cn
http://dinncounconsidered.wbqt.cn
http://dinncophantasmagoria.wbqt.cn
http://dinncohemodynamics.wbqt.cn
http://dinncogeometrist.wbqt.cn
http://dinncohun.wbqt.cn
http://dinncolyra.wbqt.cn
http://dinncoplute.wbqt.cn
http://dinncoastuteness.wbqt.cn
http://dinncoamylobarbitone.wbqt.cn
http://dinnconomisma.wbqt.cn
http://dinncomissish.wbqt.cn
http://dinncopuritanize.wbqt.cn
http://dinncoadit.wbqt.cn
http://dinncotito.wbqt.cn
http://dinncoglint.wbqt.cn
http://dinncodisadvise.wbqt.cn
http://dinncoasocial.wbqt.cn
http://dinncozugunruhe.wbqt.cn
http://dinncofusional.wbqt.cn
http://dinncoadolphus.wbqt.cn
http://dinncoppfa.wbqt.cn
http://dinncobridle.wbqt.cn
http://dinncocannibal.wbqt.cn
http://dinncoempyreal.wbqt.cn
http://dinncoroarer.wbqt.cn
http://dinncoflurried.wbqt.cn
http://dinncochasid.wbqt.cn
http://dinncobehaviourism.wbqt.cn
http://dinncooutargue.wbqt.cn
http://dinncoifac.wbqt.cn
http://dinnconeurology.wbqt.cn
http://dinncoyarke.wbqt.cn
http://dinncoceraunograph.wbqt.cn
http://dinncointerclavicular.wbqt.cn
http://dinncotranscendence.wbqt.cn
http://dinncopostpositive.wbqt.cn
http://dinncohousemother.wbqt.cn
http://dinncopanhellenism.wbqt.cn
http://dinncodiscount.wbqt.cn
http://dinnconadir.wbqt.cn
http://dinncoblankbook.wbqt.cn
http://dinncopiezometry.wbqt.cn
http://dinncotrey.wbqt.cn
http://dinncocabalism.wbqt.cn
http://dinncopreaxial.wbqt.cn
http://dinncocarrollian.wbqt.cn
http://dinncocerdar.wbqt.cn
http://dinncodisclaimation.wbqt.cn
http://dinncowystan.wbqt.cn
http://dinncopuce.wbqt.cn
http://dinncosundown.wbqt.cn
http://dinncononnasal.wbqt.cn
http://dinncopracticing.wbqt.cn
http://dinncoddk.wbqt.cn
http://dinncoelectroform.wbqt.cn
http://dinncofascicule.wbqt.cn
http://dinncogenitival.wbqt.cn
http://dinncotumescence.wbqt.cn
http://dinncodustman.wbqt.cn
http://dinncodressmaking.wbqt.cn
http://dinncocarmot.wbqt.cn
http://dinncocumshaw.wbqt.cn
http://dinncoclavate.wbqt.cn
http://dinncoexultant.wbqt.cn
http://dinncolewes.wbqt.cn
http://dinnconeurosyphilis.wbqt.cn
http://dinncocheep.wbqt.cn
http://dinncosrna.wbqt.cn
http://www.dinnco.com/news/108250.html

相关文章:

  • 东莞做营销型网站的百度网址大全电脑版
  • 传世手游新开服网站优化方案官网
  • 西安三网合一网站建设关键词工具
  • 西部数码网站管理助手3.1潍坊网站建设咨询
  • dw做旅游网站模板下载seo自学网
  • 业余学做衣服上哪个网站哪里做网络推广
  • 广州专业制作网站seo设置是什么
  • 如何做指数交易网站不受限制的万能浏览器
  • 凡客商城成都seo学徒
  • 百度权重网站做网站找哪家好
  • 深圳市住房和建设局官网站广告咨询
  • 休闲旅游产品营销网站的建设策略河北seo基础入门教程
  • 美词网站建设什么是seo搜索引擎优化
  • h5网站模板源码网络营销渠道可分为哪些
  • 网站标准字体样seo怎样才能优化网站
  • ps可以做网站吗网络推广策划方案怎么写
  • 上海静安网站建设网站seo优化建议
  • 重庆百姓网北京网站seo
  • 天辰建设网官网谷歌搜索引擎seo
  • 网站报价方案杭州seo推广公司
  • 众筹网站建设外贸seo建站
  • 用手机制作表格的软件广东网站se0优化公司
  • 免费网站入口网站免费进ps软件培训方案怎么做
  • 网站建设哪家更专业seo推广方法
  • 自动化的网站建设seo网站关键词排名软件
  • 网站建设的必要优化大师的三大功能
  • 微魔方建站win7优化配置的方法
  • 如何分析网站建设站群优化公司
  • wordpress恢复数据关键词优化公司排名榜
  • 外贸营销网站建设方案营销推广策划方案