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 = ;
int index = ;
int len = ;
int left = ;
memset(m_hashTable, , sizeof(m_hashTable));
for (auto elem : s) {
if (m_hashTable[elem] != && 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[];
};

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 / + );
else
return (findKth(A, m, B, n, total / ) +
findKth(A, m, B, n, total / + )) / 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 == )
return B[k-];
if (k == )
return min(A[], B[]); int posA = min(k/, m), posB = k - posA;
if (A[posA - ] < B[posB - ])
return findKth(A + posA, m - posA, B, n, k - posA);
else if (A[posA - ] > B[posB - ])
return findKth(A, m, B + posB, n - posB, k - posB);
else
return A[posA-];
}
};

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

  1. LeetCode Problem 90. Subsets II

    python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(sel ...

  2. leetcode problem 42 -- Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  3. leetcode problem (5) Longest Palindromic Substring

    最长回文子串: 1. 暴力搜索   时间复杂度O(n^3) 2. 动态规划 dp[i][j] 表示子串s[i…j]是否是回文 初始化:dp[i][i] = true (0 <= i <= ...

  4. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

  5. LeetCode Problem 2:Two Sum

    描述: Given an array of integers, find two numbers such that they add up to a specific target number. ...

  6. LeetCode Problem 9:Palindrome Number回文数

    描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

  7. LeetCode Problem 169: Majority Element查找多数元素

    描述:Given an array of size n, find the majority element. The majority element is the element that app ...

  8. leetcode problem sum

    2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...

  9. leetcode problem 41 -- First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

随机推荐

  1. 教程-Delphi设置功能表

    1.锁定窗体上的控件,禁止移动位置 D7-Edit>Lock Controls 2.设置控件永久显示名字 D7-Tools>Environment Options>Designer& ...

  2. Video Surveillance - POJ 1474(判断是否存在内核)

    题目大意:询问是否在家里装一个监视器就可以监控所有的角落. 分析:赤裸裸的判断多边形内核题目. 代码如下: #include<iostream> #include<string.h& ...

  3. sqlplus 连接数据库报错SP2-0642: SQL*Plus internal error state 2130, context 0:0:0

    sqlplus 连接数据库报错SP2-0642: SQL*Plus internal error state 2130, context 0:0:0 问题描述: 使用sqlplus客户端登录数据库,报 ...

  4. JQ限制输入字数,并提示剩余字数

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. PTA 08-图7 公路村村通 (30分)

    现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本. 输入格式: 输入数据包括城镇数目正整数NN(\le 1000≤1000)和候选道 ...

  6. C#中如何操作2个list

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:C#中如何操作2个list.

  7. eclipse加入git工具

    想必如今搞互联网编程的人没有人没有听过编程协作工具git了吧,命令行以下操作很方便.功能也很的强大. 可是对于java编程的人员,eclipse肯定是他们编程的神器(肯定是首选), 那么怎样在ecli ...

  8. Dragons

    http://codeforces.com/problemset/problem/230/A Dragons time limit per test 2 seconds memory limit pe ...

  9. AndroidPN中的心跳检测

    在AndroidPN客户端里存在着心跳检测功能.就是每隔一段时间客户端向服务器端发送一个消息,以检测连接是否正常,发送的消息内容为: <presence id="h09Ke-13&qu ...

  10. c++ 数据持久层研究(一)

    C++ORM框架自动生成代码数据库  用过Java的都知道SSH框架,特别对于数据库开发,Java领域有无数的ORM框架,供数据持久层调用,如Hibernate,iBatis(现在改名叫MyBatis ...