从零开始的leetcode刷题 (C++,Python,Java)

本文作者分享了在LeetCode上的刷题经历,记录了从第一题到第十题的部分解题答案,涉及Python字典算法、滑动窗口法、归并排序等技术。虽然不深入解析思路,但可以看出作者在解题过程中对不同算法的应用和思考。
摘要由CSDN通过智能技术生成

本文 不会详细描述解题思路,纯粹作为自己的刷题记录贴。
具体题目内容就不在这里贴出,只放出自己的解题答案:

第一题

//执行时间100ms,使用内存9.9m
//C++暴力算法
class Solution {
   
public:
    vector<int> twoSum(vector<int>& nums, int target) {
   
        vector<int> result;
        for (int i = 0; i < nums.size(); i++)
        {
   
            int temp = 0;
            temp = target - nums[i];
            vector<int>::iterator it = find(nums.begin(), nums.end(), temp);
            if(it==nums.end()) //未查询到该对象
            {
   
                continue;
            }
            else
            {
   
                int index = &*it - &nums[0];
                if (index == i)//表示 同一个下标
                {
   
                    continue;
                }
                else
                {
   

                    result.push_back(i);
                    result.push_back(index);
                    break;
                }
            }
        }
        return result;
    }
};

python字典算法

#python 算法
#字典算法,C++同样可以使用,但是要注意的是,要将列表中的值作为键值,下标作为value
#执行时间 40ms
from typing import List
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:  
    #这里的冒号和箭头 表示的是提示参数传入类型和输出类型
        hashmap={
   }
        for i in range(0,len(nums)):  #range顾头不顾尾
            hashmap[nums[i]]=i
        result=0
        for i in range(0,len(nums)):
            temp=target-nums[i]
            result=hashmap.get(temp) #如果get找不到该对象 则会返回none
            if result != None and result != i:
                return [i,result]

第二题

//运行时间48ms
//暴力算法,指针时间长没用了,有点不熟悉。
//内存65MB
class Solution {
   
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
   
        int length1=0;
        int length2=0;
        ListNode* l1s = l1;
        ListNode* l2s = l2;
        while (l1s != nullptr)
        {
   
            length1++;
            l1s = l1s->next;
        }
        while (l2s != nullptr)
        {
   
            length2++;
            l2s = l2s->next;
        }
     
        if (length1 < length2)
        {
   
            ListNode* temp = l1;
            l1 = l2;
            l2 = temp;
        }
        //前面这么长一大串就是确保l1比l2长
        ListNode* result = new ListNode();
        ListNode* p=result;
        bool flag = false;
        while (l2 != nullptr)
        {
   
            int temp;
            if (flag)
            {
   
                temp = l1->val + l2->val+1;
            }
            else
            {
   
                temp = l1->val + l2->val;
            }
            if (temp >= 10)
            {
   
                flag = true;
            }
            else
            {
   
                flag = false;
            }
            temp = temp % 10;
            p->val = temp;
            l1 = l1->next;
            l2 = l2->next;
            if (l2 != nullptr)
            {
   
                p->next = new ListNode();
                p = p->next;
            }
        }
        //这里 短的已经被算完了
        while (l1 != nullptr)
        {
   
            int temp = 0;
            if (flag)
            {
   
               temp = l1->val + 1;
            }
            else
            {
   
                temp = l1->val;
            }
            if (temp == 10)
            {
   
                flag = true;
                temp = 0;
            }
            else
            {
   
                flag = false;
            }
              p->next = new ListNode(temp);
               l1 = l1->next;
               p = p->next;
        }
        if (flag) //如果还有进位
        {
   
            p->next = new ListNode(1);
        }
        return result;
    }
};

第三题

//这题做的不是很快,用时大概在17ms
//利用char转int,来记录是否有重复
//属于简化的 暴力算法吧
class Solution {
   
    public int lengthOfLongestSubstring(String s) {
   
    	//我们可以定义一个数组
    	int length=s.length();
    	if(length==1||length==0)
    	{
   
    		return length;
    	}
    	int flagplace=0;
    	int maxlength=0;
    	for(int i=0;i<length;i++)
    	{
   
    		int Count[]=new int[256];
    		int templength=0;
    		for(flagplace=i;flagplace<length;flagplace++)
    		{
   
    			int place=s.charAt(flagplace); //这里char 转 int ascii码
    			Count[place]++;
    		
    			if(Count[place]>1)
    			{
   	
    				maxlength=(flagplace-i)>maxlength?(flagplace-i):maxlength;
    				break;
    			}
    			
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值