原题链接在这里:https://leetcode.com/problems/sort-transformed-array/description/

题目:

Given a sorted array of integers nums and integer values ab and c. Apply a quadratic function of the form f(x) = ax2 + bx + c to each element x in the array.

The returned array must be in sorted order.

Expected time complexity: O(n)

Example:

nums = [-4, -2, 2, 4], a = 1, b = 3, c = 5,

Result: [3, 9, 15, 33]

nums = [-4, -2, 2, 4], a = -1, b = 3, c = 5

Result: [-23, -5, 1, 7]

题解:

如果a > 0, 左右两边的点就比中间的点大. two pointers夹比, 从大往小赋值.

a < 0, 左右两边的点比中间的点小. two pointers夹比, 从小往大赋值.

Time Complexity: O(n). n = nums.length.

Space: O(1). regardless res.

AC Java:

 class Solution {
public int[] sortTransformedArray(int[] nums, int a, int b, int c) {
if(nums == null || nums.length == 0){
return nums;
} int [] res = new int[nums.length];
int l = 0;
int r = nums.length-1;
int index = a >=0 ? nums.length-1 : 0; while(l <= r){
int lNum = quad(nums[l], a, b, c);
int rNum = quad(nums[r], a, b, c);
if(a>=0){
if(lNum > rNum){
res[index--] = lNum;
l++;
}else{
res[index--] = rNum;
r--;
}
}else{
if(lNum > rNum){
res[index++] = rNum;
r--;
}else{
res[index++] = lNum;
l++;
}
}
} return res;
} private int quad(int x, int a, int b, int c){
return a*x*x + b*x + c;
}
}

类似Squares of a Sorted Array.

LeetCode 360. Sort Transformed Array的更多相关文章

  1. [LeetCode] 360. Sort Transformed Array 排序转换后的数组

    Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...

  2. 360. Sort Transformed Array二元一次方程返回大数序列

    [抄题]: Given a sorted array of integers nums and integer values a, b and c. Apply a quadratic functio ...

  3. 360. Sort Transformed Array

    一元二次方程...仿佛回到了初中. 主要看a的情况来分情况讨论: =0,一次函数,根据b的正负单调递增递减就行了. <0,凸状..从nums[]左右两边开始往中间一边比较一边 从右往左 放: 0 ...

  4. Leetcode: Sort Transformed Array

    Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...

  5. [LeetCode] 912. Sort an Array 数组排序

    Given an array of integers nums, sort the array in ascending order. Example 1: Input: [5,2,3,1] Outp ...

  6. [LeetCode] Sort Transformed Array 变换数组排序

    Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...

  7. Sort Transformed Array -- LeetCode

    Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...

  8. Sort Transformed Array

    Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...

  9. Leetcode 912. Sort an Array

    class Solution: def sortArray(self, nums: List[int]) -> List[int]: return sorted(nums)

随机推荐

  1. 使用location.hash保存页面状态

    hash 属性是一个可读可写的字符串,该字符串是 URL 的锚部分(从 # 号开始的部分). 语法 location.hash 在我们的项目中,有大量ajax查询表单+结果列表的页面,由于查询结果是a ...

  2. Android显示框架:自定义View实践之绘制篇

    文章目录 一 View 二 Paint 2.1 颜色处理 2.2 文字处理 2.3 特殊处理 三 Canvas 3.1 界面绘制 3.2 范围裁切 3.3 集合变换 四 Path 4.1 添加图形 4 ...

  3. phpstorm 右下角显示updating indices,一直有任务卡着

    其实就是生成的这个node_modules目录内文件太多了,选中node_modules这个目录右键,选择Excluded 一直在加载忽略掉这个文件就可以了

  4. Hive 元数据库表信息

    Hive 的元数据信息通常存储在关系型数据库中,常用MySQL数据库作为元数据库管理. 1. 版本表 i) VERSION   -- 查询版本信息 2. 数据库.文件存储相关 i) DBS -- 存储 ...

  5. 转载:10+ handy and reusable jQuery code snippets

    源地址:http://www.catswhocode.com/blog/10-handy-and-reusable-jquery-code-snippets Smooth scrolling to t ...

  6. 微信小程序------轮播图

    swiper 微信小程序实现轮播图,和网站,APP的效果差不多,代码少,效率高. 先来看看效果图: 主要用swiper + swiper-item来实现 <view class='swiper' ...

  7. How to find per-process I/O statistics on Linux

    以下转自http://www.xaprb.com/blog/2009/08/23/how-to-find-per-process-io-statistics-on-linux/ Newer Linux ...

  8. jQuery-轮播图(友善滴滚动切换)

    线上实例:http://lgy.1zwq.com/slide/ [处理] 这里的图片滚动轮播,做了点小处理:当在第1页状态时,你点击第5页,图片的滚动是一张滑过,而不是从2-3-4-5(这种的多张滚动 ...

  9. linux命令生成公私钥

    生成原始rsa私钥文件: openssl genrsa -out rsa_private_key.pem 1024 将原始的rsa私钥转换未pkcs8格式(即生成私钥文件): openssl pkcs ...

  10. garylog学习篇

    官方文档:https://www.graylog.org/ 简介 Graylog 是一个简单易用.功能较全面的日志管理工具,相比 ELK 组合, 优点: 部署维护简单查询语法简单易懂(对比ES的语法… ...