lintcode-easy-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"
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 == null || str.length == 0)
            return;

        int size = str.length;

        offset %= size;
        if(offset == 0)
            return;

        for(int i = 0; i < offset; i++)
            rotate(str);
        return;
    }

    public void rotate(char[] str){
        char temp = str[str.length - 1];

        for(int i = str.length - 1; i > 0; i--)
            str[i] = str[i - 1];

        str[0] = temp;
        return;
    }

}
时间: 2024-10-13 11:18:28

lintcode-easy-Rotate String的相关文章

LeetCode算法题-Rotate String(Java实现)

这是悦乐书的第317次更新,第338篇原创 在开始今天的算法题前,说几句,今天是世界读书日,推荐两本书给大家,<终身成长>和<禅与摩托车维修艺术>,值得好好阅读和反复阅读. 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第186题(顺位题号是796).给定两个字符串A和B,在A上进行移位操作,规则是将A最左边的字符移动到最右边去.例如,如果A ='abcde',那么在A上移位一次后,它将是'bcdea'.当且仅当A在A上移位一定次数后可以变为B时返回True.

Rotate String

Given a string and an offset, rotate string by offset. (rotate from left to right) Example Given "abcdefg". offset=0 => "abcdefg" offset=1 => "gabcdef" offset=2 => "fgabcde" offset=3 => "efgabcd&quo

LintCode: Rotate String

C++, time: O(n) space:O(n) 1 class Solution { 2 public: 3 /** 4 * @param str: a string 5 * @param offset: an integer 6 * @return: nothing 7 */ 8 void rotateString(string &str, int offset) { 9 // wirte your code here 10 // empty string->return 11 if

[Lintcode easy]Longest Words

Longest Words Given a dictionary, find all of the longest words in the dictionary. Example Given { "dog", "google", "facebook", "internationalization", "blabla" } the longest words are(is) ["internati

LeetCode 796. Rotate String

原题链接在这里:https://leetcode.com/problems/rotate-string/ 题目: We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = 'abcde', then it will be 'bcdea' aft

[lintcode easy]Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Example "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note Have you consider that the s

[lintcode easy]Valid Parentheses

Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. Example The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]

[lintcode easy]Binary Tree Paths

Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Example Given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: [ "1->2->5", "1->3" ] /** * Definition of TreeNode: * public class TreeNode {

[lintcode easy]Add Binary

Add Binary Given two binary strings, return their sum (also a binary string).   Example a = 11 b = 1 Return 100 solution: 比较两个string的长度,将长读较小的string左边用0补齐. 设置进位标志flag.循环结束后如果进位标识大于0,则返回进位加上其他string:否则返回新string: char 0对应ASCII码表中30 1对应31....减去0所对应的值以后就

[lintcode easy]Compare Strings

Compare Strings Compare two strings A and B, determine whether A contains all of the characters in B. The characters in string A and B are all Upper Case letters. Example For A = "ABCD", B = "ACD", return true. For A = "ABCD"