1.【题目描述】 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学。今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决但是,如果向量中包含负数,是否应该包含某个负数,并期望旁边的正数会弥补它呢例如:{6 - 3, - 2,7 - , - .15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)给一个数组,返回它的最大连续子序列的和,你会不会被他忽悠住?(子向量的长度至少是1)
##动态规划,两个标签,一个记录最大值,一个记录加和之后的值
# -*- coding:utf-8 -*-
class Solution:
def FindGreatestSumOfSubArray(self, array):
if not array:
return 0
res = array[0]
for i in range(len(array)):
for j in range(i,len(array)):
su = sum(array[i:j+1])
if su > res:
res = su
return res
2.【题目】描述求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1, -10,11,12,13-因此共出现6次,但是对于后面问题他就没辙了.ACMer希望你们帮帮他,并把问题更加普遍化,可以很快的求出任意非负整数区间中1出现的次数(从1到ñ中1出现的次数)。
,余数0,该位上1的个数受高位影响,余数为1,受高位和低位影响,余数为其他,则受高位和低位影响,但是低位1的个数已满,即若是122,十位的1的个数为10 + 10(,高位影响的110〜119,10个;低位影响的10〜19)
# -*- coding:utf-8 -*-
class Solution:
def NumberOf1Between1AndN_Solution(self, n):
a = 1 ##当前位数
b = n
k = 0 ##记录1的次数
while n // a:
c = b%10 ##尾部数字
b = b // 10 ##后移一位
if c == 0: ##20 最低位出现1的次数
k += b*a
elif c == 1: #21 最低位出现1的次数
k += b*a + 1 + n%a
else:
k += (b + 1)*a
a *= 10
return k
# write code here
3.【题目】描述输入侧侧侧侧侧侧侧侧一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321 },则打印出这三个数字能排成的最小数字为321323.(将数字转换成字符,x + y和y + x比较大小)
的的的的的的.sort(列表,CMP,反向).LIST,排序数组; CMP,排序函数;反向默认为升序假
CMP(X,Y)比较大小函数,X> Y返回-1; X <Y返回1; X = Y返回0
# -*- coding:utf-8 -*-
class Solution:
def PrintMinNumber(self, numbers):
if not numbers:
return ''
num = map(str, numbers)
num.sort(lambda x,y: cmp(x+y,y+x))
return "".join(num)
# write code here
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
# -*- coding:utf-8 -*-
import itertools
class Solution:
def PrintMinNumber(self, numbers):
if not numbers:
return ""
list = []
for i in itertools.permutations(numbers):##全排列
num = ""
for j in i:
temp = str(j)
num += temp
list.append(int(num))
return min(list)
# write code here
4.【题目描述】把只包含质因子2,3和5的数称作丑数(Ugly Number)。例如6,8都是丑数,但14不是,因为它包含质因子7.习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第ñ个丑数。
(和之前的找序列第几个数一样,将数据存在数组中。丑数一定是2,3,5的乘积,将每一个已经存在的丑数乘2,3,5即可。)
# -*- coding:utf-8 -*-
class Solution:
def GetUglyNumber_Solution(self, index):
if index <= 0:
return 0
if index == 1:
return 1
lis = [1]
index2 = 0
index3 = 0
index5 = 0
pa = 1
while pa < index:
pa += 1
temp = min(lis[index2]*2,lis[index3]*3,lis[index5]*5)
lis.append(temp)
if temp == lis[index2]*2:
index2 +=1
if temp == lis[index3]*3:
index3 +=1
if temp == lis[index5]*5:
index5 +=1
return lis[-1]
# write code here
5.【题目描述】在一个字符串(0 <=字符串长度<= 10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置,如果没有则返回-1(需要区分大小写)。
# -*- coding:utf-8 -*-
class Solution:
def FirstNotRepeatingChar(self, s):
if not s:
return -1
for index, char in enumerate(s):
if s.count(char) == 1:
return index
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
# -*- coding:utf-8 -*-
class Solution:
def FirstNotRepeatingChar(self, s):
if not s:
return -1
onece = []
res = []
for i in s:
if i in onece:
onece.remove(i)
res.append(i)
elif i not in onece and i not in res:
onece.append(i)
if len(onece) == 0:
return -1
else:
return s.index(onece[0])
# write code here
6.【题目】描述在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007取模的结果输出。即输出P%1000000007
sss
7.【题目】描述输入侧侧侧两个链表,找出它们的第一个公共结点。
【解法】1.求序值,将两个链表相同,一起走
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def FindFirstCommonNode(self, pHead1, pHead2):
p1 = pHead1
p2 = pHead2
while(p1!=p2):
if p1 == None:
p1 = pHead2
else:
p1 = p1.next
if p2 == None:
p2 = pHead1
else:
p2 = p2.next
return p1
@@@@@@@@@@@@@@@@@@@@@
class Solution:
def FindFirstCommonNode(self, pHead1, pHead2):
p1 = pHead1
p2 = pHead2
list = []
while p1:
list.append(p1.val)
p1 = p1.next
while p2:
if p2.val in list:
return p2
else:
p2 = p2.next
# write code here
8.【题目】描述统计一个数字在排序数组中出现的次数。(二分查找)
# -*- coding:utf-8 -*-
class Solution:
def GetNumberOfK(self, data, k):
count = 0
for i in data:
if i == k:
count += 1
return count
# write code here
@@@@@@@@@@@@@@@@@@@@@@@@@@
class Solution:
def GetNumberOfK(self, data, k):
return data.count(k)
# write code here
@@@@@@@@@@@@@@@@@@@@@@@@@@@@
# -*- coding:utf-8 -*-
class Solution:
def GetNumberOfK(self, data, k):
if not data:
return 0
if k< data[0] or k>data[-1]:
return 0
fir = 0
end = len(data)-1
leg = len(data)-1
num = 0
while fir <= end:
mid = (fir + end) / 2
if k == data[mid]:
while mid >= 0 and data[mid] == k:
mid = mid - 1
mid = mid + 1
while mid <= leg and data[mid] == k:
mid = mid + 1
num += 1
return num
elif k>data[mid]:
fir = mid +1
else:
end = mid -1
return num
# write code here
9.【题目】描述输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
dd