本部分是非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. ID生成算法(二)

    上一篇文章介绍了一种用雪花算法生成GUID的方法,下面介绍里外一种生成GUID并导出为.txt文件的方法: 话不多少 show you the code ! <!DOCTYPE html> ...

  2. 20191214数组习题之三:报数(附pow函数简单用法)

  3. JRebel for IntelliJ激活

    好久没用jrebel了,跟前端进行项目联调总是有些许改动,还是热部署方便. 目前用的idea版本:IntelliJ IDEA 2019.2 JRebel插件版本:JRebel for IntelliJ ...

  4. hbase单点安装

    系统环境:centos 6 软件包: hbase版本:hbase-1.4.8-bin.tar.gz     下载地址:wget  http://mirror.bit.edu.cn/apache/hba ...

  5. 我的BO之数据保护

    我的BO 1-我的BO之强类型 2-我的BO之数据保护 3-我的BO之状态控制 4-我的BO之导航属性 数据保护指什么 软件的运行离不开数据,数据一般存在对象中.这种对象在 Java 统称为 POJO ...

  6. 【DOS】取某目录下某类型文件信息放入文本

    C:\Users\horn1\Desktop\新建文件夹>dir *.jar >1.txt 这样,所有扩展名为jar的文件信息就送到新建的文本文件1.txt中了.虽然简单,但也是个常用功能 ...

  7. MSP与PSP

    摘抄自Triton.zhang——eeworld 1. MSP和PSP 的含义是Main_Stack_Pointer 和Process_Stack_Pointer,在逻辑地址上他们都是R13 2. 权 ...

  8. Locust - A modern load testing framework https://locust.io/

    Locust - A modern load testing frameworkhttps://locust.io/

  9. 解决com.android.support版本冲突问题

    原文:https://www.jianshu.com/p/0fe985a7e17e 项目中不同Module的support包版本冲突怎么办? 只需要将以下代码复制到每个模块的build.gradle( ...

  10. 分享调试SI4432的一些小经验(转)

    分享调试SI4432的一些小经验 最近使用 STM8F103 + SI4432 调无线,遇到问题不少,此处有参考过前辈的经验: 1.硬件把板给到我时USB烧录线带供电(5V),此供电接到LDO输出,就 ...