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

做淘宝客怎样建网站路由优化大师官网

做淘宝客怎样建网站,路由优化大师官网,搜了网推广效果怎么样,王野天津文章目录 题目标题和出处难度题目描述要求示例数据范围进阶 解法一思路和算法代码复杂度分析 解法二思路和算法代码复杂度分析 题目 标题和出处 标题:有序数组的平方 出处:977. 有序数组的平方 难度 2 级 题目描述 要求 给定按非递减顺序排序的整…

文章目录

  • 题目
    • 标题和出处
    • 难度
    • 题目描述
      • 要求
      • 示例
      • 数据范围
      • 进阶
  • 解法一
    • 思路和算法
    • 代码
    • 复杂度分析
  • 解法二
    • 思路和算法
    • 代码
    • 复杂度分析

题目

标题和出处

标题:有序数组的平方

出处:977. 有序数组的平方

难度

2 级

题目描述

要求

给定按非递减顺序排序的整数数组 nums \texttt{nums} nums,返回每个数字的平方组成的新数组,要求也按非递减顺序排序。

示例

示例 1:

输入: nums = [-4,-1,0,3,10] \texttt{nums = [-4,-1,0,3,10]} nums = [-4,-1,0,3,10]
输出: [0,1,9,16,100] \texttt{[0,1,9,16,100]} [0,1,9,16,100]
解释:平方后,数组变为 [16,1,0,9,100] \texttt{[16,1,0,9,100]} [16,1,0,9,100]。排序后,数组变为 [0,1,9,16,100] \texttt{[0,1,9,16,100]} [0,1,9,16,100]

示例 2:

输入: nums = [-7,-3,2,3,11] \texttt{nums = [-7,-3,2,3,11]} nums = [-7,-3,2,3,11]
输出: [4,9,9,49,121] \texttt{[4,9,9,49,121]} [4,9,9,49,121]

数据范围

  • 1 ≤ nums.length ≤ 10 4 \texttt{1} \le \texttt{nums.length} \le \texttt{10}^\texttt{4} 1nums.length104
  • -10 4 ≤ nums[i] ≤ 10 4 \texttt{-10}^\texttt{4} \le \texttt{nums[i]} \le \texttt{10}^\texttt{4} -104nums[i]104
  • nums \texttt{nums} nums 已按非递减顺序排序

进阶

计算每个元素的平方并对新数组排序的解法很简单,你可以使用不同的方法找到时间复杂度 O(n) \texttt{O(n)} O(n) 的解法吗?

解法一

思路和算法

最直观的解法是依次计算数组 nums \textit{nums} nums 中的每个元素的平方并存入新数组中,然后对新数组按非递减顺序排序,即可得到排序后的新数组。

代码

class Solution {public int[] sortedSquares(int[] nums) {int length = nums.length;int[] squares = new int[length];for (int i = 0; i < length; i++) {squares[i] = nums[i] * nums[i];}Arrays.sort(squares);return squares;}
}

复杂度分析

  • 时间复杂度: O ( n log ⁡ n ) O(n \log n) O(nlogn),其中 n n n 是数组 nums \textit{nums} nums 的长度。计算数组 nums \textit{nums} nums 中的每个元素的平方并存入新数组需要 O ( n ) O(n) O(n) 的时间,对新数组排序需要 O ( n log ⁡ n ) O(n \log n) O(nlogn) 的时间,因此时间复杂度是 O ( n log ⁡ n ) O(n \log n) O(nlogn)

  • 空间复杂度: O ( log ⁡ n ) O(\log n) O(logn),其中 n n n 是数组 nums \textit{nums} nums 的长度。对新数组排序需要 O ( log ⁡ n ) O(\log n) O(logn) 的递归调用栈空间。注意返回值不计入空间复杂度。

解法二

思路和算法

解法一没有利用到数组 nums \textit{nums} nums 已经按非递减顺序排序的条件,因此需要对新数组排序,时间复杂度是 O ( n log ⁡ n ) O(n \log n) O(nlogn)。如果利用数组 nums \textit{nums} nums 已经按非递减顺序排序的条件,则不需要对新数组排序,将时间复杂度降低到 O ( n ) O(n) O(n)

由于一个数的平方大小与这个数的绝对值有关,因此考虑数组 nums \textit{nums} nums 中的绝对值最大元素与绝对值最小元素可能出现的位置。

数组 nums \textit{nums} nums 按非递减顺序排序,可能有以下三种情况:

  • 数组 nums \textit{nums} nums 的所有元素都是非负数,元素顺序为绝对值非递减顺序,首个元素的绝对值最小,末尾元素的绝对值最大;

  • 数组 nums \textit{nums} nums 的所有元素都是非正数,元素顺序为绝对值非递增顺序,首个元素的绝对值最大,末尾元素的绝对值最小;

  • 数组 nums \textit{nums} nums 中既有正数也有负数,首个元素或末尾元素的绝对值最大。

