1、写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。
法一:
class Solution:
def Add(self, num1, num2):
ret = [num1,num2]
return sum(ret)
法二:
class Solution:
def Add(self, num1, num2):
while num1:
andnum = (num1 & num2) << 1 & 0xFFFFFFFF
xornum = num1 ^ num2 & 0xFFFFFFFF
num1 = andnum
num2 = xornum
return num2 if num2 < 0x7FFFFFFF else ~(num2 ^ 0xFFFFFFFF)
2、把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
class Solution:
def minNumberInRotateArray(self, rotateArray):
minnum = 0
for i in range(len(rotateArray)):
minnum = minnum if minnum < rotateArray[i] and minnum != 0 else rotateArray[i]
return minnum
class Solution:
def minNumberInRotateArray(self, rotateArray):
left = 0
right = len(rotateArray)-1
while left <= right:
if left == right:
return rotateArray[left]
mid = (left + right) >> 1
if rotateArray[mid] > rotateArray[right]:
left = mid + 1
else:
right = mid
3、输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
class Solution:
def reOrderArray(self, array):
ret = []
for i in array:
if i % 2 == 1:
ret.append(i)
for i in array:
if i % 2 == 0:
ret.append(i)
return ret
若要求不开辟空间,该怎么办?
#遇到偶数不动,遇到奇数往前换
list = map(int,raw_input().split())
p1 = 0
for i in range(len(list)):
if list[i]%2 == 1:
if i != 0:
for j in range(i,p1,-1):
tmp = list[j]
list[j] = list[j-1]
list[j-1] = tmp
p1 += 1
print list
#从前往后找,遇到偶数拎出来,将剩余的元素往前挪,剩了最后一个空位,把拎出的偶数放最后
list = map(int,raw_input().split())
length = len(list)
i = 0
count = 0
while i<length-count:
if list[i]%2 == 0:
tmp = list[i]
for j in range(i,length-1):
list[j] = list[j+1]
list[length-1] = tmp
count += 1
else:
i += 1
print list
不开辟空间但不要求顺序不变
#前后两个指针,往中间找,当从前边遇到偶数,后边遇到奇数,互换
def splitlist(list):
if list == []:
return False
p1, p2 = 0, len(list)-1
while p1 < p2:
while list[p1]%2 == 1:
p1 += 1
while list[p2]%2 == 0:
p2 -= 1
if p1 < p2:
p = list[p1]
list[p1] = list[p2]
list[p2] = p
return list
if __name__=='__main__':
list = map(int,raw_input().split())
splitlist(list)
print list
4、输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。
class Solution:
def GetLeastNumbers_Solution(self, tinput, k):
maxHeap = []
def creatmaxHeap(num):
maxHeap.append(num)
index = len(maxHeap)-1
while index:
parentindex = (index-1) >>1
if maxHeap[parentindex] < maxHeap[index]:
maxHeap[parentindex],maxHeap[index] = maxHeap[index], maxHeap[parentindex]
index = parentindex
else:
break
def adjustmaxHeap(num):
if num < maxHeap[0]:
maxHeap[0] = num
index = 0
length = len(maxHeap)
while index < length:
leftindex = index * 2 + 1
rightindex = index * 2 + 2
if rightindex < length:
if maxHeap[leftindex] < maxHeap[rightindex]:
largerindex = rightindex
else:
largerindex = leftindex
elif leftindex < length:
largerindex = leftindex
else:
break
if maxHeap[index] < maxHeap[largerindex]:
maxHeap[index],maxHeap[largerindex] = maxHeap[largerindex],maxHeap[index]
index = largerindex
else:
break
length = len(tinput)
if length < k or k < 1:
return []
for i in range(length):
if i < k:
creatmaxHeap(tinput[i])
else:
adjustmaxHeap(tinput[i])
maxHeap.sort()
return maxHeap
5、HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学。今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决。但是,如果向量中包含负数,是否应该包含某个负数,并期望旁边的正数会弥补它呢?例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。给一个数组,返回它的最大连续子序列的和,你会不会被他忽悠住?(子向量的长度至少是1)
class Solution:
def FindGreatestSumOfSubArray(self, array):
maxNum = None
tmpNum = 0
for i in array:
if maxNum == None:
maxNum = i
if tmpNum + i > i:
tmpNum += i
else:
tmpNum = i
if tmpNum > maxNum:
maxNum = tmpNum
return maxNum
6、输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323
class Solution:
def PrintMinNumber(self, numbers):
if not numbers:
return ""
numbers = list(map(str,numbers))
numbers.sort(cmp = lambda x,y: cmp(x+y,y+x))
return ''.join(numbers)