本部分是非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. 使用scala通过JNI技术调用c++代码

    scala代码编写 Sample1.scala class Sample1 { // --- Native methods @native def intMethod(n: Int): Int def ...

  2. codeforces626F

    CF626F Group Projects  有n个学生,每个学生有一个能力值ai.现在要把这些学生分成一些(任意数量的)组,每一组的“不和谐度”是该组能力值最大的学生与能力值最小的学生的能力值的差. ...

  3. pmm的安装,这里推荐下载官方提供的脚本,直接执行这个脚本就可以完成安装

    脚本内容如下: #!/bin/sh set -o errexit set -o xtrace root_is_needed='no' check_command() { command -v &quo ...

  4. Java学习日记基础(五)——类、对象之this、静态变量(类变量)、静态方法(类方法)、四大特征

    this 赵本山问奥尼尔:“我的爸爸的爸爸是谁?” 奥尼尔:“不知道” 赵本山:“你傻啊,是我爷爷” 奥尼尔回去问科比:“我的爸爸的爸爸是谁?” 科比:“不知道” 奥尼尔:”你傻啊,是赵本山的爷爷“ ...

  5. cas系列-cas REST协议(三)

    cas的rest协议 cas还支持rest协议方式进行访问,格式和参数如下: 1. 获取TGT 请求方式,路径,http协议及请求参数: POST /cas/v1/tickets HTTP/1.0 u ...

  6. WPF_AutoCompleteBox智能提示_Xml读写

    效果图 1.需要引用的DLL 2. Window.xaml <Window x:Class="自己的命名空间" xmlns="http://schemas.micr ...

  7. 针对于linux初学者的学习(摘自网络端)

    一. 选择适合自己的Linux发行版谈到linux的发行版本,太多了,可能谁也不能给出一个准确的数字,但是有一点是可以肯定的,linux正在变得越来越流行, 面对这么多的Linux 发行版,打算从其他 ...

  8. [Ubuntu] sudo apt-get update指令执行失败

    系统:Ubuntu 16.04 LTS 安装软件前执行 sudo apt-get update 更新,但是提示失败如下: 网上搜索后,因为连接不到 US 的服务器,所以更新失败,需要在系统设置中将源服 ...

  9. legend3---18、第一阶段代码完成

    legend3---18.第一阶段代码完成 一.总结 一句话总结: 看起来麻烦或者自己因为厌烦不想做的,其实硬着头皮来做,一下子就做完了 1.layer_mobile的loading层和关闭loadi ...

  10. Flutter移动电商实战 --(48)详细页_详情和评论的切换

    增加切换的效果,我们主要是修改这个地方 这样我们的评论的内容就显示出来了 最终代码 details_web.dart import 'package:flutter/material.dart'; i ...