Rotate String
Given a string and an offset, rotate string by offset. (rotate from left to right)
Given "abcdefg"
.
offset=0 => "abcdefg"
offset=1 => "gabcdef"
offset=2 => "fgabcde"
offset=3 => "efgabcd"
分析:
利用 (A^TB^T)^T = BA
public class Solution {
/**
* @param str: an array of char
* @param offset: an integer
* @return: nothing
*/
public void rotateString(char[] str, int offset) {
if (str == null || str.length <= ) return;
offset = offset % str.length;
if (offset == ) return; int p = str.length - - offset; swapFromIToJ(str, , p);
swapFromIToJ(str, p + , str.length - );
swapFromIToJ(str, , str.length - );
} public void swapFromIToJ(char[] str, int i, int j) {
while (i < j) {
char temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
}
}
Rotate String的更多相关文章
- Lintcode: Rotate String
Given a string and an offset, rotate string by offset. (rotate from left to right) Example Given &qu ...
- LeetCode算法题-Rotate String(Java实现)
这是悦乐书的第317次更新,第338篇原创 在开始今天的算法题前,说几句,今天是世界读书日,推荐两本书给大家,<终身成长>和<禅与摩托车维修艺术>,值得好好阅读和反复阅读. 0 ...
- leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String
344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...
- 8. Rotate String
8. Rotate String Description Given a string and an offset, rotate string by offset. (rotate from lef ...
- [Algorithm] 8. Rotate String
Description Given a string and an offset, rotate string by offset. (rotate from left to right) Examp ...
- 【Leetcode_easy】796. Rotate String
problem 796. Rotate String solution1: class Solution { public: bool rotateString(string A, string B) ...
- 796. Rotate String - LeetCode
Question 796. Rotate String Solution 题目大意:两个字符串匹配 思路:Brute Force Java实现: public boolean rotateString ...
- [Swift]LeetCode796. 旋转字符串 | Rotate String
We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...
- [LeetCode] Rotate String 旋转字符串
We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...
随机推荐
- reboot-css
dd, label { margin-bottom: .5rem; }abbr[title] { text-decoration: none; }abbr[title] { border-bottom ...
- zoj3888 找第二大
题目简化后最终要求的就是第二大的数.但是由于数据较大,不能直接求.可以先预处理,求出所有情况. #include<stdio.h> #include<string.h> #in ...
- 基于spring mvc的注解DEMO完整例子
弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件.本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mv ...
- request.getAttribute() 和 request.getParameter() 有何区别?
HttpServletRequest类既有getAttribute()方法,也由getParameter()方法,这两个方法有以下区别: (1)HttpServletRequest类有setAttri ...
- 积木(DP)问题
问题:Do you remember our children time? When we are children, we are interesting in almost everything ...
- BZOJ-3524 Couriers 可持久化线段树
可持久化线段树,其实就是类主席树了.. 3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 128 MB Submit: 1124 Sol ...
- Spring与jsp表达式的产生的问题
今天遇到一个问题就是Spring标签与jsp表达式的问题 直接上代码 <form:form commandName="book" action="/book_upd ...
- Windows Management Instrumentation WMI Security Technology Learning
目录 . 引言 . WMI(Windows Management Instrumentation)简介 . 基于WMI的攻击向量 . WMI编程示例 0. 引言 在进行服务器主机的入侵检测.安全攻防的 ...
- Codeforces 650B Image Preview
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- 初学structs2,表单验证简单补充
一.使用注解方式,跳过验证某个方法 由于在开发中,我们不需在请求每一个action类中的方法时都要走validate方法,那么我们可以在这些不需要验证的方法上加上@SkipValidation注解即可 ...