对于上述三种情况中的任意一种情况,绝对值最大的元素一定是数组 nums \textit{nums} nums 的首个元素或末尾元素。因此可以从数组 nums \textit{nums} nums 的两端向中间遍历,按照绝对值从大到小的顺序依次遍历数组 nums \textit{nums} nums 的元素,计算每个元素的平方,反向填入新数组。

具体做法是,维护两个下标 index 1 \textit{index}_1 index1 index 2 \textit{index}_2 index2,初始时 index 1 \textit{index}_1 index1 指向数组 nums \textit{nums} nums 的首个元素, index 2 \textit{index}_2 index2 指向数组 nums \textit{nums} nums 的末尾元素。遍历过程中,比较 nums [ index 1 ] \textit{nums}[\textit{index}_1] nums[index1] nums [ index 2 ] \textit{nums}[\textit{index}_2] nums[index2] 这两个元素的绝对值:

  • 如果 nums [ index 1 ] \textit{nums}[\textit{index}_1] nums[index1] 的绝对值大于 nums [ index 2 ] \textit{nums}[\textit{index}_2] nums[index2] 的绝对值,则将 nums [ index 1 ] \textit{nums}[\textit{index}_1] nums[index1] 的平方填入新数组,将 index 1 \textit{index}_1 index1 1 1 1

  • 如果 nums [ index 1 ] \textit{nums}[\textit{index}_1] nums[index1] 的绝对值小于等于 nums [ index 2 ] \textit{nums}[\textit{index}_2] nums[index2] 的绝对值,则将 nums [ index 2 ] \textit{nums}[\textit{index}_2] nums[index2] 的平方填入新数组,将 index 2 \textit{index}_2 index2 1 1 1

由于遍历数组 nums \textit{nums} nums 的过程中,每次遍历的元素都是尚未遍历的元素中的绝对值最大的元素,因此遍历元素的顺序是绝对值非递增顺序,即元素的平方非递增顺序。将遍历的元素的平方反向填入新数组,新数组中的元素顺序为非递减顺序。

代码

class Solution {public int[] sortedSquares(int[] nums) {int length = nums.length;int[] squares = new int[length];int index1 = 0, index2 = length - 1;for (int i = length - 1; i >= 0; i--) {if (Math.abs(nums[index1]) > Math.abs(nums[index2])) {squares[i] = nums[index1] * nums[index1];index1++;} else {squares[i] = nums[index2] * nums[index2];index2--;}}return squares;}
}

复杂度分析

  • 时间复杂度: O ( n ) O(n) O(n),其中 n n n 是数组 nums \textit{nums} nums 的长度。需要遍历数组 nums \textit{nums} nums 中的每个元素一次。

  • 空间复杂度: O ( 1 ) O(1) O(1)。注意返回值不计入空间复杂度。

http://www.hkea.cn/news/58693/

相关文章:

  • 征信报告优化大师好用吗
  • 一个ip地址做多个网站网络营销公司是做什么的
  • 赤峰网站建设公司旅行网站排名
  • seo网站优化工具软件拉新推广平台
  • 政府网站设计方案无锡网站制作优化
  • 社交网站盈利吗如何在各种网站投放广告
  • 建设工程合同民法典东莞快速优化排名
  • 泗县口碑营销互联网营销推荐咨询青岛seo计费
  • 做电影网站用什么服务器中文域名注册官网入口
  • 做网站那里好win10系统优化工具
  • 做外贸上哪些网站找客户口碑营销的概念是什么
  • 外贸建站magento百度地图推广
  • 做网站和管理系统全网热度指数
  • 网站的压力测试怎么做网络营销的目的是什么
  • 网站建设如何更加稳定seo优化前景
  • 网站速度诊断 慢seo chinaz
  • 企业网站建设合同搜索引擎优化关键词的处理
  • 做翻译的网站私人做网站
  • 广东两学一做考学网站百度网盘app免费下载安装老版本
  • 淄博有做网站的吗百度推广有哪些形式
  • 增值税怎么算免费seo快速排名系统
  • 响应式电商网站制作全网
  • 国内b2b网站a片长春做网站推广的公司
  • 网页制作视频的网站建设引擎优化seo怎么做
  • 吉林省建设厅证件查询网站今日热搜
  • 做熟食的网站美食网站搜索引擎推广方案案例
  • 企业模板网站推广优化平台
  • 赣州做网站的网站优化技术
  • 设计网站大全网公司推广咨询
  • 北京网站建设价格上海关键词排名优化公司