leetcode

本文用来记录自己做leetcode的解题思路,可能不是最优解法.

1,Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

我的思路:
题目给出的数组大小并不确定,因此我们可以使用vector来当这组这组数据的容器,要求出相加之和等于target的两个数,必须遍历数组分别求出每两个数之和,相等则返回这两个数的索引,由于返回的是一个数组,所以还是用一个vector来存储返回的两个元素。

我的解法:

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) 
    {
        vector<int> vec;
        for(int i = 0;i<nums.size()-1;i++)
        {
            for(int j = i+1;j<nums.size();j++)
            {
                if(nums[i]+nums[j] == target)
                {
                  vec.push_back(i);
                  vec.push_back(j);
                  return vec;
                }
            }
        }
    }
};

2,You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

我的思路:
emmmm….这道题,我是用了挺长时间才完全通过,在解答过程中总是有没有考虑到的情况。首先,题目给出的测试用例很特殊,所以我换了一个测试用例自己去解。
比如:

Input:(3->4->2) + (7->0)
Output:0->5->2
Explanation:243 + 7 = 250

从这个测试用例中可以看出来,当两个链表的前一个节点相加的值超过10,需要给它的后一个节点产生进位,而两数相加最大的情况不过就是9+9+1(进位),不会超过20;
其次,一开始我写的代码没有考虑到的情况:

Input:5 + 5
Output:0->1

这种情况又需要单独做处理。另外,就是要考虑两条链表长度不同时如何相加,此类情况较简单,不做过多解释。
我的解答:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        if(l1 == NULL && l2 == NULL)
           return NULL;
        if(l1 == NULL)
            return l2;
        else if(l2 == NULL)
            return l1;
        ListNode* cur1 = l1;
        ListNode* cur2 = l2;
        int up = 0;
        queue<int> q;
        while(cur1 || cur2)
        {
           int n1 = 0;
           if(cur1)
           {
               n1 = cur1->val;
               cur1 = cur1->next;
           }
            int n2 = 0;
            if(cur2)
            {
                n2 = cur2->val;
                cur2 = cur2->next;
            }
           int sum = n1 + n2 + up;
           if(sum > 9)
           {
               up = sum/10;
               sum %= 10;
           }
            else
            {
                up = 0;
            }
            q.push(sum);
           if((cur1 == NULL) && (cur2 == NULL) && (up!=0))
          {
            q.push(up);
          }
        }
        int front = q.front();
        ListNode* head = new ListNode(front);
        q.pop();
       ListNode *newNode = head;
       while(!q.empty())
      {
        front = q.front();
        ListNode* curNode = new ListNode(front);
        curNode->next = NULL;
        newNode->next = curNode;
        newNode = curNode;
        q.pop();
      }
    return head;
    }
};

3,Given a string, find the length of the longest substring without repeating characters.

Examples:

Given “abcabcbb”, the answer is “abc”, which the length is 3.

Given “bbbbb”, the answer is “b”, with the length of 1.

Given “pwwkew”, the answer is “wke”, with the length of 3. Note that the answer must be a substring, “pwke” is a subsequence and not a substring.

思路:
从题意可以知道,当子串的最大长度是其中不包括相同的字符,当后面出现的字符前面已经出现过时,就要重新计算其长度,并且与前面的子串长度比较,大的那个就是最大子串长度。
所以,我们需要一个数据结构,既能存储是某个 字符,又能存储字符出现的次数,很容易想到用map

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        map<char,int> mp;
        int start = -1;
        int maxlength = 0;
        for(int i = 0;i<s.size();i++)
        {
            if(mp.count(s[i]) != 0)
            {
                start = max(start,mp[s[i]]);
            }
            mp[s[i]] = i;
            maxlength = max(maxlength,i-start);
        }
        return maxlength;
    }
};

4,Given asorted array,remove theduplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array,you must do this in place with constant memory.
For example,Given input array A = [1,1,2],
Your function should return length = 2,and A is now [1,2]

int Remove(int a[], int n)
    {
        for (int i = 1; i < n; ++i)
        {
            if (a[index] != a[i])
            { 
                a[++index] = a[i];
            }
        }
        return index+1;
    }

5,Follow up for “Remove Duplicates”:What if duplicates are allowed at most twice?
For example,Given sorted array A = [1,1,1,2,2,3],
Your function should return length = 5,and A is now [1 ,1,2,2,3]

int Remove(int a[], int n)
    {
        if (n <= 2)
            return n;

        int index = 2;
        for (int i = 2; i < n; i++)
        {
            if (a[i] != a[index - 2])
            {
                a[index++] = a[i];
            }
        }
        return index;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值