leetcode problem (2-4)

Problem 2 --- Add Two Numbers

简单的模拟题。

Problem 3 --- Longest Substring Without Repeating Characters

题意: 给定一个字符串序列,找出最长无重复的子序列。如"abcabcbb"的最长不重复子序列为"abc"

思路: 首先分配一个hashTable[256],里面保存每个字符在当前字符序列中的位置,同时设置left变量表示当前无重复字符串的最左端位置。然后从头到尾扫面字符串S,每扫描一个字符便更新相应的hashTable,同时当前序列长度len+1。

    如果遇到的字符在当前子序列中有重复(即hashTable[elem] >= left),此时更新max和left: max = len > max ? len : max, left = hashtable[elem]。

    最后返回max.

    时间复杂度 O(n)   空间复杂度O(n)

代码:

class Solution {
public:
    int lengthOfLongestSubstring(string s){
        int max = 0;
        int index = 0;
        int len = 0;
        int left = 0;
        memset(m_hashTable, 0, sizeof(m_hashTable));
        for (auto elem : s) {
            if (m_hashTable[elem] != 0 && m_hashTable[elem] >= left) {
                max = max < len ? len : max;
                left = m_hashTable[elem];
                len = index - m_hashTable[elem];
            }
            ++len;
            m_hashTable[elem] = ++index;
        }
        max = max < len ? len : max;
        return max;
    }
private:
    int m_hashTable[256];
};

Problem 4 --- Median of Two Sorted Arrays

题意:给出两个已经排序好的数列,得到它们合并后的数列中位数。

思路: 这道题实际可以扩展为找到第k大的数。假定给出的序列为A[1..m]和B[1..n],合并后的序列为C[1..m+n]。

   第一种方法,合并两个数组,直接返回C[k],简单。时间复杂度是O(n)

   第二种方法是: 找到A[k/2]和B[k/2], 如果A[k/2] < B[k/2]。 那么说明A[1..k/2]一定在C[k]的左侧。因此可以分解为子问题:找到A[k/2+1..m]和B[1..n]的第k-k/2大数。最终用递归解此题。

      时间复杂度O(logk)

代码:

class Solution {
public:
    double findMedianSortedArrays(int A[], int m, int B[], int n) {
        int total = m + n;
        if (total & 0x01)
            return findKth(A, m, B, n, total / 2 + 1);
        else
            return (findKth(A, m, B, n, total / 2) +
                    findKth(A, m, B, n, total / 2 + 1)) / 2.0;
    }

private:
    int findKth(int A[], int m, int B[], int n, int k) {
        if (m > n)
            return findKth(B, n, A, m, k);
        if (m == 0)
            return B[k-1];
        if (k == 1)
            return min(A[0], B[0]);

        int posA = min(k/2, m), posB = k - posA;
        if (A[posA - 1] < B[posB - 1])
            return findKth(A + posA, m - posA, B, n, k - posA);
        else if (A[posA - 1] > B[posB - 1])
            return findKth(A, m, B + posB, n - posB, k - posB);
        else
            return A[posA-1];
    }
};

时间: 2024-10-15 15:00:42

leetcode problem (2-4)的相关文章

LeetCode Problem 90. Subsets II

python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ """ 思路整理:DFS recursion 1)对nums进行排序以避免nums中重复元素

LeetCode Problem: Majority Element查找多数元素

描述:Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. 思路1:Moore voting algorith

Solution to LeetCode Problem Set

Here is my collection of solutions to leetcode problems. LeetCode - Course Schedule LeetCode - Reverse Linked List LeetCode - Isomorphic Strings LeetCode - Count Primes LeetCode - Remove Linked List Elements LeetCode - Happy Number LeetCode - Bitwise

LeetCode Problem 2:Two Sum

描述: Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note t

LeetCode Problem 9:Palindrome Number回文数

描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could a

leetcode problem (5)

最长回文子串: 1. 暴力搜索   时间复杂度O(n^3) 2. 动态规划 dp[i][j] 表示子串s[i…j]是否是回文 初始化:dp[i][i] = true (0 <= i <= n-1);  dp[i][i-1] = true (1 <= i <= n-1); 其余的初始化为false dp[i][j] = (s[i] == s[j] && dp[i+1][j-1] == true) 时间复杂度O(n^2),空间O(n^2) 3.  以某个元素为中心,分别

leetcode problem 42 -- Trapping Rain Water

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map is represented by a

LeetCode Problem 3.Longest Substring Without Repeating Characters

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.

leetcode problem 6: zigzag word

class Solution { public: string convert(string s, int numRows) { if (numRows == 1){ return s; } string out; for (int i = 0; i < numRows; ++i){ int j = 0; while (true){ int pos = -1; if (j % 2 == 0){ pos = j * (numRows - 1) + i; } else if (i != 0 &&