(LeetCode)旋转数组
原体描写叙述例如以下:
Rotate an array of n elements to the right by k steps.
For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is
rotated to [5,6,7,1,2,3,4].
Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
Could you do it in-place with O(1) extra space?
Related problem: Reverse Words in a String II
我的解答例如以下:
public class Solution {
public void rotate(int[] nums, int k) {
if(nums==null||k==0)
{
return;
}
k = k%nums.length;
reverse(nums,0,nums.length-1);
reverse(nums,0,k-1);
reverse(nums,k,nums.length-1);
}
public void reverse(int[] a ,int start, int end)
{
while(start<end)
{
int temp = a[start];
a[start] = a[end];
a[end] = temp;
start++;
end--;
}
}
}
(LeetCode)旋转数组的更多相关文章
- leetcode旋转数组查找 二分查找的变形
http://blog.csdn.net/pickless/article/details/9191075 Suppose a sorted array is rotated at some pivo ...
- [LeetCode] Rotate Array 旋转数组
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- LeetCode 189. Rotate Array (旋转数组)
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- 【Leetcode】【简单】【189. 旋转数组】【JavaScript】
189. 旋转数组 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6,7] 和 k = 3输出: [5,6,7,1,2,3,4]解释 ...
- LeetCode初级算法--数组02:旋转数组
LeetCode初级算法--数组02:旋转数组 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/ ...
- [LeetCode] 189. Rotate Array 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- 前端与算法 leetcode 189. 旋转数组
目录 # 前端与算法 leetcode 189. 旋转数组 题目描述 概要 提示 解析 算法 # 前端与算法 leetcode 189. 旋转数组 题目描述 189. 旋转数组 概要 把他当做一到简单 ...
- LeetCode 189. 旋转数组(Rotate Array)
189. 旋转数组 LeetCode189. Rotate Array 题目描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6, ...
- 【LeetCode每天一题】Search in Rotated Sorted Array(在旋转数组中搜索)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., ...
随机推荐
- Spark Streaming updateStateByKey案例实战和内幕源码解密
本节课程主要分二个部分: 一.Spark Streaming updateStateByKey案例实战二.Spark Streaming updateStateByKey源码解密 第一部分: upda ...
- 解决LayoutItem lable 太长的问题
<Style TargetType="dxlc:LayoutItem"> <Setter Property="AddColonToLabel" ...
- ComboxEdit 重要属性
DisplayMember="ComboItemName" ValueMember="ComboItemCode"IsTextEditable="Tr ...
- Mac下进行基于java服务端语言的微信公众号本地js-sdk调试的大致方法
开发微信公众号应用调用js-sdk,需要先在微信公众号后台配置可信域名,之后从微信的入口地址重定向到改域名下的路径后便会返回code,之后可以拿到一系列需要的参数等等.那么本地开发,如果使用的是PHP ...
- [Node.js]24. Level 5: Express, Express routes
Create an express route that responds to GET requests at the URL /tweets that responds with the file ...
- sed 常用的命令
n: 读取一行,执行n,把当前行打印到标准输出,再读取一行,覆盖当前行,然后对模式空间执行一组模式/行为.N:读取一行,执行N,再读取一行,现在模式空间有两行内容,执行一组模式/行为.如下:[root ...
- 织梦DeDeCms会员登录或退出跳转到首页的修改方法
会员在主页登陆后,默认会跳转到会员中心,如果我们想登陆后,跳转到网站主页,那么就请参考下面的方法实现织梦DeDeCms会员登录或退出跳转到首页. 1.在根目录的member目录中找到index_do. ...
- swift第一章
swift中添加的类型:Tuple(元组类型),能够让你创建或者传递一组数据. 比方作为函数的返回值时.你能够用一个元组能够返回多个值. swift中还添加了可选(Optional)类型,用于处理值缺 ...
- 【oracle】dblink创建
目的:oracle中跨数据库查询 两台数据库服务器db_A(本地)和db_B(远程192.168.1.100),db_A下用户user_a 需要访问到db_B下user_b的数据 解决:查询得知使用d ...
- centos下两种方法安装git
来自:http://blog.slogra.com/post-176.html 今天下个包需要使用git,网上找了下看到大多数只有编译安装,并且编译安装还有错,不知道他们也没有实验过,这里我来给大家介 ...