leetcode ----Trie/stack专题
一:Implement Trie (Prefix Tree)
题目:
Implement a trie with insert
, search
, and startsWith
methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z
.
分析:此题是典型的trie树。能够參见:http://blog.csdn.net/lu597203933/article/details/44227431
代码:
class TrieNode {
public:// Initialize your data structure here.TrieNode() {for(int i = 0; i < 26; i++)next[i] = NULL;isString = false;}TrieNode *next[26];bool isString;
};class Trie {
public:Trie() {root = new TrieNode();}// Inserts a word into the trie.void insert(string s) {TrieNode *p = root;for(int i = 0; i < s.size(); i++){if(p->next[s[i]-'a'] == NULL){p->next[s[i]-'a'] = new TrieNode();}p = p->next[s[i]-'a'];}p->isString = true;}// Returns if the word is in the trie.bool search(string key) {TrieNode *p = root;for(int i = 0; i < key.size(); i++){if(p == NULL) return false;p = p->next[key[i]-'a'];}if(p == NULL || p->isString == false) return false;return true;}// Returns if there is any word in the trie// that starts with the given prefix.bool startsWith(string prefix) {TrieNode *p = root;for(int i = 0; i <= prefix.size(); i++){if(p == NULL) return false;p = p->next[prefix[i]-'a'];}return true;}private:TrieNode* root;
};// Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");
二:Basic Calculator
题目:
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open (
and closing parentheses )
, the plus +
or minus sign -
, non-negative integers and empty spaces .
You may assume that the given expression is always valid.
Some examples:
"1 + 1" = 2 " 2-1 + 2 " = 3 "(1+(4+5+2)-3)+(6+8)" = 23
分析:这道题能够将括号中面的看做是负负得正,这样用sign记录当前数字前面得符号。是+为1,是-为-1,。然后还要它所在的括号深度的符号,用stack来标记。
同一时候num = num *10 + c - '0'。, 也给出了正向计算一个字符串比方“12342”的数值方法。。。
class Solution {
public:int calculate(string s) {int sign = 1; // 当前元素前是+还是-stack<char> st; // 主要是为了考虑括号的深度, 括号前面是+ 则为1否则为0st.push(1); int ans = 0;int tmp = 0;for(int i = 0; i < s.size(); i++){char c = s[i];if(isdigit(c)){ // 假设是数字 则保存起来tmp = tmp * 10 + s[i] - '0'; }else if(c == '-' || c == '+'){ ans += tmp * sign * st.top(); // 由当前符号和括号外面的符号两者决定!sign = (c=='+' ? 1 : -1);tmp = 0;}else if(c == '('){st.push(sign*st.top()); // 当前括号内元素的符号由其前面的各个外层括号符号决定sign = 1; // 括号后面首个是+}else if(c ==')'){ans += tmp *sign * st.top();st.pop();tmp = 0;sign = 1;}}ans += tmp * sign * st.top();return ans;}
};
转载于:https://www.cnblogs.com/brucemengbm/p/7191171.html
leetcode ----Trie/stack专题相关推荐
- LeetCode Hot100 ---- 二叉树专题
树 力扣102:二叉搜索树的层次遍历 力扣105:从前序和中序重构二叉树 力扣108:将有序数组转化为二叉搜索树 力扣110:平衡二叉树 力扣113:路径总和 力扣124:二叉树的最大路径和 力扣13 ...
- 花花酱leetcode 题目——搜索专题
在刷完极客时间的算法题目之后,偶然看到了花花酱关于leetcode进入千题时代的一些刷题看法.决定跟着他的思路继续刷题. 要刷多少题 1 每个类型 10-20 如何刷题 1 同类型题目一起刷 2 看代 ...
- 每周leetcode - 02链表专题 237/83/234/138/92/142/Offer 22/148/23/24/147/86/61/328/2/Offer06
文章目录 leetcode - 237. 删除链表中的节点 leetcode - 83. 删除排序链表中的重复元素 leetcode - 234. 回文链表 leetcode - 138. 复制带随机 ...
- LeetCode (二分小专题)33搜索旋转排序数组34在排序数组中查找元素的第一个和最后一个位置35搜索插入位置
前言 国庆前最后一次打卡,国庆后继续开启,公众号bigsai回复进群欢迎加入打卡,如有帮助记得点赞收藏. 近期打卡记录: LeetCode 32最长有效括号(困难) (本周) LeetCode 30串 ...
- Leetcode - 广度优先遍历专题
> 基础 1. 广度遍历优先是从给定的root节点开始,逐层次的向下访问各个节点: 2. 实现的方式是通过队列的先进先出,将从root节点开始的左孩子和右孩子压入到队列中,并顺序取出: 3. 由 ...
- Leetcode Trie Conclusion
Implement Trie (Prefix Tree) 208. Implement Trie (Prefix Tree) Add and Search Word - Data structure ...
- LeetCode -前缀和专题
文章目录 一.前缀和 二.刷题 一.前缀和 二.刷题 LeetCode 724. 寻找数组的中心下标 原题链接 class Solution {public:int pivotIndex(vector ...
- LeetCode Hot100 ---- 排序专题
排序算法 快速排序 插入排序 归并排序 希尔排序 堆排序 快速排序 快速排序使用分治法(Divide and conquer)策略来把一个序列(list)分为较小和较大的2个子序列,然后递归地排序两个 ...
- LeetCode Hot100 ---- 链表专题专题
链表 力扣109:将有序链表转化为二叉搜素树 力扣141:环形链表判断是否有环 力扣142:环形链表检测入口位置 力扣143:重拍链表 力扣160:相交链表 力扣206:反转链表 力扣21:合并两个有 ...
- LeetCode Hot100 ---- 动态规划专题
动态规划问题 力扣121:买卖股票(一次交易) 力扣122:买卖股票(多次交易) 力扣134:加油站 力扣309:买卖股票(包含冷冻时间) 力扣322:零钱兑换 力扣518:零钱兑换 力扣53:最大子 ...
最新文章
- GdiPlus[44]: IGPGraphics (三) 关于文本绘制续 - IGPStringFormat
- php 实现tab切换_微信小程序实例:实现顶部tab切换以及滑动切换时导航栏会随着移动的效果(代码)...
- 微软安全管理解决方案 为企业安全保驾护航
- SHELL脚本取系统当前年月日问题 (去0)
- linux查看apache端口,linux系统下Apache服务启动时80端口报错
- 上线4年从畅销Top 200到Top 20,这款刀塔传奇like卡牌正在美国市场逆流而上
- Symantec改变了产品下载方式
- 机器学习统计学相关书籍
- har文件解析工具_嵌入式MCU也能跑AI?STM32 Cube.AI工具包使用初探
- glassfish 初次使用 (介绍・目录结构・注意点・基本命令・控制台・eclipse插件安装)
- 先搭云安全框架 再谈云落地
- Python统计Java代码行数
- AMEsim2019.2的安装和matlab2019的联合仿真
- 编译Openwrt(LEDE)for K3固件详细指南
- Openv Switch 完全使用手册
- pix2pixHD总结
- 【3. Redis高级特性】
- Verilog上机实验(二):多功能计数器
- c++字符串中元音字母转置
- Android开源框架:Retrofit