leetcode977.有序数组的平方
2019-05-06 15:37
477 查看
leetcode977.有序数组的平方
代码
python2.7
class Solution(object): def sortedSquares(self, A): """ :type A: List[int] :rtype: List[int] """ return sorted( [i*i for i in A] )
C++
class Solution { public: int abs(int x){ return (x>=0)?x:(-x); } vector<int> sortedSquares(vector<int>& A) { vector<int> ans(A.size()); int i=0, j=A.size()-1; int index = j; while (index>=0){ if (abs(A[i]) >= abs(A[j]) ){ ans[index] = A[i]*A[i]; i++; index--; } else { ans[index] = A[j]*A[j]; j--; index--; } } return ans; } };
总结
- python中array.sort()无return值,sorted(array)有return值,都是对array排序;
- C++STL中vector用法!见CSDN/C++/vector
相关文章推荐
- LeetCode 977. 有序数组的平方
- LeetCode 977. 有序数组的平方
- Leetcode刷题2-977.有序数组的平方(C++)
- LeetCode977.有序数组的平方(python)
- LeetCode 有序数组的平方
- leetcode:有序数组的平方
- leetcode刷题思路总结_有序数组的平方
- 977[leetcode]C++实现有序数组的平方(Squares of a Sorted Array)
- leetcode 977、有序数组的平方(双指针)
- LeetCode每日刷题Day2--- 701转换成小写字母、997有序数组的平方、985查询后的偶数和
- [PHP In Leetcode 977] 有序数组的平方
- LeetCode 88. Merge Sorted Array(合并有序数组)
- 【LeetCode】Python实现-167. 两数之和 II - 输入有序数组
- LeetCode-3Sum -三数求和-有序数组扫描
- LeetCode--旋转有序数组大总结
- Leetcode || 将有序数组转换为二叉搜索树
- [LeetCode] 4. Median of Two Sorted Arrays 两个有序数组的中位数
- leetcode160 两数之和 II - 输入有序数组--解法
- leetcode 合并两个有序数组
- LeetCode 33 Search in Rotated Sorted Array(循环有序数组中进行查找操作)