IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

字母移位

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

字母移位

有一个由小写字母组成的字符串S,和一个整数数组shifts
我们将字母表中的下一个字母称为原字母的 移位(由于字母表是环绕的,z将会变成a)。
例如,shift('a') = 'b'shift('t') = 'u',以及shift('z') = 'a'
对于每个shifts[i] = x, 我们会将S中的前i+1个字母移位x次。
返回将所有这些移位都应用到S后最终得到的字符串。

示例

输入:S = "abc", shifts = [3,5,9]
输出:"rpl"
解释: 
我们以 "abc" 开始。
将 S 中的第 1 个字母移位 3 次后,我们得到 "dbc"。
再将 S 中的前 2 个字母移位 5 次后,我们得到 "igc"。
最后将 S 中的这 3 个字母移位 9 次后,我们得到答案 "rpl"。

题解

/**
 * @param {string} S
 * @param {number[]} shifts
 * @return {string}
 */
var shiftingLetters = function(S, shifts) {
    var sub = 0;
    var base = "a".charCodeAt(0);
    var target = "";
    for(let i=S.length-1; i>=0; --i){
        sub = (sub + shifts[i]);
        let charPath = (S[i].charCodeAt(0) - base + sub) % 26;
        target = String.fromCharCode(base + charPath) + target;

    }
    return target;
};

思路

这是字符的循环移位问题,根据shifts数组就可以计算出每个字符的移位数量,第i个字母共移位shifts[i] + shifts[i+1] + ... + shifts[shifts.length - 1]次,虽然可以直接是用数组计算出每个字符应该位移的长度,但是如果直接从数组尾部向前遍历,那么直接记录之前的累加值然后作循环移位即可,首先定义一个累加值计数和subJs中没有char基本数据类型,所以对于字符操作需要通过Ascii码计算,定义base作为字符aAscii码值,target为将要返回的目标字符串,然后从后向前遍历数组,累加sub计数器,计算当前字符加入计数器的长度减掉a字符来计算Ascii码值,然后取余获得对于a字符的偏移长度,然后将Ascii码值转换为字符拼接到目标字符串即可。

每日一题

https://github.com/WindrunnerMax/EveryDay

题源

https://leetcode-cn.com/problems/shifting-letters

文章永久链接:https://tech.souyunku.com/?p=39645


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(63) 打赏



未经允许不得转载:搜云库技术团队 » 字母移位

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367