Leetcode 360 Sort Transformed Array
来源:互联网 发布:英格拉姆弹跳数据 编辑:程序博客网 时间:2024/06/08 10:21
Given a sorted array of integers nums and integer values a, b and c. Apply a 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 = 5Result: [-23, -5, 1, 7]抛物线开口朝上,max一定出现在两端,抛物线开口朝下,min一定出现在两端。
用双指针来进行排序。
public class Solution { private int calcu(int x, int a, int b, int c){ return a*x*x + b*x + c; } public int[] sortTransformedArray(int[] nums, int a, int b, int c) { int index; if(a > 0){ index = nums.length - 1 ; } else { index = 0; } int result[] = new int[nums.length]; int i = 0; int j = nums.length - 1; if(a > 0){ while(i <= j){ result[index--] = calcu(nums[i],a,b,c) > calcu(nums[j],a,b,c) ? calcu(nums[i++],a,b,c):calcu(nums[j--],a,b,c); } }else{ while(i <= j){ result[index++] = calcu(nums[i],a,b,c) < calcu(nums[j],a,b,c) ? calcu(nums[i++],a,b,c):calcu(nums[j--],a,b,c); } } return result; }}
阅读全文
0 0
- Leetcode 360 Sort Transformed Array
- LeetCode 360. Sort Transformed Array
- LeetCode 360. Sort Transformed Array
- [leetcode] 360. Sort Transformed Array 解题报告
- [Leetcode] 360. Sort Transformed Array 解题报告
- Sort Transformed Array
- 360. Sort Transformed Array
- LeetCode 360. Sort Transformed Array(计算函数)
- leetcode.array--75. Sort Colors
- Array#sort
- Array Sort
- array.sort()
- Array.sort()
- Array.Sort
- 【leetcode】Array——Sort Color(75)
- [LeetCode]--242. Valid Anagram(Count table && char array sort())
- Array.Sort方法
- java.util.array.sort
- uitableview contentview 添加子view出现约束错误
- html中img图片路径src不对的问题
- EA&UML日拱一卒--活动图::入门
- CS224d Assignment1 答案, Part(2/4)
- 在邻接矩阵中的DFS与BFS的实现
- Leetcode 360 Sort Transformed Array
- vim安装插件YouCompleteMe,出错,YouCompleteMe unavailable: requires Vim compiled with Python 2.x support
- 编译防火墙——C++的Pimpl惯用法解析
- socket学习
- linux线程系列(5)线程清理和控制函数
- SVM简单代码实现MATLAB
- CS224d Assignment1 答案, Part(3/4)
- C++ 将当前系统时间转换成标准格式的时间和时间戳
- KEIL MDK 查看代码量、RAM使用情况--RO-data、RW-data、ZI-data的解释