题目1
输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
思路:
求所有可能在第一个位置的字符,即把第一个字符和后面的依次交换
固定第一个字符,求后面所有字符的排列。后面所有的字符又可看成第一个字符跟后面所有的字符的排列。典型的递归思路
增加一个迭代
# -*- coding:utf-8 -*-
import itertools
class Solution:
def Permutation(self, ss):
# write code here
result=[]
if not ss:
return []
else:
res=itertools.permutations(ss)
for i in res:
if "".join(i) not in result:
result.append("".join(i))
return result
一样的方法
# -*- coding:utf-8 -*-
class Solution:
def Permutation(self, ss):
# write code here
if not ss:
return []
if len(ss) == 1:
return [ss]
ret = []
#遍历字符串,固定第一个元素,然后递归求解
for i in range(len(ss)):
for j in self.Permutation(ss[:i]+ss[i+1:]): 注意这里是s[i:j] 表示获取a[i]到a[j-1]
ret.append(ss[i]+j)
#通过set进行去重,sorted进行重新排序
return sorted(list(set(ret)))
题目2
在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写)
思路: 字典
# -*- coding:utf-8 -*-字典方法,注意enumerate 用法
class Solution:
def FirstNotRepeatingChar(self, s):
if len(s)<1:
return -1
dict={}
for c in s:
if c not in dict:
dict[c]=1
else:
dict[c]+=1
for i ,c in enumerate(s):
if dict[c]==1:
return i
return -1
# -*- coding:utf-8 -*- 直接用count函数
class Solution:
def FirstNotRepeatingChar(self, s):
if len(s)<=0 or len(s)>10000:
return -1
for i in s:
if s.count(i)==1:
return s.index(i)
break
最差的方法 暴力循环
def IsRepeatingChar(self, i,s):
for j in range(len(s)):
if j==i:
continue
elif s[j] == s[i]:
return 0
return 1
def FirstNotRepeatingChar(self, s):
for i in range(len(s)):
if self.IsRepeatingChar(i,s):
return i
return -1
题目3
汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它!
思路:把字符串转为列表,把循环左移的前几位加到列表后面
class Solution:
def LeftRotateString(self, s, n):
# write code here
ans = []
if s :
lists=list(s)#字符串转换为列表
#ind = n%len(lists)
ans = lists[n:]
ans.extend(lists[0:n])
return ''.join(ans)
题目4–把字符串转换为整数
将一个字符串转换成一个整数(实现Integer.valueOf(string)的功能,但是string不符合数字要求时返回0),要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0。
思路:
# -*- coding:utf-8 -*-
class Solution:
def StrToInt(self, s):
numlist=['0','1','2','3','4','5','6','7','8','9','+','-']
sum=0
label=1#正负数标记
if s=='': #如果为空,则返回0
return 0
for string in s: #对于字符串里面的字符
if string in numlist:#如果是合法字符
if string=='+':
label=1
continue
if string=='-':
label=-1
continue
else:
sum=sum*10+numlist.index(string) #计算总数
if string not in numlist:#非合法字符
sum=0
break#跳出循环
return sum*label
题目5-表示数值的字符串
请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100",“5e2”,"-123",“3.1416"和”-1E-16"都表示数值。 但是"12e",“1a3.14”,“1.2.3”,"±5"和"12e+4.3"都不是。
思路,比较琐碎,需要把一些特殊情况给排除
# -*- coding:utf-8 -*-
class Solution:
# s字符串
def isNumeric(self, s):
# write code here
if s is None or len(s) == 0:
return False
# 是否有e
hasE = False
# 是否有小数
isDecimal = False
# 是否有+-符号
hasSign = False
for i in range(len(s)):
# 如果有e,只能有一个e且不能是最后一个
if s[i] == "e" or s[i] == "E":
if hasE or i == len(s) - 1:
return False
hasE = True
# 小数点不能重复出现或和e共线
elif s[i] == ".":
if hasE or isDecimal:
return False
isDecimal = True
elif s[i] == "-" or s[i] == "+":
# 重复出现符号时,必须跟在e后面
if hasSign and s[i - 1] != "e" and s[i - 1] != "E":
return False
# 重复出现符号时,必须跟在e后面
if not hasSign and i > 0 and s[i - 1] != "e" and s[i - 1] != "E":
return False
hasSign = True
elif s[i] < "0" or s[i] > "9":
return False
return True
# write code here
题目6-字符流中第一个不重复的数字
请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。如果当前字符流没有存在出现一次的字符,返回#字符。
思路: 应用字典
# -*- coding:utf-8 -*-
class Solution:
# 返回对应char
def __init__(self):
self.c = ""
self.d = {}
def FirstAppearingOnce(self): #检测第一次只出现一次的字符
for s in self.c:
if self.c.count(s) == 1:
return s
return "#"
def Insert(self, char): # 字符流中输入数据
self.c = self.c + (char)