字符串

Posted by Liao on 2019-11-27

hdu 1238

找出字符串中,连续子串(逆子串)相等的长度

简单字符串处理

首先要读懂题意,题目给出一串手机号,按格式从大到小输出手机号中含有的数字,存入arr数组,再输出每个手机号的数字在arr数组中的下标。

处理的几个巧妙之处:

  • 首先把vis数组初始化为1(默认手机号的每个数字都已经访问过),然后通过循环查询,出现手机号码中的数字则记录,没有出现则通过判断语句筛掉了,实现找出没有重复的数字。
  • 数字间隔“,”的输出巧妙,输出的第一个肯定不是”,”,排除掉之后间隔输出”,”和数组中的数字。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int arr[100]; //存储手机号中出现的数字
int vis[100]; //存放是否访问过
int a[100]; //存储手机号码的每个数字在arr中的下标
bool cmp(int a,int b)
{
return a>b;
}

int main()
{
string s;
int k=0;
cin >> s;
int len = s.size();

//处理arr[]
for(int i=0;i<len;i++)
vis[s[i]-'0'] = 1;

for(int i=0;i<len;i++)
{
if(vis[i])
arr[k++] = i; //巧妙1
}

cout << "int[] arr = new int[]{";
sort(arr,arr+k,cmp);
for(int i=0;i<k;i++) //输出不重复的数
{
if(i != 0 )
cout << ",";
cout << arr[i];
}
cout << "};" <<endl;


//index[]
cout << "int[] index = new int[]{";
for(int i=0;i<len;i++)
{
for(int j=0;j<k;j++)
{
if((s[i]- '0') == arr[j])
{
a[i] = j;
}
}
}

for(int i=0;i<len;i++)
{
if(i!=0) //巧妙2
cout << ",";
cout << a[i];
}
cout << "};";
return 0;

}



LeetCode 拼写单词

给字符串数组words和字符串chars,问通过chars中的字母,能拼写words中每个单词的最大长度

输入:words = [“cat”,”bt”,”hat”,”tree”], chars = “atach”
输出:6
解释:
可以形成字符串 “cat” 和 “hat”,所以答案是 3 + 3 = 6。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int countCharacters(vector<string>& words, string chars) {
char ch[26] = {0};
int res = 0;
for(char c : chars)
ch[c-'a']++; //统计chars中各个字母的个数
//通过这种方式的双循环,能查找到字符串数组中每个字符串的每一个字符。
for(string word : words){
char wd[26] = {0}; //处理每个字符串都要更新
for(char c : word)
wd[c-'a']++; //统计每个字符串中,每个字母的个数
res += word.size();
for(char c:word){
if(wd[c-'a'] > ch[c-'a']){  //字符串数组有,但字符串没有字符匹配
res -= word.size();
break;
}
}
}
return res;
}
LeetCode 二进制求和
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
string addBinary(string a, string b) {
int lena = a.size();
int lenb = b.size();
//若两数长度不相等,要补0
while(lena > lenb)
{
b = '0' + b;
++lenb;
}
while(lena < lenb)
{
a = '0' + a;
++lena;
}
for(int i = a.size() - 1; i > 0; --i)
{
a[i] = a[i] -'0' + b[i];
if(a[i] >= '2')
{
a[i] = (a[i]-'0') % 2 +'0'; //进位
a[i-1] = a[i-1] + 1; //前一位数目加1
}
}

a[0] = a[0] - '0' + b[0];
if(a[0] >= '2')
{
a[0] = (a[0]-'0') %2 +'0';
a = '1' + a; //把1拼接到a串前
}
return a;
}

LeetCode 翻转字符串的每个单词

实战删去字符串开头、结尾的空格,中间不能多于一个空格。

思路还是使用栈,如果把处理空格放在拼接栈的字符串会有问题…于是就直接在放栈之前就处理了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
string reverseWords(string s) {
stack<string>st;
string str = "";
string ans = "";
int left = 0, right = s.size() - 1;
int blank = 0;
//去掉开头和结尾的空格
while(left<=right && s[left] == ' ')
left++;
while(left<=right && s[right] == ' ')
right--;

//把每一个单词放到栈中,并处理其中不和规则的空格(空格多于1)
for(int i = left; i <= right; i++){
if(s[i] != ' '){
blank = 0;
str += s[i];
}
else{
blank++;
if(blank > 1){
continue;
}
else{
st.push(str);
st.push(" "); //每个单词后都有一个空格
str = "";
}
}
}
st.push(str); //最后一个单词后面没有空格,没有默认把单词进栈,因此要记得把最后一个单词放进栈

//把栈中处理好的单词通过字符串拼接起来
while(!st.empty()){
ans += st.top();
st.pop();
}
return ans;
}