LeetCode--Squares of a Sorted Array && Robot Return to Origin (Easy)
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)的更多相关文章
- 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 ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- LeetCode: Search in Rotated Sorted Array II 解题报告
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...
- LeetCode977.Squares of a Sorted Array
题目 977. Squares of a Sorted Array Given an array of integers A sorted in non-decreasing order, retur ...
- 【Leetcode_easy】977. Squares of a Sorted Array
problem 977. Squares of a Sorted Array solution: class Solution { public: vector<int> sortedSq ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
- 【Leetcode_easy】657. Robot Return to Origin
problem 657. Robot Return to Origin 题意: solution1: class Solution { public: bool judgeCircle(string ...
- [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
随机推荐
- 选择IT行业的自我心得,希望能帮助到各位!(三)失败篇
可能很多小伙伴会说人人创业岂不是人人都能成功,岂不是人人都能成功,是不是每个人都能开上保时捷,法拉利泡着美女,很多人也会说你看他看她多轻松,做个IT一样就赚钱赚钱了. 那么又有多少人能理解到你的心酸了 ...
- jQuery+ajax实现滚动到页面底部自动加载图文列表效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- vue中的ref属性
1.什么是ref? ref是用于快速定位到dom结构,vue中一般不去操作dom结构,他是数据驱动的.jQuery会疯狂操作DOM {{msg}} mounted(){ let h = this.$r ...
- C语言 贪吃蛇
贪吃蛇(单人版): 本人先来介绍一个函数 -- bioskey函数: int bioskey (int cmd) 参数 (cmd) 基本功能 0 返回下一个从键盘键入的值(若不键入任何值,则将等下一个 ...
- APT32入侵我国,试图窃取COVID-19相关情报
新闻一篇: 一直以来,APT32都以东南亚为攻击目标,并且是近几年来针对中国大陆进行攻击活动最活跃的APT攻击组织,没有之一.此次再将目标对准中国,与新冠疫情离不开关系. 4月22日,Fireye发布 ...
- 【已解决】error setting certificate verify locations报错
目录 1.问题描述 2.问题分析 3.解决方法 1.问题描述 在公司的电脑上从Github上clone项目的时候git黑窗口报错"error setting certificate veri ...
- BypassUAC
BypassUAC 本篇主要介绍如何以ICMLuaUtil方式BypassUAC,主要内容如下: 过掉UAC提示框的方法总结 UACME项目 什么类型的COM interface可以利用? 如何快速找 ...
- 巧用Grafana和Arthas自动抓取K8S中异常Java进程的线程堆栈
前言 近期发现业务高峰期时刻会出现CPU繁忙导致的timeout异常,通过监控来看是因为Node上面的一些Pod突发抢占了大量CPU导致的. 问: 没有限制CPU吗?是不是限制的CPU使用值就可以解决 ...
- Tomcat系列教材 (一)- 教程
Tomcat系列教材 (一)- 教程 Tomcat是常见的免费的web服务器. Tomcat 这个名字的来历,Tomcat是一种野外的猫科动物,不依赖人类,独立生活. Tomcat的作者,取这个名字的 ...
- Libra白皮书解读
文章目录 Libra简介 Libra区块链 Libra货币和存储 Libra协会 Libra简介 Libra是facebook发起的一个区块链项目,其使命是建立一套简单的.无国界的货币和为数十亿人服务 ...