977. Squares of a Sorted Array (Easy)#

    Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.

Example 1:

Input: [-4,-1,0,3,10]
Output: [0,1,9,16,100] Example 2: Input: [-7,-3,2,3,11]
Output: [4,9,9,49,121] Note: 1 <= A.length <= 10000
-10000 <= A[i] <= 10000
A is sorted in non-decreasing order.

solution##

我的解法

class Solution {
public int[] sortedSquares(int[] A) {
int[] B = new int[A.length];
int i = 0, j = A.length - 1;
for (int k = A.length - 1; k >= 0; k--)
{
if (Math.abs(A[i]) > Math.abs(A[j]))
{
B[k] = A[i] * A[i];
i++;
}
else
{
B[k] = A[j] * A[j];
j--;
}
}
return B;
}
}

官方提供的解法

class Solution {
public int[] sortedSquares(int[] A) {
int N = A.length;
int[] ans = new int[N];
for (int i = 0; i < N; ++i)
ans[i] = A[i] * A[i];
Arrays.sort(ans); //排序,默认为自然顺序
return ans;
}
}

reference:

https://leetcode.com/problems/squares-of-a-sorted-array/solution/

总结##

此题主要有两种思路:

  • 第一种最容易想到的就是先用一个新数组B存储A里面平方后的元素,然后再调用java自带的排序算法对B数组排序。
  • 第二种方法是新建一个数组B,然后用一个数字i记录A数组开始下标,数字j记录结束下标首先比较A[i]与A[j]的绝对值大小,若前者大,则将前者的平方存入数组B的尾部,随后i++;否则将后者的平方存入数组B尾部,随后j--,直至循环结束后返回B数组。

Notes:

1.第二种方法不太好想到,可能是我太菜!!

657. Robot Return to Origin (Easy)#

There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.

The move sequence is represented by a string, and the character moves[i] represents its ith move. Valid moves are R (right), L (left), U (up), and D (down). If the robot returns to the origin after it finishes all of its moves, return true. Otherwise, return false.

Note: The way that the robot is "facing" is irrelevant. "R" will always make the robot move to the right once, "L" will always make it move left, etc. Also, assume that the magnitude of the robot's movement is the same for each move.

Example 1:

Input: "UD"
Output: true
Explanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true. Example 2: Input: "LL"
Output: false
Explanation: The robot moves left twice. It ends up two "moves" to the left of the origin. We return false because it is not at the origin at the end of its moves.

solution##

class Solution {
public boolean judgeCircle(String moves) {
int x = 0 , y = 0;
for (char move : moves.toCharArray())
{
switch (move)
{
case 'U' : x++; break;
case 'D' : x--; break;
case 'L' : y++; break;
case 'R' : y--; break;
}
}
return (x == 0 && y == 0);
}
}

总结##

此题的意思就是判断机器人在一系列的移动之后是否能回到原点。只需设置两个计数器x和y,初始值均为0。如果机器人上移就x++,下移就x--,左移就y++,右移就y--,最后判断x和y是否均为0,为0就返回true,否则返回false。

Notes:

1.用switch语句效率更高;

2.可以字符串转为字符数组再用for-each语句遍历字符串;

3.&为逻辑与,如果运算符左边为false,则再检查运算符右边;&&为短路与,如果运算符左边为false,则直接返回false,不再检查运算符右边,类似于电路的短路;

最新文章

  1. Oracle 数据库重放(Database Replay)功能演示
  2. Google C++单元测试框架GoogleTest---TestFixture使用
  3. 修改了chrome的官方的有道词典插件,添加了生词本的功能
  4. CXF 入门:创建一个基于WS-Security标准的安全验证(CXF回调函数使用,)
  5. PHP clone
  6. Oracle抓取表结构的语句
  7. vs2005 ,2008,2010中引入app.manifest(即c#程序在win7下以管理员权限运行方法)
  8. java 操作 redis
  9. 取得 iframe 容器的 URL
  10. Redisson实现分布式锁
  11. 一个页面中使用多个UEditor
  12. Leetcode : eImplement strStr
  13. 将一台电脑上的虚拟机上的系统复制到另一台电脑的虚拟机上!!!and想询问大神们问题的解决办法??
  14. String的方法capitalize
  15. Oracle免客户端InstantClient安装使用
  16. 协议 protocol
  17. [K8S]污点调度
  18. Chrome+XX_Net的上网渠道
  19. Set常用子类特点
  20. JavaScript-实现滚动条

热门文章

  1. 2019-07-31【机器学习】无监督学习之降维NMF算法 (人脸特征提取)
  2. 三个步骤就能让你轻松掌握Python爬虫
  3. 原创Hbase1.2.1集群安装
  4. C++学习--编译优化
  5. Python冒泡排序算法及其优化
  6. [linux] [nginx] 一键安装web环境全攻略phpstudy版,超详细!
  7. 你知道什么是 GitHub Action 么?
  8. 解决sublime打开文档,出现中文乱码问题
  9. Mysql数据操作指令
  10. MySQL 主从复制:基于二进制文件复制配置详解