美文网首页
[入门级]lintcode--刷题

[入门级]lintcode--刷题

作者: 慕止 | 来源:发表于2018-10-21 23:28 被阅读0次
第一题
反转一个只有3位数的整数。

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

您在真实的面试中是否遇到过这个题?  
样例
123 反转之后是 321。
900 反转之后是 9。
class Solution:
    """
    @param number: A 3-digit number.
    @return: Reversed number.
    """
    def reverseInteger(self, number):
        # write your code here
        l = list(str(number))
        s = str(l[2]+l[1]+l[0]).replace('0', '')
        return int(s)
print(Solution().reverseInteger(123))
print(Solution().reverseInteger(900))
第二题
将一个字符由小写字母转换为大写字母

你可以假设输入一定在小写字母 a ~ z 之间

您在真实的面试中是否遇到过这个题?  
样例
a -> A

b -> B
class Solution:
    """
    @param character: a character
    @return: a character
    """
    def lowercaseToUppercase(self, character):
        # write your code here
        return character.upper()
print(Solution().lowercaseToUppercase('b'))
第三题
描述
实现一个矩阵类Rectangle,包含如下的一些成员变量与函数:

两个共有的成员变量 width 和 height 分别代表宽度和高度。
一个构造函数,接受2个参数 width 和 height 来设定矩阵的宽度和高度。
一个成员函数 getArea,返回这个矩阵的面积。
您在真实的面试中是否遇到过这个题?  
样例
Java:
    Rectangle rec = new Rectangle(3, 4);
    rec.getArea(); // should get 12

Python:
    rec = Rectangle(3, 4)
    rec.getArea()
class Rectangle:

    '''
     * Define a constructor which expects two parameters width and height here.
    '''
    # write your code here
    
    '''
     * Define a public method `getArea` which can calculate the area of the
     * rectangle and return.
    '''
    # write your code here
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def getArea(self):
        return self.width * self.height


rec = Rectangle(5,6)
rec.getArea()
第四题
描述
给一组整数,按照升序排序,使用选择排序,冒泡排序,插入排序或者任何 O(n2) 的排序算法。

您在真实的面试中是否遇到过这个题?  
样例
对于数组 [3, 2, 1, 4, 5], 排序后为:[1, 2, 3, 4, 5]。
class Solution:
    """
    @param A: an integer array
    @return: nothing
    """
    def sortIntegers(self, A):
        # write your code here
        return A.sort()

print(Solution().sortIntegers([3, 2, 1, 4, 5]))
第五题
描述
计算链表中有多少个节点.

您在真实的面试中是否遇到过这个题?  
样例
给出 1->3->5, 返回 3.
class Solution:
    """
    @param head: the first node of linked list.
    @return: An integer
    """
    def countNodes(self, head):
        # write your code here
            return len(head.replace('->', ''))

print(Solution().countNodes('1->3->5->1->3->5'))
第六题
class Solution:
    """
    @param A: An integer array
    @param index1: the first index
    @param index2: the second index
    @return: nothing
    """
    def swapIntegers(self, A, index1, index2):
        # write your code here
class Solution:
    """
    @param A: An integer array
    @param index1: the first index
    @param index2: the second index
    @return: nothing
    """
    def swapIntegers(self, A, index1, index2):
        # write your code here
        a=A[index1]
        A[index1]=A[index2]
        A[index2]=a
        return A

print(Solution().swapIntegers([1,2,3,4],2,3))

相关文章

  • [入门级]lintcode--刷题

    第一题 第二题 第三题 第四题 第五题 第六题

  • 刷题刷题

    时间紧迫,任务繁重,又有疫情影响,搞的人心惶惶,一时间复习得不安宁,又舍不得摆烂。 在焦灼、惶恐的情绪中,紧张急迫...

  • 2022-09-16

    刷题,刷题还是刷题

  • 2018-07-16

    刷题,祸害的何止是教育? 报班,刷题;买练习册,刷题;家教,刷题;跟不上,刷题;学得好,刷题;为了抢跑,刷题;为了...

  • 刷题啊刷题

    因为月底又要考试,所以最近几天一直在刷题。按说是看了书再看视频再刷题效果比较好才是。可是来不及了啊。 上次考试,就...

  • 刷题啊刷题

    刷题啊刷题 因为11月中旬要举行期中考试,所以最近几天,学校精心组织,一直在刷题。按说是看了书再看PPT课件或教师...

  • 2020-02-01关于刷题的几个建议

    算法刷题 针对性刷题,刻意练习。刻意刷题!不是麻木刷题!刷题前一定要先看书,清楚明白为什么要刷这些题,这些题刷完能...

  • 刷题

    清早起来刷题 吃饭也在刷题 上厕所也在刷题 中午也在刷题 下午也在刷题 晚上也在刷题 一天到晚都在刷题 考试马上到...

  • 程序猿刷题网站你知道吗?

    Coderbyte 刷题网LeetCode 刷题网Stack Overflow 刷题网

  • 刷算法题

    入门级难度的几道题目简单乘法->斐波那契数列->链表->整数排序->二叉树一点一点过度, 让思维进入到刷题状态. ...

网友评论

      本文标题:[入门级]lintcode--刷题

      本文链接:https://www.haomeiwen.com/subject/zkoizftx.html