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,不再检查运算符右边,类似于电路的短路;

LeetCode--Squares of a Sorted Array && Robot Return to Origin (Easy)的更多相关文章

  1. LeetCode:Search in Rotated Sorted Array I II

    LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...

  2. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

  3. LeetCode: Search in Rotated Sorted Array II 解题报告

    Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...

  4. LeetCode977.Squares of a Sorted Array

    题目 977. Squares of a Sorted Array Given an array of integers A sorted in non-decreasing order, retur ...

  5. 【Leetcode_easy】977. Squares of a Sorted Array

    problem 977. Squares of a Sorted Array solution: class Solution { public: vector<int> sortedSq ...

  6. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

  7. 【Leetcode_easy】657. Robot Return to Origin

    problem 657. Robot Return to Origin 题意: solution1: class Solution { public: bool judgeCircle(string ...

  8. [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  9. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

随机推荐

  1. AJ学IOS(02)UI之按钮操作 点击变换 移动 放大缩小 旋转

    不多说,先上图片看效果,AJ分享,必须精品 这个小程序主要实现点击方向键可以让图标上下左右动还有放大缩小以及旋转的功能,点击图片会显示另一张图片. 点击变换 其实用到了按钮的两个状态,再State C ...

  2. Android 程序代码进行代码混淆

    1.在Eclipse项目包下的project.properties文件中加入proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt ...

  3. 用Python介绍了企业资产情况的数据爬取、分析与展示。

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:张耀杰 PS:如有需要Python学习资料的小伙伴可以加点击下方链接自 ...

  4. Python程序设计 实验 1 熟悉 IDLE 和在线编程平台

    ------------恢复内容开始------------ 安徽工程大学 Python程序设计 实验报告 班级   物流191   姓名  姚彩琴  学号3190505129 成绩 日期     2 ...

  5. Linux下安装python3环境搭建

    Linux下python3环境搭建 Linux安装软件有哪些方式? rpm软件包 手动安装 拒绝此方式 需要手动解决依赖关系 yum自动化安装 自动处理依赖关系 非常好用 源代码编译安装,可自定义的功 ...

  6. jmeter事务控制器

    jmeter事务控制器常用于压力测试时如果一个功能包括多个请求时,需要测试这个功能的压力情况,则需要把多个请求放到一个事务控制器里面

  7. 分析PE

    PE文件是Windows平台下可执行文件标准格式,浓缩了微软工程师的设计精华,历经40年依旧保持着原有的设计.分析PE文件对于研究Windows操作系统有着重要的实践意义,对于逆向分析有着重要的指导作 ...

  8. 详细的JavaScript知识梳理和经典的一百个例题,让你掌握JavaScript

    这里先做一下JavaScript知识点的梳理,具体的可领取资料 JavaScript语法: js语法.png DOM操作: DOM操作.png 数据类型 面向对象 继承 闭包 插件 作用域 跨域 原型 ...

  9. Linux 日常操作

    Linux 日常操作 */--> Linux 日常操作 Table of Contents 1. 查看硬件信息 1.1. 服务器型号序列号 1.2. 主板型号 1.3. 查看BIOS信息 1.4 ...

  10. php正则匹配到字符串里面的a标签

    $cont = preg_replace('/<a href=\"(.*?)\".*?>(.*?)<\/a>/i','',$cont);