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用户需要了解的命令行技能
最近在Quora上看到一个问答题目,关于在高效率Linux用户节省时间Tips. 将该题目的回答进行学习总结,加上自己的一些经验,记录如下,方便自己和大家参考. 下面介绍的都是一些命令行工具,这些工具 ...
- [置顶] 漫谈SOA(面向服务架构)
面向服务架构的思想在整个软件的架构中已经不是什么新鲜的东西.我简单的认为服务化是模块化的延伸,所以服务化有着和模块化类似的优点和缺点.这里不再讨论这些服务定义服务与服务之间的通信协议(像WSDL等等) ...
- 1、IIS常见的的问难及解决方法
配置IIS 7.5 出现的问题及解决办法: 1.问题: CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Tempor ...
- UOJ#191. 【集训队互测2016】Unknown
题意:维护一个数列,每个元素是个二维向量,每次可以在后面加一个元素或者删除一个元素.给定P(x,y),询问对于[l,r]区间内的元素$S_i$,$S_i \times P$的最大值是多少. 首先简单地 ...
- JavaScript- 获得TreeView CheckBox里选中项的值
获得TreeView CheckBox里选中项的值,对JSDOM控制还不是很熟,感觉不太容易.试了很多次终于成功了. 代码如下 <body> <form id="form1 ...
- FZOJ2110 star(DFS)
Overpower often go to the playground with classmates. They play and chat on the playground. One day, ...
- POJ 3670 , 3671 LIS
题意:两题意思差不多,都是给你一个序列,然后求最少需要改变多少个数字,使得成为一个最长不升,或者最长不降子序列. 当然3671是只能升序,所以更简单一点. 然后就没有什么了,用二分的方法求LIS即可. ...
- mysql 源代码学习 博客 [lock..]
http://blog.chinaunix.net/uid/28364803/cid-168292-list-1.html
- 标准I/O库之标准I/O的效率
程序清单5-1 用getc和putc将标准输入复制到标准输出 #include "apue.h" int main( void ) { int c; while(( c = get ...
- c语言学习
#include <stdio.h>void sum_diff(int *n1,int *n2,int *sum,int *diff){ *sum=*n1+*n2; *diff=(*n1& ...