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. PTA 5-12 排序 (25分)

    给定NN个(长整型范围内的)整数,要求输出从小到大排序后的结果. 本题旨在测试各种不同的排序算法在各种数据情况下的表现.各组测试数据特点如下: 数据1:只有1个元素: 数据2:11个不相同的整数,测试 ...

  2. ASP.NET MVC- JSON ,Jquery, State management and Asynch controllers

    一.JSON  MVC And JQuery In case you are new to JSON please read this before moving ahead with this la ...

  3. iOS开发 落地消息多的处理办法(仅供参考)

    1.首先要知道一点,你的消息储存是用数据库储存的! 看了一下微信和qq的消息处理,一般情况下第三方(亲加,容云,环信都会有本地的数据库)处理过的! 但是我发现,最近一个需求要求开发@"消息已 ...

  4. android_ViewPager_实现导航页

    android_ViewPager_实现导航页 既然是实现导航页的效果,那么我们肯定是要实现ViewPager的 要实现的效果如下 1.用户进入欢迎页面 2.判断是否是第一次进入,如果是,则进入导航页 ...

  5. YII2安装中遇到的错误解决Calling unknown method: yii\web\UrlManager::addRules()

    安装好YII2 后出现 例如以下图错误提示: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenF0c3g=/font/5a6L5L2T/fontsize/ ...

  6. SensorThread线程

    SensorThread  && createEventQueue http://www.csdn.com/html/itweb/20131101/200375.htm_123 htt ...

  7. spring mvc[转]

    Spring 注解学习手札(一) 构建简单Web应用 Spring 注解学习手札(二) 控制层梳理 Spring 注解学习手札(三) 表单页面处理 Spring 注解学习手札(四) 持久层浅析 Spr ...

  8. 真相:中国版BBB用USB连电脑没有盘符的根本原因分析

    很多网友在问:为什么中国版的装完驱动插上板子没有显示端口号和69M的盘符??楼主发现,在开机启动的时候,加载g_multi模块时出现错误提示 invalid argument.         Emb ...

  9. 解锁Dagger2使用姿势(一)

    毫无疑问,Dagger2的 上手是有门槛的,有门槛是因为它里边的概念多,用起来复杂,可是一旦你学会了Dagger2的使用,你一定会爱不释手的.与ButterKnife和AndroidAnnotatio ...

  10. PHP第四章数组2

    $str =array("dd"=>"d","dc"=>"ds","dd"=>&q ...