LintCode:旋转字符串
2016-05-12 00:28
477 查看
LintCode:旋转字符串
Python
class Solution: # @param s: a list of char # @param offset: an integer # @return: nothing def rotateString(self, s, offset): # write you code here if len(s) == 0 : return s m = len(s) offset %= m n = m - offset tmp = s[n:] while n > 0: s[n + offset-1] = s[n-1] n -= 1 for i in range(offset): s[i] = tmp[i]
Java
public class Solution { /** * @param str: an array of char * @param offset: an integer * @return: nothing */ public void rotateString(char[] str, int offset) { // write your code here if (str.length == 0){ return; } int m = str.length; offset %= m; int n = m - offset; char[] tmp = new char[offset]; int k = 0; for(int i=n; i<m; i++){ tmp[k++] = str[i]; } while(n>0){ str[n+offset-1] = str[n-1]; n --; } for(int i=0; i<offset; i++){ str[i] = tmp[i]; } } }
相关文章推荐
- java基础第六天_接口与适配器模式、多态、内部类
- bzoj2738: 矩阵乘法
- 作业-5
- HDU 1228 - A + B
- pat1009. Product of Polynomials (25)
- ASP.NET网站发布到IIS后验证码不能显示与IIS模式
- 天嵌TQ_E9卡片电脑移植飞思卡尔yocto L4.1.15_1.0.0_ga 第二篇 新版uboot移植
- 对象和数组的相互转化
- ssh中jndi的配置(struts2.4.1+spring4.1.5+4.3.10+tomcat7)
- hihoCoder #1127:二分图最小点覆盖和最大独立集
- 一个OpenCV实现的人脸检测的源码
- java基础第六天_接口与适配器模式、多态、内部类
- 随机采样和随机模拟:吉布斯采样Gibbs Sampling
- netfilter/iptables全攻略
- 关于复杂指针数组,函数以及函数指针,数组思考
- Apache2.4+Tomcat8负载均衡
- 随机采样和随机模拟:吉布斯采样Gibbs Sampling
- iptables防火墙详解
- 函数声明中的强制类型转换
- 关于ng-disabled 不起作用的解决办法