全部评论
bool isNum(char ch)
{
return ch >= '0' && ch <= '9';
}
string decode(string str)
{
int index1 = 0;
int index2 = 1;
int index3 = 0;
long long int len = 0;
vector<vector<int>> vecIndecx;
vector<int> vecCount;
while(index2 < str.size())
{
char ch = str[index2];
if (isNum(ch))
{
int count = ch - '0';
index3 = index2 + 1;
while(index3 < str.size() && isNum(str[index3]))
{
count *= 10;
count += (str[index3] - '0');
index3++;
}
vecCount.push_back(count);
vector<int> temp;
temp.push_back(index1);
temp.push_back(index2);
vecIndecx.push_back(temp);
len += count * (index2 - index1);
index1 = index3;
index2 = index1 + 1;
}
else
{
index2++;
}
}
string ret;
ret.resize(len+1);
int retIndex = 0;
vector<vector<int>>::iterator iter = vecIndecx.begin();
vector<int>::iterator countIter = vecCount.begin();
for(int i=0; i<vecIndecx.size(); i++)
{
for (int j = 0; j < *countIter; j++)
{
for(int index = (*iter)[0]; index < (*iter)[1]; index++)
{
ret[retIndex++] = str[index];
}
}
iter++;
countIter++;
}
ret[retIndex] = '\0';
return ret;
}
public class Main {
public static void main(String[] args) {
System.out.println(decode("a2bc10d1"));
}
public static String decode(String str){
if (str==null||str.length()==0) {
return null;
}
String[] strstr = str.split("\\d+");
String[] strnum = str.split("[a-zA-Z]+");
StringBuilder stringBuilder = new StringBuilder();
int i=0;//统计strstr
int j=0;//统计strnum
while (i<strstr.length&&j<strnum.length) {
if (strnum[j]!=null&&!strnum[j].equals("")) {
int num = Integer.parseInt(strnum[j]);
for (int k = 0; k < num; k++) {
stringBuilder.append(strstr[i]);
}
i++;
}
j++;
}
return new String(stringBuilder);
}
}
王海洋你好。。。。
package 去哪儿;
public class Chongfu {
public static void chongfu(String s)
{
String subletter = "";
String pre = "";
String output="";
boolean isletter = true;
boolean isfirst = true;
int times = 0;//记录每个字串需要重复的次数
int curnum = 0;//记录当前这个字符代表的数字值
for(int i = 0 ; i < s.length() ; i++)
{
if((s.charAt(i) >= 'a' && s.charAt(i)
<= 'z') ||(s.charAt(i) >= 'A' && s.charAt(i) <= 'Z'))
{
subletter += s.charAt(i);
isletter = true;
}else
{
curnum =s.charAt(i) - '0';
if(isletter == false)
{
times = times * 10 + curnum;
}
else
{
if(isfirst == false)
{
for(int j = 0 ; j < times ; j++)
{
output += pre;
}
}else
{
isfirst = false;
}
pre = subletter;
subletter = "";
times = curnum;
}
isletter = false;
}
}
for(int j = 0 ; j < times; j++)
{
output += pre;
}
System.out.print(output);
}
public static void main(String[] args)
{
String s="a10bc3d1";
chongfu(s);
}
}
![](https://uploadfiles.nowcoder.com/files/20240514/510894044_1715654316364/pingllunicon.png)
第一题是什么?
这是什么时候的?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define N 1024
typedef unsigned int __u32;
char *decode(const char *src)
{
const char *pnumb = src;
const char *pchar = src;
__u32 chartoint;
__u32 offset;
char *cdest;
char *res;
res = (char *)malloc(sizeof(char) * N);
if (!res)
{
/* code */
printf("allocate memory failed!.\n");
exit(1);
}
*res = '\0';
while(*pchar){
if (isalpha(*pchar))
{
/* code */
pnumb = pchar;
while(!isdigit(*pnumb))
{
pnumb++;
}
offset = pnumb - pchar;
chartoint = (int)(*pnumb - '0');
while(chartoint)
{
cdest = (char *)malloc((offset + 1)*sizeof(char));
if (!res)
{
/* code */
printf("allocate memory failed!.\n");
exit(1);
}
strncpy(cdest, pchar, offset);
*(cdest + offset) = '\0';
strcat(res, cdest);
free(cdest);
chartoint--;
}
}
pchar = pnumb + 1;
}
return res;
}
int main(int argc, char const *argv[])
{
/* code */
char *res;
char str[] = "a2bc3d1";
res = decode(str);
if (!res)
{
/* code */
printf("decrypt error!.\n");
goto end;
}
printf("After decrypt, the result is: %s\n", res);
free(res);
end:
return 0;
}
public class Solution {
public static String decode(String str) {
// 定义存储解码结果的变量
StringBuffer sb = new StringBuffer();
// 如果输入的字符串为空,直接返回
if (str == null || str.length() == 0) {
return sb.toString();
}
// 遍历字符串
int index = 0;
while (index < str.length()) {
//定义存储子串变量
String s = "";
//定义存储个数变量
String n = "";
// 获取子字符串
while (str.charAt(index) >= 'a' && str.charAt(index) <= 'z') {
s += str.charAt(index);
index++;
}
// 获取子字符串的个数(在这里进行判断str.charAt(index)时,有可能已经越界了,所以先判断是否索引越界)
while (index < str.length() && str.charAt(index) >= '0'
&& str.charAt(index) <= '9') {
n += str.charAt(index);
index++;
}
int number = Integer.parseInt(n);
for (int i = 0; i < number; i++) {
sb.append(s);
}
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(decode("abc123ds3d1kkd3"));
}
}
楼主还有剩下的一题吗?
这是什么时候的题啊?
public class StringDecode {
public static void main(String[] args) {
String a="a2bc3d1";
StringBuffer b=new StringBuffer();
StringBuffer c=new StringBuffer();
for(int i=0;i<a.length();i++){
if(!(a.charAt(i)>='0' && a.charAt(i)<='9')){
b.append(a.charAt(i));
}else{
int n=a.charAt(i)-'0';
c.append(add(b,n));
b.delete(0, b.length());
}
}
System.out.println(c);
}
public static StringBuffer add(StringBuffer s,int n){
StringBuffer s2=new StringBuffer();
if(n==1){
return s;
}else{
for(int i=0;i<n;i++){
s2.append(s);
}
return s2;
}
}
}
同问,这是什么时候的题目?2016校招?
/**
* @param str 解码前字符串
* @return 解码后字符串
* @author mnmlist
*/
public static String decode(String str)
{
if(str==null||str.length()==0)
return str;
StringBuilder sBuilder=new StringBuilder();
int strLen=str.length();
int i=0;
int start=0,end=0;
char ch;
String tempSubString=null,tempNumString=null;
while(i<strLen)
{
//找出子串
start=i;
ch=str.charAt(i);
while(i<strLen&&(ch>'9'||ch<'0'))
{
i++;
if(i<strLen)
ch=str.charAt(i);
}
end=i;
tempSubString=str.substring(start,end);
//找出每个子串出现的次数
start=end;
ch=str.charAt(i);
while(i<strLen&&(ch<='9'&&ch>='0'))
{
i++;
if(i<strLen)
ch=str.charAt(i);
}
end=i;
tempNumString=str.substring(start,end);
int count=Integer.valueOf(tempNumString);
//将子串重复解码
for(int j=0;j<count;j++)
sBuilder.append(tempSubString);
}
return sBuilder.toString();
}
private static void decode(String s) {
int len=s.length();
StringBuffer sb=new StringBuffer();
ArrayList<Integer> al=new
ArrayList<Integer>();
for(int i=0;i<len;){
if(isNum(s.charAt(i)))
{
int end=i+1;
int j=i+1;
while(j<len)
{
if(isNum(s.charAt(j)))
{
end=j+1;
j++;
}
else
break;
}
if(al.size()==0)
{
for(int
z=0;z<Integer.valueOf(s.substring(i,end))-1;z++)
{
sb.append(s.substring(0,i));
}
al.add(end);
}
else{
for(int
z=0;z<Integer.valueOf(s.substring(i,end))-1;z++)
{
sb.append(s.substring(al.get(0),i));
}
al.set(0, end);
}
i=end;
}
else
{
sb.append(s.charAt(i));
i++;
}
}
System.out.println(sb.toString());
}
private static boolean isNum(char c) {
if(c-48>=0 && c-48<=9)
{
return true;
}
return false;
}
function decode(str) {
var result = '';
str.replace(/([a-zA-Z]+)(\d+)/gi, function (a, m1, m2) {
for (var i = 0; i < parseInt(m2); i++) {
result += m1;
}
});
return result;
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
String str = "aa2bc1d10";
System.out.println(decode(str));
}
public static String decode(String str) {
StringBuffer sb = new StringBuffer();
StringBuffer tempBuffer = new StringBuffer();
int numStart = 0;
int numEnd = 0;
int times = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (!Character.isDigit(c)) {
tempBuffer.append(c);
} else {
if ((i - 1) >= 0 &&
!Character.isDigit(str.charAt(i - 1))) {
numStart = i;
}
if ((i + 1) < str.length()
&& !Character.isDigit(str.charAt(i +
1))) {
numEnd = i + 1;
}
if ((i + 1) == str.length()) {
numEnd = i + 1;
}
if (numEnd != 0) {
times = Integer.parseInt(str.substring(numStart, numEnd));
for (int j = 0; j < times; j++) {
sb.append(tempBuffer);
}
tempBuffer.delete(0, tempBuffer.length());
numEnd = 0;
}
}
}
return sb.toString();
}
}
这是去年招实习生的题目
import re
def decode(s):
res = ""
e = re.compile("[a-zA-Z]*")
r = re.compile("[0-9]*")
a1 = r.split(s)
a2 = e.split(s)
for i in range(len(a1)-1):
res += a1[i]*int(a2[i+1])
print res
相关推荐
点赞 评论 收藏
分享