// 求2个很大数之和funcmaxNumSum(a string, b string)string{
size :=0
alen :=len(a)
blen :=len(b)if alen > blen {
size = alen
}else{
size = blen
}
result :=""for i :=0; i < size; i++{// 如果下标越界了,就设置为0
num1 :=0if i < alen {// a[i]类型为byte(ascii码),和整型运算会报错,转换成对应整型就-48
num1 =int(a[i])-48}
num2 :=0if i < blen {
num2 =int(b[i])-48}// 字符和整型相加会报错,string(num1+num2)会变成空字符,用Sprintf或strconv.Itoa
result += fmt.Sprintf("%d",(num1 + num2))}return result
}
// 字符串中第一个不重复的字符funcstrFristNotDup(str string)string{
m :=make(map[rune]int)
keys :=make([]rune,0)for_, s :=range str {if_, ok := m[s];!ok {
keys =append(keys, s)}
m[s]+=1}// map遍历是无序的,因此使用切片,当然也可以再遍历下字符strfor_, s :=range keys {if m[s]==1{returnstring(s)}}return""}
Nodejs
// 求2个很大数之和functionmaxNumSum(a, b){const alen = a.length;const blen = b.length;const len = alen > blen ? alen : blen;let result ="";for(let i=0; i<len; i++){// a[i]为对应字符,转换成整型才能相加,不然变成字符累加const num1 = i < alen ?parseInt(a[i],10):0;const num2 = i < blen ?parseInt(b[i],10):0;// 字符和整型可以相加,自动转换成字符
result +=(num1 + num2)}return result;}
// 字符串中第1个不重复字符functionstrFristNotDup(str){const m ={};for(let i =0; i < str.length; i++){if(!m[str[i]]&& str.indexOf(str[i], i +1)===-1){return str[i];}
m[str[i]]=true;}}
Python
# 求2个很大数之和defmaxNumSum(a, b):
alen =len(a)
blen =len(b)
size = alen if alen > blen else blen
result =""for i inrange(size):# a[i]为对应字符,转换成整型才能相加,不然变成字符累加
num1 =int(a[i])if i < alen else0
num2 =int(b[i])if i < blen else0# 字符不能和整型直接累加,需要str转换成字符
result +=str(num1 + num2)return result
# 字符串中第1个不重复的字符 # 方法1:defstrFirstNotDup(str):
m = Counter(str)# []返回的是数组,()返回的是迭代器需要使用next从中取1个returnnext((k for k, v in m.items()if v ==1),'')# 方法2:defstrFirstNotDup(str):
m ={}for s instr:
m[s]= m.get(s,0)+1returnnext((s for s instrif m[s]==1),'')
C#
// 求2个很大数之和publicstaticstringmaxNumSum(string a,string b){int alen = a.Length;int blen = b.Length;int len = alen >blen ? alen : blen;string result ="";for(int i=0; i<len; i++){// Console.WriteLine(a[i].GetType());// a[i]为char类型(ascii码),转换成对应整型-48,char可以直接和整型运算int num1 = i < alen ? a[i]-48:0;int num2 = i < blen ? b[i]-48:0;// 字符和整型可以相加,自动转换成字符
result +=(num1 + num2);}return result;}
// 字符串中第1个不重复的字符 // 方法1:publicstaticcharstrFirstNotDup(string str){foreach(char s in str){if(str.IndexOf(s)== str.LastIndexOf(s)){return s;}}return' ';}// 方法2:publicstaticcharstrFirstNotDup(string str){Dictionary<char,int> m =newDictionary<char,int>();foreach(char s in str){// 不能直接m[s]++
m[s]= m.ContainsKey(s)? m[s]+1:1;}foreach(var o in m){if(o.Value ==1){return o.Key;}}// 中间必须包含空格,否则是无效字符return' ';}