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. IOS网络多线程-GCD

    Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法. dispatch queue分成以下三种: 1)运行在主线程的Main queue,通过dispat ...

  2. jQuery事件绑定的最佳实践

    如果你经常使用jQuery,那么你也许很熟悉事件绑定.这是很基本的东西,但是深入一点,你就能够找到机会让你事件驱动的代码变得不太零碎,并且更容易管理. 更好的选择器策略 让我们从基础的例子开始.下面的 ...

  3. C#下的 Emgu CV

    Emgu CV下载地址 http://sourceforge.net/projects/emgucv/files/ 找最新的下就行了,傻瓜式安装,选择目录后自动完成安装,然后提示安装VS2008和VS ...

  4. Nodejs实现代理服务器配置

    var net = require('net'); var local_port = 8893; //在本地创建一个server监听本地local_port端口 net.createServer(fu ...

  5. (翻译)什么是Java的永久代(PermGen)内存泄漏

    http://www.codelast.com/?p=7248 转载请注明出处:http://www.codelast.com/ 本文是我对这篇文章的翻译:What is a PermGen leak ...

  6. Hibernate继承映射

    在面向对象的程序领域中,类与类之间是有继承关系的,例如Java世界中只需要extends关键字就可以确定这两个类的父子关系,但是在关系数据库的世界中,表与表之间没有任何关键字可以明确指明这两张表的父子 ...

  7. excel导入数据库iis设置

    导入成功以后,基本这个小项目的所有功能都开发完成了,请IT部门帮我设定了一个固定IP,我以本机作为服务器,在本机IIS上发布了一个测试版,结果上传Excel数据报错, 错误信息“未在本地计算机上注册“ ...

  8. jstl前台Jsp日期格式化

    1. 引入fmt标签 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %&g ...

  9. [Javascript] Create an Array concatAll method

    In addition to flat Arrays, programmers must often deal with nested Arrays. For example let's say we ...

  10. leecode 每日解题思路 127-Factorial Trailing Zeroes

    原题描述: 原题地址: Factorial Trailing Zeroes 题目描述很直接, 给出一个整数N, 求这个N的阶乘后尾有几个零.(要求O(logN)时间复杂度) 个人思路: 一开始,最简单 ...