LeetCode 360. Sort Transformed Array
原题链接在这里:https://leetcode.com/problems/sort-transformed-array/description/
题目:
Given a sorted array of integers nums and integer values a, b 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;
}
}
LeetCode 360. Sort Transformed Array的更多相关文章
- [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( ...
- 360. Sort Transformed Array二元一次方程返回大数序列
[抄题]: Given a sorted array of integers nums and integer values a, b and c. Apply a quadratic functio ...
- 360. Sort Transformed Array
一元二次方程...仿佛回到了初中. 主要看a的情况来分情况讨论: =0,一次函数,根据b的正负单调递增递减就行了. <0,凸状..从nums[]左右两边开始往中间一边比较一边 从右往左 放: 0 ...
- 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( ...
- [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 ...
- [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( ...
- 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( ...
- Sort Transformed Array
Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...
- Leetcode 912. Sort an Array
class Solution: def sortArray(self, nums: List[int]) -> List[int]: return sorted(nums)
随机推荐
- 【三小时学会Kubernetes!(五) 】完成整个架构
完成整个架构 现在我们学习了完成架构的所有必须的资源,因此这一节会非常快.图 22 中灰色的部分是需要做的事情.让我们从底部开始:部署 sa-logic 的部署. 图 22:当前应用程序状态 部署 S ...
- Spring ApplicationListener 理解
在开发时有时候需要在整个应用开始运行时执行一些特定代码,比如初始化环境,准备测试数据.加载一些数据到内存等等. 在spring中可以通过ApplicationListener来实现相关的功能,加载完成 ...
- MySQL之长连接、短连接、连接池
当数据库服务器和客户端位于不同的主机时,就需要建立网络连接来进行通信.客户端必须使用数据库连接来发送命令和接收应答.数据.通过提供给客户端数据库的驱动指定连接字符串后,客户端就可以和数据库建立连接了. ...
- Nginx、Apache工作原理及Nginx为何比Apache高效
Nginx才短短几年,就拿下了web服务器大笔江山,众所周知,Nginx在处理大并发静态请求方面,效率明显高于httpd,甚至能轻松解决C10K问题. 在高并发连接的情况下,Nginx是Apache服 ...
- rabbitmq&&erlang 安装
# yum install epel-release CentOS and Red Hat Enterprise Linux 6.x wget https://dl.fedoraproject.org ...
- ansible入门三(Ansible的基础元素和YAML介绍)
Ansible的基础元素和YAML介绍 本节内容: YAML Ansible常用的数据类型 Ansible基础元素 一.YAML 1. YAML介绍 YAML是一个可读性高的用来表达资料序列的格式 ...
- Tree各种遍历实现
数据结构.算法及应用 张宪超主编 科学出版社 1. 数据结构的基本概念知识 数据结构的逻辑结构由数据节点和连接两个节点的边组成. 数据节点的数据类型:整型,实数型,布尔型,字符型,指针数据类型 结构的 ...
- 二十二、utl_inaddr(用于取得局域网或Internet环境中的主机名和IP地址)
1.概述 作用:用于取得局域网或Internet环境中的主机名和IP地址. 2.包的组成 1).get_host_name作用:用于取得指定IP地址所对应的主机名语法:utl_inaddr.get_h ...
- 4: 模块化应用程序开发 Modular Application Development Using Prism Library 5.0 for WPF (英汉对照版)
A modular application is an application that is divided into a set of loosely coupled functional uni ...
- Python基础学习----元组
# 元组和列表的区别: # 相同点: # 1.表示多个元素的序列 # 2.数据之间用 , 分开 # 3.索引都是[0]开始 # # 不同点: # 1.列表创建是[],元组是()表示 # 2.元组元素一 ...