当前位置:网站首页>力扣每日一练之双指针1Day8
力扣每日一练之双指针1Day8
2022-06-21 17:44:00 【InfoQ】
力扣每日一练之双指针1Day8
Leetcode 977.有序数组的平方
示例 1:
输入:nums = [-4,-1,0,3,10]
输出:[0,1,9,16,100]
解释:平方后,数组变为 [16,1,0,9,100]
排序后,数组变为 [0,1,9,16,100]
示例 2:
输入:nums = [-7,-3,2,3,11]
输出:[4,9,9,49,121]
🦺解题思路
🥼源代码
class Solution {
public int[] sortedSquares(int[] nums) {
int[] ans=new int[nums.length];
for(int i=0;i<nums.length;i++){
ans[i]=nums[i]*nums[i];
}
Arrays.sort(ans);
return ans;
}
}
Leetcode189.轮转数组
示例 1:
输入: nums = [1,2,3,4,5,6,7], k = 3
输出: [5,6,7,1,2,3,4]
解释:
向右轮转 1 步: [7,1,2,3,4,5,6]
向右轮转 2 步: [6,7,1,2,3,4,5]
向右轮转 3 步: [5,6,7,1,2,3,4]
示例 2:
输入:nums = [-1,-100,3,99], k = 2
输出:[3,99,-1,-100]
解释:
向右轮转 1 步: [99,-1,-100,3]
向右轮转 2 步: [3,99,-1,-100]
🩳解题思路
源代码
class Solution {
public void rotate(int[] nums, int k) {
int len=nums.length;
int[] ans=new int[len];
for(int i=0;i<len;i++){
int index=i+k;
while(index>=len){
index-=len;
}
ans[index]=nums[i];
}
for(int i=0;i<len;i++){
nums[i]=ans[i];
}
}
}
总结
觉得文章写的不错的亲亲,点赞评论关注走一波,爱你们哦
边栏推荐
猜你喜欢
随机推荐
Book at the end of the article | new work by teacher Li Hang! A new upgrade of the classic machine learning work statistical learning methods
How to apply for SSL certificate using IP
JDBC 笔记
数据库主键一定要自增吗?有哪些场景不建议自增?
In the new season, China Super League and Guoan are moving forward amid thorns
ACL 2022 | 基于自监督图对齐的多语言知识图谱推理
Does the school of Finance and business belong to a securities company? Is it safe to open an account?
删除指定的screen
动态加载资源之AssetDatabase
Servlet learning (II)
新赛季的中超和国安,荆棘中前行
《Go题库·9》同一个协程里面,对无缓冲channel同时发送和接收数据有什么问题
缓存设计问题
文件上传漏洞靶场分析 UPLOAD_LABS
Listener and filter (monitor and interceptor) in Servlet
Wwdc22 multimedia feature summary
6月22日直播 | 华南理工詹志辉: 面向昂贵优化的进化计算
挖财商学院属于证券公司吗?开户安全吗?
图像分类、AI 与全自动性能测试
Three.js 3d粒子动画js特效代码








![[HCTF 2018]WarmUp](/img/b0/6baee8ac76b56378230c2218f15734.png)
