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 ...
随机推荐
- 当Spring Cloud Alibaba Sentinel碰上Spring Cloud Sleuth会擦出怎样的火花
前言 今年主要会做一个比较完整的微服务项目开源出来.目前已经开始了,刚兴趣的先Star一个吧. 项目:https://github.com/yinjihuan/kitty-cloud 基础框架:htt ...
- Eclipse Hadoop源码阅读环境
一.解压hadoop src包到workspace目录.为加快下载jar包的速度,在eclipse的maven设置里将配置文件的路径设置正确,然后配置maven的settings.xml: <m ...
- OpenAL试水
参考了https://wysaid.org/976.html. 这个博客给了一个EGE+OpenAL的demo和源代码.一开始没注意,博主也没有给EGE相关信息.会找不到EGE相关头文件,建议如果要二 ...
- 20200107——记spring的DataSource
spring项目中总要跟数据库打交道,其中怎么连接数据库的方法都有很多,大概分为3类: 1) 通过JNDI获取应用服务器(如JBOSS, Tomcat) 的数据源 2) Spring容器中直接配置数 ...
- 前端基础进阶(七)-前端工程师最容易出错的问题-this关键字
我们在学习JavaScript的时候,因为对一些概念不是很清楚,但是又会通过一些简洁的方式把它给记下来,那么这样自己记下来的概念和真正的概念产生了很强的偏差. 当然,还有一些以为这个是对的,还会把它发 ...
- WFS: postgresql(postgis)和shp文件查询效率对比
对GeoServer上的WFS的各种数据源查询效率感兴趣,做个测试.本次测试了Postgresql.geopackage.shp文件三种数据源的查询效率,无论是本机还是服务器环境,pg存储查询效率都比 ...
- 使用cat命令清空文件
比如要清空 /www/aaa.txt cat /dev/null > /www/aaa.txt; 即可.
- 2019-2020-1 20199326《Linux内核原理与分析》第一周作业
开篇概述 我利用假期的时间自学了实验楼上的Linux基础入门前八个实验的课程,学习过程中遇到了一些小问题.但经过查资料等方式最终还是解决了问题.现将学到的一些知识点总结下来.方便日后复习查看. 1.零 ...
- Mac文件上传下载到服务器指定命令
下载文件夹 scp -r 远程登录服务器用户名@远程服务器ip地址:/下载文件夹的目录 『空格』 本地目录 下载文件 scp 远程登录服务器用户名@远程服务器ip地址:/下载文件的 ...
- tcpdump常用抓包命令
主要语法 过滤主机/IP: tcpdump -i eth1 host 172.16.7.206 抓取所有经过网卡1,目的IP为172.16.7.206的网络数据 过滤端口: tcpdump -i ...