LintCode 算法部分入门题目 【C++】

目录

1613.最高频的IP

483.链表转数组

449.字符转整数

 228.链表的中点

225.在链表中找节点

22.列表扁平化


1613.最高频的IP

给定一个字符串数组lines, 每一个元素代表一个IP地址,找到出现频率最高的IP。

输入 = ["192.168.1.1","192.118.2.1","192.168.1.1","192.118.2.1","192.118.2.1"]
输出 "192.118.2.1"

 将字vector用sort排序后,相同的字符串挨到了一起,每一个字符串只需与其之后的进行比较即可,因为当遇到一个新的字符串时,前面的一定与本身不同,因此一层循环即可。

class Solution {
public:
    /**
     * @param ipLines: ip  address
     * @return: return highestFrequency ip address
     */
    string highestFrequency(vector<string> &ipLines) {
        // Write your code here
        int time = 1;
		int time_max = 0;
		sort(ipLines.begin(), ipLines.end());
		string res = *(ipLines.begin());
		for (vector<string>::iterator it = ipLines.begin();it != ipLines.end();it++) 
		{
			if (*(it) == *(it + 1))
				time++;
			else 
			{
				if (time > time_max) 
				{
					res = *it;
					time_max = time;
				}
                time = 1;
			}
		}
		return res;
    }
};

483.链表转数组

将一个链表转换为一个数组。

输入: 1->2->3->null
输出: [1,2,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 head of linked list.
     * @return: An integer list
     */
    vector<int> toArrayList(ListNode *head) {
        // write your code here
        vector<int> v;
        while(head){
            v.push_back(head->val);
            head=head->next;
        }
        return v;
    }
};

449.字符转整数

将字符转换为一个整数。你可以假设字符是ASCII码,也就是说转换后的整数在0~255之间。

样例  1:
	输入:  'a'
	输出: 97
	
	样例解释: 
	返回ASCII码中对应的数字.

样例 2:
	输入:  '%'
	输出: 37
	
	样例解释: 
	返回ASCII码中对应的数字.

没啥说的。

class Solution {
public:
    /**
     * @param character: a character
     * @return: An integer
     */
    int charToInteger(char character) {
        // write your code here
        return character;
    }
};

 228.链表的中点

找链表的中点,并返回这个节点。

输入:  1->2->3
输出: 2	
样例解释: 返回中间节点

 经典的快慢指针问题。快指针一次移动两个节点,慢指针一次移动一个节点。当链表内节点个数为奇数时,快指针到最后一个节点时,慢指针到中间的节点。当链表内节点个数为偶数时,快指针的下一个节点为最后一个节点时,慢指针到中间的前一个节点。

/**
 * 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 head of linked list.
     * @return: a middle node of the linked list
     */
    ListNode * middleNode(ListNode * head) {
        // write your code here
        ListNode *fast=head;
        ListNode *slow=head;
        while(fast && slow)
        {
            if(fast->next == NULL || fast->next->next == NULL)
                return slow;
            else
            {
                fast = fast->next->next;
                slow = slow->next;
            }
        }
    }
};

225.在链表中找节点

在链表中找值为 value 的节点,如果没有的话,返回空(null)。

样例 1:

输入:  1->2->3 and value = 3
输出: 最后一个结点

样例 2:

输入:  1->2->3 and value = 4
输出: null

 记得判断链表本身为空的情况。

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


class Solution {
public:
    /*
     * @param head: the head of linked list.
     * @param val: An integer.
     * @return: a linked node or null.
     */
    ListNode * findNode(ListNode * head, int val) {
        // write your code here
        if (head == NULL)
			return NULL; 
		while (head->val != val)
		{
			if (head->next == NULL)
				return NULL;
			else
				head = head->next;
		}
		return head;
    }
};

22.列表扁平化

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

样例 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]

 建立一个栈,将题目给的vector元素从后往前入栈。建立一个新的vector res,然后依次出栈,若是整数,则进入res,如不是整数,则将该列表的元素一次存入res;

/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * class NestedInteger {
 *   public:
 *     // Return true if this NestedInteger holds a single integer,
 *     // rather than a nested list.
 *     bool isInteger() const;
 *
 *     // Return the single integer that this NestedInteger holds,
 *     // if it holds a single integer
 *     // The result is undefined if this NestedInteger holds a nested list
 *     int getInteger() const;
 *
 *     // Return the nested list that this NestedInteger holds,
 *     // if it holds a nested list
 *     // The result is undefined if this NestedInteger holds a single integer
 *     const vector<NestedInteger> &getList() const;
 * };
 */
class Solution {
public:
    // @param nestedList a list of NestedInteger
    // @return a list of integer
    vector<int> flatten(vector<NestedInteger> &nestedList) {
        // Write your code here
        stack <NestedInteger> st;
		vector<int> res;
		for (int i = nestedList.size() - 1; i >= 0; i--) 
		{
			st.push(nestedList[i]);
		}
		while (!st.empty()) 
		{
			NestedInteger out = st.top();
			st.pop();
			if (out.isInteger()) 
			{
				res.push_back(out.getInteger());
			}
			else 
			{
				const vector<NestedInteger> &list = out.getList();
				for (int i = list.size() - 1; i >= 0; i--) 
				{
					st.push(list[i]);
				}
			}
		}
		return res;
    }
};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kether410

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值