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 = ;
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)的更多相关文章
- LeetCode Problem 90. Subsets II
python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(sel ...
- leetcode problem 42 -- Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- leetcode problem (5) Longest Palindromic Substring
最长回文子串: 1. 暴力搜索 时间复杂度O(n^3) 2. 动态规划 dp[i][j] 表示子串s[i…j]是否是回文 初始化:dp[i][i] = true (0 <= i <= ...
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
- LeetCode Problem 2:Two Sum
描述: Given an array of integers, find two numbers such that they add up to a specific target number. ...
- LeetCode Problem 9:Palindrome Number回文数
描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- LeetCode Problem 169: Majority Element查找多数元素
描述:Given an array of size n, find the majority element. The majority element is the element that app ...
- leetcode problem sum
2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...
- leetcode problem 41 -- First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
随机推荐
- Linux内存寻址之分页机制
在上一篇文章Linux内存寻址之分段机制中,我们了解逻辑地址通过分段机制转换为线性地址的过程.下面,我们就来看看更加重要和复杂的分页机制. 分页机制在段机制之后进行,以完成线性—物理地址的转换过程.段 ...
- jquery实现公告上下滚动显示
js: // JavaScript Documentfunction b(){ t = parseInt(x.css('top')); y.css('top','19px'); x.animate({ ...
- Win+R指令(1)
CMD命令:开始->运行->键入cmd或command(在命令行里可以看到系统版本.文件系统版本)1. appwiz.cpl:程序和功能 2. calc:启动计算器 3. certmgr. ...
- 【开发工具 - Git】之Git常用命令汇总
本文记录了Git Bash中的常用指令. 1 Git操作 git clone XXXXX:将GitHub项目XXXXX克隆到本地 git remote –v:查看远程连接信息 git ch ...
- mysql 5.7新数据库sys解析(一)
mysql5.7增加了sys 系统数据库,通过这个库可以快速的了解系统的元数据信息 这个库确实可以方便DBA发现数据库的很多信息,解决性能瓶颈都提供了巨大帮助 这个库在mysql5.7中是默认存在 ...
- IOS开发之TableView替换默认的checkmark为自定义图像
直接上代码: On cellForRowAtIndexPath: UIButton*button =[UIButton buttonWithType:UIButtonTypeCustom];CGRec ...
- C# 读取 timestamp 时间戳 值为byte[] 类型时转换为时间戳字符串
C# 中如何读取出来的时间戳为 byte[] 类型的话,用以下方式转换成 16进制字符串 string tmpUfts = "0x"+ BitConverter.ToString( ...
- 安装J2EE的SDK报错:could not find the required version of the Java(TM)2 Runtime Environment in '(null)'的解决办法。
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- 解决Ubuntu下sublime中不能输入中文的问题
解决Ubuntu下sublime中不能输入中文的问题 Ubuntu下安装sublime后,不能输入中文,而在其他软件中能正常输入,这是sublime的bug,解决方案是在通过shell在每次运行sub ...
- skynet启动过程_bootstrap
这遍摘自skynet 的wiki skynet 由一个或多个进程构成,每个进程被称为一个 skynet 节点.本文描述了 skynet 节点的启动流程. skynet 节点通过运行 skynet 主程 ...