LintCode 入门题目解析

第一题、单例

单例 是最为最常见的设计模式之一。对于任何时刻,如果某个类只存在且最多存在一个具体的实例,那么我们称这种设计模式为单例。例如,对于 class Mouse (不是动物的mouse哦),我们应将其设计为 singleton 模式。

你的任务是设计一个 getInstance 方法,对于给定的类,每次调用 getInstance 时,都可得到同一个实例。

样例

在 Java 中:

A a = A.getInstance(); A b = A.getInstance();

a 应等于 b.

挑战 

如果并发的调用 getInstance,你的程序也可以正确的执行么?

 

答案:

class Solution {
    /**
     * @return: The same instance of this class every time
     */
    private final static Solution solution = new Solution();
    
    private Solution(){
        
    }
    public static Solution getInstance() {
        return solution;
    }
};

 

第二题 矩阵面积

 

实现一个矩阵类Rectangle,包含如下的一些成员变量与函数:

  1. 两个共有的成员变量 width 和 height 分别代表宽度和高度。
  2. 一个构造函数,接受2个参数 width 和 height 来设定矩阵的宽度和高度。
  3. 一个成员函数 getArea,返回这个矩阵的面积。

样例

Rectangle rec = new Rectangle(3, 4); rec.getArea(); // should get 12

 

答案:

public class Rectangle {

    private int width;
    private int height;
    
    public Rectangle(int width, int height){
        this.width = width;
        this.height = height;
    }
    
    public int getArea(){
        int area = width * height;
        return area;
    }
}

第三题 二叉树的最大节点

在二叉树中寻找值最大的节点并返回。

 

样例

给出如下一棵二叉树:

1 / \ -5 2 / \ / \ 0 3 -4 -5

返回值为 3 的节点。

答案:

public class Solution {
    /**
     * @param root the root of binary tree
     * @return the max ndoe
     */
    int maxValue = -9999;
    TreeNode returnNode ;
    void max(TreeNode root){
        if(root == null){
            return ;
        }
        if(root.val > maxValue){
            maxValue = root.val;
            returnNode = root;
        }
        max(root.left);
        max(root.right);
        
    }
    
    public TreeNode maxNode(TreeNode root) {
        if(root == null){
            return root;
        }
        max(root);
        return returnNode;
        
    }
}

 

第四题 整数排序

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

样例

对于数组 [3, 2, 1, 4, 5], 排序后为:[1, 2, 3, 4, 5]。

 

public class Solution {
    /**
     * @param A an integer array
     * @return void
     */
    public void sortIntegers(int[] A) {
        for(int i = 0; i < A.length; i ++){
            for(int j = i ; j < A.length; j ++){
                if(A[i] > A[j]){
                    int temp = A[i];
                    A[i] = A[j];
                    A[j] = temp;
                }
            }
        }
    }
}

第五题 删除链表中的元素

删除链表中等于给定值val的所有节点。

样例

给出链表 1->2->3->3->4->5->3, 和 val = 3, 你需要返回删除3之后的链表:1->2->4->5。

 

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    /**
     * @param head a ListNode
     * @param val an integer
     * @return a ListNode
     */
    public ListNode removeElements(ListNode head, int val) {
        ListNode result = head;
        if(head == null){
            return null;
        }
        while(head.next != null){
            if(head.next.val == val)
            {
                if(head.next.next!=null)
                    head.next=head.next.next;
                else 
                {
                  head.next=null;
                  break;
                }
            }
            else
            {
                head=head.next;
            }

        }
        if(result.val == val) return result.next;
        return result;
    }
}

第六题 斐波那契数列

查找斐波纳契数列中第 N 个数。

所谓的斐波纳契数列是指:

  • 前2个数是 0 和 1 。
  • 第 i 个数是第 i-1 个数和第i-2 个数的和。

斐波纳契数列的前10个数字是:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ...

 注意事项

The Nth fibonacci number won't exceed the max value of signed 32-bit integer in the test cases.

样例

给定 1,返回 0

给定 2,返回 1

给定 10,返回 34

答案:

class Solution {
    /**
     * @param n: an integer
     * @return an integer f(n)
     */
    public int fibonacci(int n) {
        if(n == 1){
            return 0;
        }else if(n ==2){
            return 1;
        }else{
            int a = 0, b = 1;  
            do {  
                int tmp = b;  
                b += a;  
                a = tmp;  
            } while (--n > 2);  
            return b;
        }
    }
}

 

 

 

 

 

 

 

转载于:https://my.oschina.net/hxflar1314520/blog/912577

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值