本部分是非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--第二部分的更多相关文章

  1. LeetCode第二天&第三天

    leetcode 第二天 2017年12月27日 4.(118)Pascal's Triangle JAVA class Solution { public List<List<Integ ...

  2. JavaScript笔记(第一章,第二章)

    JavaScript笔记(第一章,第二章) 第一章: <meta http-equiv="Content-Type" content="text/html; cha ...

  3. 《LINUX内核设计与实现》读书笔记之第一章和第二章

    一.第一章 1. Unix内核的特点简洁:仅提供系统调用并有一个非常明确的设计目的抽象:几乎所有东西都被当做文件可移植性:使用C语言编写,使得其在各种硬件体系架构面前都具备令人惊异的移植能力进程:创建 ...

  4. Linux内核分析 读书笔记 (第一章、第二章)

    第一章 Linux内核简介 1.1 Unix的历史 Unix很简洁,仅仅提供几百个系统调用并且有一个非常明确的设计目的. 在Unix中,所有东西都被当做文件,这种抽象使对数据和对设备的操作是通过一套相 ...

  5. 【第二章】 第二个spring-boot程序

    上一节的代码是spring-boot的入门程序,也是官方文档上的一个程序.这一节会引入spring-boot官方文档推荐的方式来开发代码,并引入我们在spring开发中service层等的调用. 1. ...

  6. 算法导论 第一章and第二章(python)

    算法导论 第一章 算法     输入--(算法)-->输出   解决的问题     识别DNA(排序,最长公共子序列,) # 确定一部分用法     互联网快速访问索引     电子商务(数值算 ...

  7. Unity 游戏框架搭建 2019 (九~十二) 第一章小结&第二章简介&第八个示例

    第一章小结 为了强化教程的重点,会在合适的时候进行总结与快速复习. 第二章 简介 在第一章我们做了知识库的准备,从而让我们更高效地收集示例. 在第二章,我们就用准备好的导出工具试着收集几个示例,这些示 ...

  8. leetcode 第二题Add Two Numbers java

    链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...

  9. 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 ...

  10. Python 3标准库课件第一章(第二版)

    第一章文本1.1 string:文本常量和模板1.2 textwrap:格式化文本段落1.3 re:正则表达式1.4  difflib:比较序列str类,string.Templatetextwrap ...

随机推荐

  1. Hadoop hadoop(2.9.0)---uber模式(小作业“ubertask”优化)

    前言: 在有些情况下,运行于Hadoop集群上的一些mapreduce作业本身的数据量并不是很大,如果此时的任务分片很多,那么为每个map任务或者reduce任务频繁创建Container,势必会增加 ...

  2. legend3---阿里云服务器配置多个网站项目

    legend3---阿里云服务器配置多个网站项目 一.总结 一句话总结: 就是和本机上面的一样,多个域名可以指向同一个ip,配置apache的时候记得ServerName配置域名,不要直接整ip 二. ...

  3. 从Cortex-M3的MSP 和PSP谈Linux能否在中断中使用Sleep

    1.Cortex-M3 的PSP和MSP 曾经在STM32上使用过RT thread和uC/OS,对于任务切换代码一直是一知半解,没有自己手动写出来过,对于任务切换后的ORR   LR, LR, #0 ...

  4. 等待 Redis 应答

    https://zhuanlan.zhihu.com/p/58608323 mq消息合并:由于mq请求发出到响应的时间,即往返时间, RTT(Round Time Trip),每次提交都要消耗RTT, ...

  5. android: Context引起的内存泄露问题

    错误的使用Context可能会导致内存泄漏,典型的例子就是单例模式时引用不合适的Context. public class SingleInstance { private static Single ...

  6. 【转载】 什么是P问题、NP问题和NPC问题

    原文地址: http://www.matrix67.com/blog/archives/105 转载地址: https://www.cnblogs.com/marsggbo/p/9360324.htm ...

  7. List的remove()方法的三种正确打开方式

    转: java编程:List的remove()方法的三种正确打开方式! 2018年08月12日 16:26:13 Aries9986 阅读数 2728更多 分类专栏: leetcode刷题   版权声 ...

  8. [ML] Bayesian Linear Regression

    热身预览 1.1.10. Bayesian Regression 1.1.10.1. Bayesian Ridge Regression 1.1.10.2. Automatic Relevance D ...

  9. 对Mysql数据表本身进行操作

    创建实验环境 mysql> create database test_db; Query OK, 1 row affected (0.00 sec) mysql> use test_db; ...

  10. python基础之线程、进程、协程

    线程 线程基础知识 一个应用程序,可以多进程.也可以多线程. 一个python脚本,默认是单进程,单线程的. I/O操作(音频.视频.显卡操作),不占用CPU,所以: 对于I/O密集型操作,不会占用C ...