LintCode第九章入门算法合集

今日刷完了LintCode的入门算法共10道,按照自身的理解答题如下:

1.反转一个3位整数

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

样例

123 反转之后是 321
900 反转之后是 9

class Solution {
public:
    /**
     * @param number: A 3-digit number.
     * @return: Reversed number.
     */
    int reverseInteger(int number) {
        int number1=number%10;
        int number2=(number/10)%10;
        int number3=number/100;
        int NewNumber=number1*100+10*number2+number3;
        return NewNumber;
        // write your code here
    }
};

2.大小写转换

将一个字符由小写字母转换为大写字母
样例

a -> A

b -> B

class Solution {
public:
    /**
     * @param character: a character
     * @return: a character
     */
    char lowercaseToUppercase(char character) {
        character-=32;
        // write your code here
    }
};

3.斐波纳契数列

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

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

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

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

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

样例

给定 1,返回 0

给定 2,返回 1

给定 10,返回 34

class Solution {
public:
    /**
     * @param n: an integer
     * @return: an ineger f(n)
     */
    int fibonacci(int n) {
        if(n==1)
        return 0;
        if(n==2||n==3)
        return 1;
        int s1=1,s2=1;
        int i=4;
        int sum=0;
        while(i<=n)
        {
            sum=s1+s2;
            s1=s2;
            s2=sum;
            i++;
        }
        return sum;
        // write your code here
    }
};

4.删除链表中的元素

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

样例

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

/**
 * Definition of singly-linked-list:
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *        this->val = val;
 *        this->next = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param head: a ListNode
     * @param val: An integer
     * @return: a ListNode
     */
    ListNode *removeElements(ListNode *head, int val) {
        // Write your code here
        while ( head != NULL && head->val == val){   
            head = head->next;   //将头节点后移
        }
        if ( head == NULL) {   //链表为空
            return NULL;
        } 
        
        ListNode *pre = head;
        ListNode *p = head->next;
        
        while( p != NULL) {
            if ( p->val == val) {
                pre->next = p->next;   //将原节点p删除
            } else {
                pre = p;    //向后移动一个节点
            }
            p = p->next;
        }
        return head;
    }
};

5.矩阵面积

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

  1. 两个共有的成员变量 widthheight 分别代表宽度和高度。
  2. 一个构造函数,接受2个参数 width 和 height 来设定矩阵的宽度和高度。
  3. 一个成员函数 getArea,返回这个矩阵的面积。
样例
Java:
    Rectangle rec = new Rectangle(3, 4);
    rec.getArea(); // should get 12

Python:
    rec = Rectangle(3, 4)
    rec.getArea()
public class Rectangle {
    /*
     * Define two public attributes width and height of type int.
     */
    // write your code here
    public int width;
    public int height;
    /*
     * Define a constructor which expects two parameters width and height here.
     */
    // write your code here
    public Rectangle(int width,int height)
    {
        this.width=width;
        this.height=height;
    }
    
    /*
     * Define a public method `getArea` which can calculate the area of the
     * rectangle and return.
     */
    // write your code here
    public int getArea()
    {
        return width*height;
    }
}

6.整数排序

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

样例

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

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

这里我用的是冒泡算法,本来想用快速排序,却没成功。

这里附上其他算法的传送门:https://www.cnblogs.com/lelelelele/p/6087036.html

                                            http://blog.csdn.net/dougan_/article/details/78095783


7.链表节点计数

计算链表中有多少个节点.

样例

给出 1->3->5, 返回 3.

/**
 * Definition of singly-linked-list:
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *        this->val = val;
 *        this->next = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param head: the first node of linked list.
     * @return: An integer
     */
    int countNodes(ListNode * head) {
        // write your code here
        ListNode *p=head;
        int i=0;
        while(p!=NULL)
        {
            p=p->next;
            i++;
        }
        return i;
    }
};

8.交换数组两个元素

给你一个数组和两个索引,交换下标为这两个索引的数字

样例

给出 [1,2,3,4] index1 = 2, index2 = 3. 交换之后变成 [1,2,4,3]

public class Solution {
    /**
     * @param A: An integer array
     * @param index1: the first index
     * @param index2: the second index
     * @return: nothing
     */
    public void swapIntegers(int[] A, int index1, int index2) {
        int temp;
        temp=A[index1];
        A[index1]=A[index2];
        A[index2]=temp;
        // write your code here
    }
}

9.二叉树的最大节点

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

样例

给出如下一棵二叉树:

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

返回值为 3 的节点。

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /*
     * @param root: the root of tree
     * @return: the max node
     */
    public TreeNode maxNode(TreeNode root) {
        // write your code here
    }
}

10.Hex Conversion(进制转换)

Given a decimal number n and an integer k, Convert decimal number n to base-k.

注意事项

1.0<=n<=2^31-1, 2<=k<=16
2.Each letter over 9 is indicated in uppercase

样例

Example 1:
Given n = 5, k = 2
return "101"

Example 2:
Given n = 30, k = 16
return "1E"

class Solution {
public:
    /**
     * @param n: a decimal number
     * @param k: a Integer represent base-k
     * @return: a base-k number
     */
    string hexConversion(int n, int k) {
        string res="";
        if(n==0)
        return "0";//在提交的时候要求你输出为"0"
        while (n>0)
        {
            char c;
            int m=n%k;
            if(m<=9)
            c=('0'+m);
            else
            c=('A'+m-10);
            res=c+res;
            n=n/k;
        }
        return res;
        
        // write your code here
    }
};

猜你喜欢

转载自blog.csdn.net/shentanyue/article/details/79596816