2018-10-04 12:53:06

问题描述:

问题求解:

首先本题给出了问题的规模,从Note中我们可以看到解码后的字符串长度甚至可以达到2^63的长度,这个长度已经远远超过整型数的范围,因此如果只是先解码后提取的话无疑是会超时的。

那么本题还有什么别的思路呢?正常来说如果碰到这种重复循环的问题,首先想到的自然是取余操作,比如hahahaha,K = 5,其实和 (K % 2 = 1)是一样的,因此本题的解法就很明确了,就是每次对K进行取余操作,如果说碰到了K == size的情况,也就是说当前的字符扩展后正好长度为K,那么直接返回这个字符即可(当然首先需要判断是否为字母)。

    public String decodeAtIndex(String S, int K) {
long size = 0;
int N = S.length();
for (int i = 0; i < N; i++) {
char c = S.charAt(i);
if (Character.isDigit(c)) size *= (c - '0');
else size++;
}
for(int i = N - 1; i >= 0; i--) {
char c = S.charAt(i);
K %= size;
if (K == 0 && Character.isLetter(c)) return Character.toString(c);
if (Character.isDigit(c)) size /= (c - '0');
else size--;
}
return null;
}

2019.04.27

    public String decodeAtIndex(String S, int K) {
long size = 0;
char[] chs = S.toCharArray();
for (int i = 0; i < chs.length; i++) {
if (chs[i] >= '0' && chs[i] <= '9') size *= (chs[i] - '0');
else size++;
}
for (int i = chs.length - 1; i >= 0; i--) {
if ((K == 0 || K == size) && !Character.isDigit(chs[i])) return String.valueOf(chs[i]);
if (chs[i] >= '0' && chs[i] <= '9') {
size /= (chs[i] - '0');
K %= size;
}
else size--;
}
return "";
}

  

获取解码字符串指定位置的数值 Decoded String at Index的更多相关文章

  1. PHP字符串指定位置插入字符串

    1.substr_replace(string,replacement,start,length);需插入时设置length为0即可 string 必需.规定要检查的字符串. replacement ...

  2. 【HANA系列】SAP HANA SQL获取某字符串的位置

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL获取某字 ...

  3. 【LeetCode】880. Decoded String at Index 解题报告(Python)

    [LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  4. [Swift]LeetCode880. 索引处的解码字符串 | Decoded String at Index

    An encoded string S is given.  To find and write the decodedstring to a tape, the encoded string is ...

  5. [LeetCode] 880. Decoded String at Index 在位置坐标处解码字符串

    An encoded string S is given.  To find and write the decoded string to a tape, the encoded string is ...

  6. 【PHP】在目标字符串指定位置插入字符串

    PHP如何在指定位置插入相关字符串,例子:123456789变为1_23_456789插入"_"到指定的位置! (可以用作换行或者其他处理) 插入示例,具体思路在代码中有注释: & ...

  7. Python 字符串指定位置替换字符

    指定位置替换字符 def replace_char(old_string, char, index): ''' 字符串按索引位置替换字符 ''' old_string = str(old_string ...

  8. C#获取json字符串指定的值

    Newtonsoft.Json在json和对象之间转化是一个非常强大的工具. 对象转化json字符串 Newtonsoft.Json.JsonConvert.SerializeObject() jso ...

  9. C# 在字符串指定位置之前插入新的字符串

    http://zhidao.baidu.com/link?url=XbU4souNCiDk9AbdYWMDj6VMO7AxlnIpcEnAy4JgfaZXxlpjVt2cEoL6GPO9B0WytMq ...

随机推荐

  1. 教你用Visual Studio Code做PHP开发 - 微软官方工具,IDE中的黑马

    转载于:http://bbs.wfun.com/thread-902655-1-1.html,仅供自己备忘 本文为我在智机网的原创  ] 关于Visual Studio Code,可能有的开发者很陌生 ...

  2. linux homebrew skill 技巧

    $ cat /usr/bin/gitdiffH0#!/bin/bashgit diff HEAD $(git ls-files| grep '\.py$\|\.py\.in$') | osflake8 ...

  3. 20155201 网络攻防技术 实验九 Web安全基础

    20155201 网络攻防技术 实验九 Web安全基础 一.实践内容 本实践的目标理解常用网络攻击技术的基本原理.Webgoat实践下相关实验. 二.报告内容: 1. 基础问题回答 1)SQL注入攻击 ...

  4. NOIP 2017 游(划水)记

    Day 0 上午,大概做了一套(大)信(水)心题. 让我想想我题目都是些什么鬼.. T1:大水题.什么sort一下就过了.据说lemon上用map不会被卡常(lemon上评测,程序跑得蜜汁快). T2 ...

  5. sqlserver无法在数据库上放置锁

    由于无法在数据库 ' ' 上放置锁,ALTER DATABASE 失败.请稍后再试.消息5069,级别16,状态1,第一行ALTER DATABASE 语句失败. 解决方法: 新建查询,通过下面SQL ...

  6. Flask学习【第7篇】:Flask中的wtforms使用

    简介flask中的wtforms WTForms是一个支持多个web框架的form组件,主要用于对用户请求数据进行验证. 安装 pip3 install wtforms 简单使用wtforms组件 用 ...

  7. android设备如何进入深度休眠还能继续使用定时器【求解】

    经过试验,andriod设备进入深度休眠的时候,定时器是不能使用.但是阻止设备进入深度休眠,可以获取一把锁,但是拿了锁之后,设备不能进入休眠,系统的功耗会增加.怎么能够在系统进入休眠,定时器还能正常工 ...

  8. git 如何revert指定范围内的commit并且只生成一个新的commit?

    答:一共分成两步 一. revert多个commit并生成多个新的commit git revert <old commit>^..<new commit> 二. 使用reba ...

  9. 3. Elements of a Test Plan

    https://jmeter.apache.org/usermanual/test_plan.html This section describes the different parts of a ...

  10. 【问题解决:时区】连接MySQL时错误The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone

    问题描述: MySQL升级到8.0.11之后连接数据库报错: Your login attempt was not successful, try again. Reason: Could not g ...