第2章:LeetCode--第二部分
本部分是非Top的一些常见题型及不常见题
LeetCode -- Longest Palindromic Substring
class Solution {
public:
int isPalindromic(string s, int size, int len){
for(int i=; i+size<=len; i++){
//check s[i]~s[i+size-1]
int j = ;
while(j<size/){
if(s[i+j] != s[i+size--j])
break;
j+=;
}
if(j==size/) return i;
}
return -;
}
string longestPalindrome(string s) {
int len = s.size();
for(int sublen = len; sublen>; sublen--){
int ret = isPalindromic(s, sublen, len);
if(ret>=) return s.substr(ret, sublen);
}
return s;
}
};
//other guy's solution
class Solution {
public:
string longestPalindrome(string s)
{
int sLen = s.length(), maxLen = , maxStart = ;
int i = , l = , r = , len = ;
while(i<=sLen-maxLen/)
{
l = r = i;
while(r<sLen- && s[r+]==s[r]) r++;
i = r+;
while(l> && r<sLen- && s[r+]==s[l-]) l--, r++;
len = r-l+;
if(maxLen < len) maxLen = len, maxStart = l;
}
return s.substr(maxStart, maxLen);
}
}; LeetCode--. Valid Parentheses
class Solution {
public:
bool isValid(string s) {
vector<char> OpStr;
int len = s.length();
char ee;
for(int i=; i<len; i++){
switch (s[i]){
case '(':
case '[':
case '{':
OpStr.push_back(s[i]);
break;
case ')':
if(OpStr.empty()) return false;
ee = OpStr.back();
if(ee == '(')
OpStr.pop_back();
else
return false;
break;
case ']':
if(OpStr.empty()) return false;
ee = *(OpStr.end()-);
if(ee == '[')
OpStr.pop_back();
else
return false;
break;
case '}':
if(OpStr.empty()) return false;
ee = *(OpStr.rbegin());
if(ee == '{')
OpStr.pop_back();
else
return false;
break;
default:
return false;
}
}
if(OpStr.empty())
return true;
else
return false;
}
}; ===Other Guy's Opetimazed solution====
#include <stack>
class Solution {
public:
bool isValid(string s) {
stack<char> paren;
for (char& c : s) {
switch (c) {
case '(':
case '{':
case '[': paren.push(c); break;
case ')': if (paren.empty() || paren.top()!='(') return false; else paren.pop(); break;
case '}': if (paren.empty() || paren.top()!='{') return false; else paren.pop(); break;
case ']': if (paren.empty() || paren.top()!='[') return false; else paren.pop(); break;
default: ; // pass
}
}
return paren.empty() ;
}
};
=======
bool isValid(string s) {
stack<char> st;
for(char c : s){
if(c == '('|| c == '{' || c == '['){
st.push(c);
}else{
if(st.empty()) return false;
if(c == ')' && st.top() != '(') return false;
if(c == '}' && st.top() != '{') return false;
if(c == ']' && st.top() != '[') return false;
st.pop();
}
}
return st.empty(); //LeetCode -- 73. Set Matrix Zeroes
//https://www.cnblogs.com/feliz/p/11059445.html LeetCode -- . Valid Number
.Skip leading spaces.
.Skip sign bit.
.Integer, decimal point and fractional parts (make sure at least one digit exists)
.Exponential bit. (There may be sign bits again and make sure there is digit following)
.Skip following spaces.
.Make sure that's the end. bool isNumber(string s)
{
int n = s.size();
if(n == ) return false; int i = ;
//Skip leading spaces.
while(s[i] == ' ') i++; //Significand
if(s[i] == '+' || s[i] == '-') i++; int cnt = ;
//Integer part
while(isdigit(s[i]))
{
i++;
cnt++;
}
//Decimal point
if(s[i] == '.') i++;
//Fractional part
while(isdigit(s[i]))
{
i++;
cnt++;
}
if(cnt == ) return false; //No number in front or behind '.' //Exponential
if(s[i] == 'e')
{
i++;
if(s[i] == '+' || s[i] == '-') i++;
if(!isdigit(s[i])) return false; //No number follows
while(isdigit(s[i])) i++;
} //Skip following spaces;
while(s[i] == ' ') i++; return s[i] == '\0';
}
//动态规划问题: 求一个数组里子数组的最大和
//暴力算法:
int GetMaxAddOfArray(int *arr, int sz)
{
int SUM = arr[];
for (int i = ; i < sz; i++)
{
int subOfArr = ; //临时最大值
for (int j = i; j < sz; j++)
{
subOfArr += arr[j]; if (subOfArr > SUM)
{
SUM = subOfArr;
}
}
}
return SUM;
}
---------------------
动态规划思想
思路分析
、状态方程 : max( dp[ i ] ) = getMax( max( dp[ i - ] ) + arr[ i ] ,arr[ i ] ) 、上面式子的意义是:我们从头开始遍历数组,遍历到数组元素 arr[ i ] 时,连续的最大的和 可能为 max( dp[ i - ] ) + arr[ i ] ,也可能为 arr[ i ] ,做比较即可得出哪个更大,取最大值。时间复杂度为 n。 代码实现
int GetMax(int a, int b) //得到两个数的最大值
{
return (a) > (b) ? (a) : (b);
} int GetMaxAddOfArray(int* arr, int sz)
{
if (arr == NULL || sz <= )
return ; int Sum = arr[]; //临时最大值
int MAX = arr[]; //比较之后的最大值 for (int i = ; i < sz; i++)
{
Sum = GetMax(Sum + arr[i], arr[i]); //状态方程 if (Sum >= MAX)
MAX = Sum;
}
return MAX;
} int main()
{
int array[] = { , , -, , , , -, , - };
int sz = sizeof(array) / sizeof(array[]);
int MAX = GetMaxAddOfArray(array, sz);
cout << MAX << endl;
return ;
}
第2章:LeetCode--第二部分的更多相关文章
- LeetCode第二天&第三天
leetcode 第二天 2017年12月27日 4.(118)Pascal's Triangle JAVA class Solution { public List<List<Integ ...
- JavaScript笔记(第一章,第二章)
JavaScript笔记(第一章,第二章) 第一章: <meta http-equiv="Content-Type" content="text/html; cha ...
- 《LINUX内核设计与实现》读书笔记之第一章和第二章
一.第一章 1. Unix内核的特点简洁:仅提供系统调用并有一个非常明确的设计目的抽象:几乎所有东西都被当做文件可移植性:使用C语言编写,使得其在各种硬件体系架构面前都具备令人惊异的移植能力进程:创建 ...
- Linux内核分析 读书笔记 (第一章、第二章)
第一章 Linux内核简介 1.1 Unix的历史 Unix很简洁,仅仅提供几百个系统调用并且有一个非常明确的设计目的. 在Unix中,所有东西都被当做文件,这种抽象使对数据和对设备的操作是通过一套相 ...
- 【第二章】 第二个spring-boot程序
上一节的代码是spring-boot的入门程序,也是官方文档上的一个程序.这一节会引入spring-boot官方文档推荐的方式来开发代码,并引入我们在spring开发中service层等的调用. 1. ...
- 算法导论 第一章and第二章(python)
算法导论 第一章 算法 输入--(算法)-->输出 解决的问题 识别DNA(排序,最长公共子序列,) # 确定一部分用法 互联网快速访问索引 电子商务(数值算 ...
- Unity 游戏框架搭建 2019 (九~十二) 第一章小结&第二章简介&第八个示例
第一章小结 为了强化教程的重点,会在合适的时候进行总结与快速复习. 第二章 简介 在第一章我们做了知识库的准备,从而让我们更高效地收集示例. 在第二章,我们就用准备好的导出工具试着收集几个示例,这些示 ...
- leetcode 第二题Add Two Numbers java
链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...
- leetcode第二题--Median of Two Sorted Arrays
Problem:There are two sorted arrays A and B of size m and n respectively. Find the median of the two ...
- Python 3标准库课件第一章(第二版)
第一章文本1.1 string:文本常量和模板1.2 textwrap:格式化文本段落1.3 re:正则表达式1.4 difflib:比较序列str类,string.Templatetextwrap ...
随机推荐
- P1021 邮票面值设计——搜索+完全背包
P1021 邮票面值设计 题目意思是你最多用n张邮票,你可以自己设定k种邮票的面值,每种邮票数量无穷,你最多能用这k种邮票在不超过n张的情况下,组合成的价值要求是从1开始连续的, 求最大能连续到多少: ...
- 使用jsonpath解析多层嵌套的json响应信息
Python自带的json库可以把请求转为字典格式, 但在多层嵌套的字典中取值往往要进行多次循环遍历才能取到相应的数据, 如: res_dict = { "code": 0, &q ...
- 三十三、DNS资源记录类型和请求流程
DNS分布均衡(Load balance)的实现 在上级数据库中写两条记录(同一个名字对应对个IP时),DNS会自动将请求基于轮循方式,分给每个DNS服务器 例如: 第一次将请求给第一个DNS,第二次 ...
- Mysql 生成指定时间段内的所有日期
DELIMITER $$ DROP PROCEDURE IF EXISTS create_calendar $$ CREATE PROCEDURE create_calendar (s_date DA ...
- OAuth 2.0 的四种方式
上一篇文章介绍了 OAuth 2.0 是一种授权机制,主要用来颁发令牌(token).本文接着介绍颁发令牌的实务操作. 下面我假定,你已经理解了 OAuth 2.0 的含义和设计思想,否则请先阅读这个 ...
- contos的apt-get安装
本文链接:https://blog.csdn.net/lesz_s/article/details/88732500 首先contos的软件包管理是yum但是yum包好多都没有 比如我想装的slwge ...
- legend3---阿里云服务器配置多个网站项目
legend3---阿里云服务器配置多个网站项目 一.总结 一句话总结: 就是和本机上面的一样,多个域名可以指向同一个ip,配置apache的时候记得ServerName配置域名,不要直接整ip 二. ...
- Windows 下 把EXE 程序变成服务运行
1. 下载 instsrv.exe 和 srvany.exe 我下载的地址 [点击打开链接][https://www.cr173.com/soft/64394.html] 2. cmd cd ...
- vue-cli及vue-router
1.单文件组件 (1)问题: 使用 Vue.component 来定义全局组件,紧接着用 new Vue({ el: ‘#container ‘}) 在每个页面内指定一个容器元素.当在更复杂的项 ...
- MediaPlayer: 在不同控件之间实现视频的无缝切换的方法
最近使用MediaPlayer + TextureView 实现了一个视频播放器,并且实现了它的横竖屏切换的效果,唯一美中不足的是在横竖屏切换的时候画面会卡顿一下,虽然也不影响播放,但是怕测试会报Bu ...