lintcode入门篇一

1.反转一个只有3位数的整数。

样例

样例 1:

输入: number = 123
输出: 321

样例 2:

输入: number = 900
输出: 9

注意事项

你可以假设输入一定是一个只有三位数的整数,这个整数大于等于100,小于1000。

class Solution:
    """
    @param number: A 3-digit number.
    @return: Reversed number.
    """
    def reverseInteger(self, number):
        # write your code here
        '''
        首位数字:number//100 整除
        第二位数字:number//10%10
        第三位数字:number%10
        最总结果:number%10*100+number//10%10*10+number//100*1
        
        '''
        return(number%10*100+number//10%10*10+number//100*1)

注释:

// 整除,得到的是整除 

% 模除,得到的是余数 

/ 直接除,得到的可能是小数

2.整数排序

给一组整数,按照升序排序,使用选择排序,冒泡排序,插入排序或者任何 O(n2) 的排序算法。

样例

样例  1:
	输入:  [3, 2, 1, 4, 5]
	输出:  [1, 2, 3, 4, 5]
	
	样例解释: 
	返回排序后的数组。

样例 2:
	输入:  [1, 1, 2, 1, 1]
	输出:  [1, 1, 1, 1, 2]
	
	样例解释: 
	返回排好序的数组。

 
 
输入测试数据 (每行一个参数)如何理解测试数据?
##冒泡排序
def  sortnums(nums):
    for i in range(len(nums)):
        for j in range(len(nums) - 1):
            if nums[j] > nums[j+1]:
                temp = nums[j]
                nums[j]=nums[j+1]
                nums[j+1]=temp
    return  nums
res=sortnums([3,5,2,65,2])
print(res)
class Solution:
    """
    @param A: an integer array
    @return: nothing
    """
    def sortIntegers(self, A):
        # write your code here
        for i in range(len(A)):
            for j in range(i,len(A)):
                if A[i]>A[j]:
                    temp = A[j]
                    A[j] = A[i]
                    A[i] = temp
        return A

3.二分查找

中文English

给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1

样例

样例  1:
	输入:[1,4,4,5,7,7,8,9,9,10],1
	输出: 0
	
	样例解释: 
	第一次出现在第0个位置。

样例 2:
	输入: [1, 2, 3, 3, 4, 5, 10],3
	输出: 2
	
	样例解释: 
	第一次出现在第2个位置
	
样例 3:
	输入: [1, 2, 3, 3, 4, 5, 10],6
	输出: -1
	
	样例解释: 
	没有出现过6, 返回-1

挑战

如果数组中的整数个数超过了2^32,你的算法是否会出错?

常见写法:

class Solution:
    """
    @param nums: The integer array.
    @param target: Target to find.
    @return: The first position of target. Position starts from 0.
    """
    def binarySearch(self, nums, target):
        # write your code here
        if target in nums:
            return nums.index(target)
        else:
            return -1       

注释:

数组[].index()  可以查找到target在数组的第几个位置,从0开始算起

一行表达式:

def binarySearch(nums,target):
    return  nums.index(target) + 1 if target in nums else  -1
result = binarySearch([1,3,4,5,6,5,8,8],5)

二维列表法:

def binarySearch(nums,target):
    return  [-1,nums.index(target)][target in nums]
result = binarySearch([1,3,4,5,6,5,8,8],5)
print(result)
注释:
前面的作为结果,后面的作为条件
[0,1][条件]
如果符合条件的话,则返回1的结果,如果不符合的话,则返回0的结果
逻辑运算符写法:
def binarySearch(nums,target):
    return  target in nums and nums.index(target) or -1
result = binarySearch([1,3,4,5,6,5,8,8],5)
print(result)
注释:
对于c =(a>b and a or b)来说
如果a > b:
真: 则 1 and a = a
假: 则 0 or b = b

4.列表扁平化

给定一个列表,该列表中的每个元素要么是个列表,要么是整数。将其变成一个只包含整数的简单列表。

样例

样例  1:
	输入: [[1,1],2,[1,1]]
	输出:[1,1,2,1,1] 
	
	样例解释:
	将其变成一个只包含整数的简单列表。


样例 2:
	输入: [1,2,[1,2]]
	输出:[1,2,1,2]
	
	样例解释: 
	将其变成一个只包含整数的简单列表。
	
样例 3:
	输入:[4,[3,[2,[1]]]]
	输出:[4,3,2,1]
	
	样例解释: 
	将其变成一个只包含整数的简单列表。
	

挑战

请用非递归方法尝试解答这道题。

注意事项

如果给定的列表中的要素本身也是一个列表,那么它也可以包含列表。

输入测试数据 (每行一个参数)如何理解测试数据?
class Solution(object):
    def __init__(self):
        self.list = []

    # @param nestedList a list, each element in the list 
    # can be a list or integer, for example [1,2,[1,2]]
    # @return {int[]} a list of integer
    def flatten(self, nestedList):
        # Write your code here
        for row in nestedList:
            if isinstance(row,int):
                self.list.append(row)
            else:
                self.flatten(row)
        return self.list
           
注释:if isinstace(row,int) 判断的是这个row的类型是否是int类型
self.flatten(row) 如果不是int类型的话,为list类型,递归下去,内层递归加上外层循环。
new_list = []
def flatten(list):
    for column in list:
        if type(column) == int:
            new_list.append(column)
        else:
            flatten(column)
    return  new_list
result = flatten([[32,12,54,21],43,[22,1],[21]])
print(result)
'''
首先第一次外层循环过来,判断不是int类型,递归一次,循环,全部符合要求,append进来
第二次外层循环,符合要求,append进来
第三次外层循环,判断不是int类型,递归一次,循环,全部符合要求,append进来
第四次外层循环,判断不是int类型,递归一次,循环,符合要求,append进来
结束
'''
原文地址:https://www.cnblogs.com/yunxintryyoubest/p/12154028.html