LeetCode 977. Squares of a Sorted Array (有序数组的平方)
题目标签:Array
题目给了我们一组 从小到大的 integers,让我们平方数字 并且 也排序成 从小到达。
因为有负数在里面,平方后,负数在array的位置会变动。
可以设left 和 right pointers,从两边遍历,比较一下两个平方后的数字,把大的那个 放入新建的array的末尾。
具体看code。
Java Solution:
Runtime beats 100.00%
完成日期:02/03/2019
关键点:two pointers
class Solution
{
public int[] sortedSquares(int[] A)
{
int left = 0;
int right = A.length - 1;
int[] result = new int[A.length];
int inputIndex = right; while(left <= right)
{
int leftInt = A[left] * A[left];
int rightInt = A[right] * A[right]; if(leftInt > rightInt)
{
result[inputIndex] = leftInt;
left++;
inputIndex--;
}
else
{
result[inputIndex] = rightInt;
right--;
inputIndex--;
}
}
return result;
}
}
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 977. Squares of a Sorted Array (有序数组的平方)的更多相关文章
- 977. Squares of a Sorted Array有序数组的平方
网址:https://leetcode.com/problems/squares-of-a-sorted-array/ 双指针法 把左端的元素和右端的元素比较后挑出绝对值大的,将其平方放入ans中,并 ...
- #Leetcode# 977. Squares of a Sorted Array
https://leetcode.com/problems/squares-of-a-sorted-array/ Given an array of integers A sorted in non- ...
- [LeetCode] Single Element in a Sorted Array 有序数组中的单独元素
Given a sorted array consisting of only integers where every element appears twice except for one el ...
- [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- [leetcode]26. Remove Duplicates from Sorted Array有序数组去重(单个元素只出现一次)
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- LeetCode 977 Squares of a Sorted Array 解题报告
题目要求 Given an array of integers A sorted in non-decreasing order, return an array of the squares of ...
- 【Leetcode_easy】977. Squares of a Sorted Array
problem 977. Squares of a Sorted Array solution: class Solution { public: vector<int> sortedSq ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
随机推荐
- zabbix3.0_网络发现问题
问题1. Zabbix网络发现system.uanem找不到主机,打开zabbix_server.conf文件的debug DebugLevel=5 # 错误信息如下 # item [system.u ...
- 00Cascading Style Sheet
Cascading Style Sheet CSS(Cascading Style Sheet)即层叠样式表,简称样式表.要理解层叠样式表的概念先要理解样式的概念.样式就是对网页中的 元素(字体.段落 ...
- wpf 自定义单选按钮 RadioButton
新建RadioButtonEx.cs public class RadioButtonEx : RadioButton { public Geometry SelectIcon { get { ret ...
- java命令行版的ATM
import java.util.*;public class Jatm{ static String user = "123"; static String password = ...
- zip相关知识梳理(一)
zip相关知识梳理(一) 经过对zip文件的长时间研究,对zip文件进行相关知识进行梳理,虽然网上很多牛人对其做了相关基础解析,但是对于特殊情况没有进行说明,比如超过4G的zip文件该以什么格式进行编 ...
- Jqueryd的一些 总结
JSP层 /*发送data 主要有三种方式:1.json 数组(推荐1)2.url拼接3.表单的序列化 serialize*/ <script type="text/javascrip ...
- Jmeter逻辑控制器-ForEach Controller
ForEach Controller 介绍 ForEach Contoller 即循环控制器,顾名思义是定义一个规则.主要有以下一个参数: 名称:随便填写 注释:随便填写 输入变量前缀:可以在&quo ...
- ebay 店铺状态
OrderStatusCodeType @XmlEnumValue("Active") @XmlEnumValue("Inactive") @XmlEnumVa ...
- 【Codeforces 682C】Alyona and the Tree
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 设dis[v]表示v以上的点到达这个点的最大权值(肯定是它的祖先中的某个点到这个点) 类似于最大连续累加和 当往下走(x,y)这条边的时候,设 ...
- NYOJ 832 合并游戏
合并游戏 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 大家都知道Yougth除了热爱编程之外,他还有一个爱好就是喜欢玩.某天在河边玩耍的时候,他发现了一种神奇的石 ...