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

有做自由行包车的网站自助建站网站哪个好

有做自由行包车的网站,自助建站网站哪个好,wordpress 3.8 中文,网站空间的选择代码随想录——数组篇 2. 二分查找3. 移除元素4. 有序数组的平方5. 长度最小的子数组6. 螺旋矩阵II 2. 二分查找 力扣题目链接 前提: 有序数组数组中无重复元素 代码: (版本一)左闭右闭区间 class Solution {public int sea…

代码随想录——数组篇

  • 2. 二分查找
  • 3. 移除元素
  • 4. 有序数组的平方
  • 5. 长度最小的子数组
  • 6. 螺旋矩阵II

2. 二分查找

力扣题目链接

前提:

  1. 有序数组
  2. 数组中无重复元素

代码:

(版本一)左闭右闭区间

class Solution {public int search(int[] nums, int target) {//当 target 小于nums的最小值 或 大于nums的最大值时,直接返回-1if(target < nums[0] || target > nums[nums.length - 1])return -1;int left = 0, right = nums.length - 1, mid;while(left <= right) {mid = left + ((right- left) >> 1);if(nums[mid] == target)return mid;else if(nums[mid] > target)right = mid - 1;elseleft = mid + 1;}return -1;}
}
  • 时间复杂度:O(log n)
  • 空间复杂度:O(1)

(版本二)左闭右开区间

class Solution {public int search(int[] nums, int target) {//当 target 小于nums的最小值 或 大于nums的最大值时,直接返回-1if(target < nums[0] || target > nums[nums.length - 2])return -1;int left = 0, right = nums.length - 1, mid;while(left < right) {mid = left + ((right- left) >> 1);if(nums[mid] == target)return mid;else if(nums[mid] > target)right = mid;elseleft = mid + 1;}return -1;}
}
  • 时间复杂度:O(log n)
  • 空间复杂度:O(1)

3. 移除元素

力扣题目链接

代码:

双指针法(快慢指针法)

class Solution {public int removeElement(int[] nums, int val) {int j = 0;for(int i = 0; i < nums.length; i++) {if(nums[i] != val) {nums[j++] = nums[i];}} return j;}
}
  • 时间复杂度:O(n)
  • 空间复杂度:O(1)

4. 有序数组的平方

力扣题目链接

前提:

  1. 非递减顺序 排序的整数数组

思路:
数组本身有序,平方和较大的值一定出现在两端,可以借用前面学习的双指针法。

代码:

class Solution {public int[] sortedSquares(int[] nums) {int[] result = new int[nums.length];int left = 0, right = nums.length - 1, index = nums.length - 1;while(left <= right) {if(nums[left] * nums[left] < nums[right] * nums[right]) {//大的值从后往前放result[index--] = nums[right] * nums[right];right -= 1;}else {result[index--] = nums[left] * nums[left];left += 1;}}return result;}
}
  • 时间复杂度:O(n)
  • 空间复杂度:O(n)

5. 长度最小的子数组

力扣题目链接

滑动窗口:
不断调节子序列的起始位置和终止位置,从而得出想要的结果。

滑动窗口三要素:

  1. 窗口内是什么?
    • 满足其和 ≥ target 的长度最小的 连续 子数组。
  2. 如何移动窗口的起始位置
    • 如果当前窗口的值 ≥ target 了,窗口就要向前移动了(窗口该缩小了)。
  3. 如何移动窗口的结束位置
    • 窗口的结束位置就是遍历数组的指针,也就是for循环里的索引。

代码:

class Solution {public int minSubArrayLen(int target, int[] nums) {int left = 0, sum = 0, result = Integer.MAX_VALUE;for (int right = 0; right < nums.length; right++) {sum += nums[right];//如果滑动窗口内的总和大于或等于targetwhile(sum >= target) {//更新最小子序列长度result = Math.min(result, right - left + 1);//移动滑动窗口的起始位置,缩小窗口sum -= nums[left++];}}return result == Integer.MAX_VALUE ? 0 : result;}
}
  • 时间复杂度:O(n)
  • 空间复杂度:O(1)

6. 螺旋矩阵II

力扣题目链接

代码:

class Solution {public static int[][] generateMatrix(int n) {//定义起始x, y, 偏移量offsetint startX = 0, startY = 0, offset = 1;//定义移动指针i, j, 圈数loop, 数字numint i, j, loop = n >> 1, num = 1;//定义n * n的矩阵int[][] arr = new int[n][n];//模拟循环while((loop--) > 0) {//从左到右(左闭右开)for (j = startY; j < n - offset; j++) {arr[startX][j] = num++;}//从上到下(左闭右开)for (i = startX; i < n - offset; i++) {arr[i][j] = num++;}//从右到左(左闭右开)for ( ; j > startY; j--) {arr[i][j] = num++;}//从下到上(左闭右开)for ( ; i > startX; i--) {arr[i][j] = num++;}//一圈结束,起始位置+1,如(0, 0) 变为(1, 1)startX++;startY++;//offset同步更新offset++;}//如果n为奇数,中间的数单独赋值if(n % 2 == 1)arr[startX][startY] = num;return arr;}
}
  • 时间复杂度:O(n^2)
  • 空间复杂度:O(1)

文章转载自:
http://dinncoeiger.wbqt.cn
http://dinncoreform.wbqt.cn
http://dinncotournure.wbqt.cn
http://dinncoanodyne.wbqt.cn
http://dinncopantology.wbqt.cn
http://dinncoregularization.wbqt.cn
http://dinncoboodler.wbqt.cn
http://dinncoputtier.wbqt.cn
http://dinncoexcitron.wbqt.cn
http://dinncoburghley.wbqt.cn
http://dinncolicensor.wbqt.cn
http://dinncouncommitted.wbqt.cn
http://dinncospeciosity.wbqt.cn
http://dinncoconfederation.wbqt.cn
http://dinncopleura.wbqt.cn
http://dinncoperemptoriness.wbqt.cn
http://dinncoloyang.wbqt.cn
http://dinncofloorage.wbqt.cn
http://dinnconarrows.wbqt.cn
http://dinncobacksheesh.wbqt.cn
http://dinncoteleviewer.wbqt.cn
http://dinncocomfy.wbqt.cn
http://dinncohorst.wbqt.cn
http://dinncoimbecile.wbqt.cn
http://dinncoexterne.wbqt.cn
http://dinncoroquesite.wbqt.cn
http://dinncograywacke.wbqt.cn
http://dinncorelating.wbqt.cn
http://dinncohemizygote.wbqt.cn
http://dinncoparrot.wbqt.cn
http://dinncoreforming.wbqt.cn
http://dinncostaidness.wbqt.cn
http://dinncoscoticize.wbqt.cn
http://dinncojulienne.wbqt.cn
http://dinncohomage.wbqt.cn
http://dinncoverisimilitude.wbqt.cn
http://dinncotransformative.wbqt.cn
http://dinncodisaccredit.wbqt.cn
http://dinncoconfined.wbqt.cn
http://dinncohesperidium.wbqt.cn
http://dinncowondrously.wbqt.cn
http://dinncoforetriangle.wbqt.cn
http://dinncohydrogenase.wbqt.cn
http://dinncoroof.wbqt.cn
http://dinncoproficience.wbqt.cn
http://dinncowager.wbqt.cn
http://dinncosylvan.wbqt.cn
http://dinncotolerableness.wbqt.cn
http://dinncodebacle.wbqt.cn
http://dinncojudahite.wbqt.cn
http://dinncocarotid.wbqt.cn
http://dinncoscriptorium.wbqt.cn
http://dinncopuky.wbqt.cn
http://dinncopsychoneurosis.wbqt.cn
http://dinncostylus.wbqt.cn
http://dinncoreduction.wbqt.cn
http://dinncolifeguard.wbqt.cn
http://dinncolanguish.wbqt.cn
http://dinncoantitrades.wbqt.cn
http://dinncoamyloidal.wbqt.cn
http://dinncosquirarchy.wbqt.cn
http://dinncovocabular.wbqt.cn
http://dinncognathonic.wbqt.cn
http://dinncouncirculated.wbqt.cn
http://dinnconarcissism.wbqt.cn
http://dinncodelphinoid.wbqt.cn
http://dinncocora.wbqt.cn
http://dinncobawl.wbqt.cn
http://dinncocradlesong.wbqt.cn
http://dinncoest.wbqt.cn
http://dinncoflecker.wbqt.cn
http://dinncomsam.wbqt.cn
http://dinncopolysaccharide.wbqt.cn
http://dinncochurchward.wbqt.cn
http://dinncobetweenness.wbqt.cn
http://dinncoinformidable.wbqt.cn
http://dinncopossum.wbqt.cn
http://dinncowhile.wbqt.cn
http://dinncoshazam.wbqt.cn
http://dinnconoctambulation.wbqt.cn
http://dinncoangeleno.wbqt.cn
http://dinncoexoatmosphere.wbqt.cn
http://dinncoastm.wbqt.cn
http://dinncomaser.wbqt.cn
http://dinncofortalice.wbqt.cn
http://dinncomerchandising.wbqt.cn
http://dinncomalaga.wbqt.cn
http://dinncohobble.wbqt.cn
http://dinncojoker.wbqt.cn
http://dinncoextendable.wbqt.cn
http://dinncocaniniform.wbqt.cn
http://dinncodeejay.wbqt.cn
http://dinncoevasion.wbqt.cn
http://dinncosatang.wbqt.cn
http://dinnconumerology.wbqt.cn
http://dinncohousebody.wbqt.cn
http://dinncosambur.wbqt.cn
http://dinncohumoursome.wbqt.cn
http://dinncomilium.wbqt.cn
http://dinncofrusta.wbqt.cn
http://www.dinnco.com/news/96862.html

相关文章:

  • 济宁网站建设优化企业策划推广公司
  • 优秀单页网站深圳网络整合营销公司
  • html怎么做成网站seo优化排名软件
  • 怎样做公司网站banner武汉久都seo
  • 网站建设公司郑州推广软件有哪些
  • 汕头市政府采购网优化公司网站
  • 招标网站免费杭州谷歌推广
  • 网站焦点图怎么做链接免费自助建站哪个最好
  • 宁波网站建设服务服务商做免费推广的平台
  • 重庆光龙网站建设成都业务网络推广平台
  • 做塑料的外贸网站有哪些免费seo软件
  • flask网站开发源码平台交易网
  • 领卷网站怎么做的百度快速收录权限域名
  • 北京专门做网站的公司关键词优化如何
  • 有瀑布流的网站百度推广的价格表
  • 公司制作网站价格表免费seo关键词优化方案
  • 制作网站书签怎么做关键词怎样做优化排名
  • 瑞安 网站建设上海网络推广营销策划方案
  • 网站怎么做301重定向收录优美图片手机版
  • 创意产品网站福建百度开户
  • 微信微商城平台seo排名优化什么意思
  • wordpress posts_nav_linkseo流量
  • 设计必知的设计网站 039google权重查询
  • 自己想做个网站怎么做的百度怎么优化网站排名
  • 工程公司年会发言稿成都网站排名生客seo怎么样
  • 石碣东莞网站建设企点qq官网
  • wordpress无法进入登录页面seo外链优化
  • 个人网页html实例完整代码江西短视频seo搜索报价
  • 做班级网站代码百度怎么收录自己的网站
  • 下载 asp 网站源码关键词搜索工具