leetcode刷题(二)

本文深入解析了五道经典算法题目,包括两数之和、链表相加、最长无重复子串、二分查找及字符串Z字形排列,提供了详细的代码实现与思路讲解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

#include<vector>
#include<unordered_map>  

vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> res;
        unordered_map<int, int> hash;//无序hash表,时间复杂度o(1)
        
        for(int i = 0; i < nums.size(); i++)
        {
            int another = target - nums[i];
            if(hash.count(another)){
                res = vector<int>({hash[another], i});
                break;
            }
            
            hash[nums[i]] = i;
        }
        return res;
    }

2.给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。

如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

您可以假设除了数字 0 之外,这两个数都不会以 0 开头。

 struct ListNode {
     int val;
     ListNode *next;
     ListNode(int x) : val(x), next(NULL) {}
 };  

  int nodeValue(ListNode *node)
    {
        if(!node)
            return 0;
        
        return node->val;
    }
    
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        
        int carry = 0;
        ListNode *lstart = NULL, *lnext = NULL;
        
        while(l1 || l2)
        {     
            if(NULL == lstart){
                lnext = lstart = new ListNode((nodeValue(l1) + nodeValue(l2)) % 10);
            }else {
                lnext = lnext->next = new ListNode((nodeValue(l1) + nodeValue(l2) + carry) % 10);
            }
            
            carry = (nodeValue(l1) + nodeValue(l2) + carry) / 10;
            
            l1 = l1 ? l1->next : NULL;
            l2 = l2 ? l2->next : NULL;
            
            if(!l1 && !l2)
                if(carry > 0)
                    lnext->next = new ListNode(carry);           
        }
        
        return lstart;
    }

3.给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 

#include<map>
#include<math.h>

int lengthOfLongestSubstring(std::string s)
{
	int i, t_count = 0, max_count = 0;
	std::multimap<char, int> sub_map;

	for (i = 0; i < s.size(); )
	{
		sub_map.insert(std::make_pair(s[i], i));;
		t_count = sub_map.size();

		do
		{
			if (sub_map.count(s[i]) > 1)
			{
				if (i + 1 == s.size())
				{
					i++;  t_count--;
					break;
				}

				max_count = fmax(t_count - 1, max_count);
				i = sub_map.lower_bound(s[i])->second + 1;
				sub_map.clear();
			}
			else
				i++;
		} while (false);

		if (i == s.size())
		{
			max_count = fmax(t_count, max_count);
			break;
		}
	}
	return max_count;
}

4.重温一下二分查找 

int binaryFind(std::vector<int> &nums, int left, int right, int val)
{
	int centrel = (left + right) / 2;

	if (centrel == left || centrel == right)
		if (nums[centrel] == val)
			return centrel;
		else
			return -1;

	if (nums[centrel] > val)
		right = centrel - 1;
	else if (nums[centrel] < val)
		left = centrel + 1;
	else
		return centrel;

	return binaryFind(nums, left, right, val);
}

5.将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。 

                                         

std::string convert(std::string s, int numRows) {

	if (numRows == 1)
		return s;

	std::string z_s;
	int i = 0, j = 0, index = 0, line = 0;
	int t = 2 * (numRows - 1);

	while (i < s.size())
	{
		if (line == 0)
			index = abs((j * t) -  line );
		else if (line == numRows - 1) 
			index = abs((j * t) + line);
		else
			index = abs((j * t) - index);

		if (index >= s.size())
		{
			line++; j = 0;
			index = line;
			continue;
		}
		z_s.push_back(s[index]);
		j++; i++;
	}
	return z_s;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值