心情还是有问题,保持每日更新,只能如此了。

Problem

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

Example:

Given binary tree {,,,#,#,,},

····
/ \ / \ Result return its level order traversal as: [
[],
[,],
[,]
]
Code /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int HeightOfTree(TreeNode* root) {
int result = ;
if (root == NULL) {
return result;
}
int left = HeightOfTree(root->left) + ;
int right = HeightOfTree(root->right) + ;
result = left > right ? left : right;
return result;
}
void calLevelOrder(TreeNode* root, int level, vector<vector<int> >& result) {
if (root == NULL) {
return;
}
result[level].push_back(root->val);
calLevelOrder(root->left, level + , result);
calLevelOrder(root->right, level + , result);
}
vector<vector<int> > levelOrder(TreeNode* root) {
vector<vector<int> > result;
vector<int> v;
if (root == NULL) {
return result;
}
int height = HeightOfTree(root);
for (int i = ; i < height; i++) {
result.push_back(v);
}
calLevelOrder(root, , result);
return result;
}
};
Problem1

Write a function that takes a string as input and returns the string reversed.

Example:

Given s = "hello", return "olleh".

Code

class Solution {
public:
string reverseString(string s) {
if (s.size() &lt;= ) {
return s;
}
int i = , j = s.size() - ;
char ch;
while (i &lt; j) {
ch = s[i];
s[i] = s[j];
s[j] = ch;
i++;
j--;
}
return s;
}
};
Problem2 Write a function that takes a string as input and reverse only the vowels of a string. Example : Given s = "hello", return "holle". Example : Given s = "leetcode", return "leotcede". Code class Solution {
public:
string reverseVowels(string s) {
if (s.size() &lt;= ) {
return s;
}
int i = , j = s.size() - ;
char ch;
while (i &lt; j) {
while(i &lt; j && s[i] != &#;a&#; && s[i] != &#;e&#; && s[i] != &#;i&#; && s[i] != &#;o&#; && s[i] != &#;u&#;
&& s[i] != &#;A&#; && s[i] != &#;E&#; && s[i] != &#;I&#; && s[i] != &#;O&#; && s[i] != &#;U&#;) {
i++;
}
while(i &lt; j && s[j] != &#;a&#; && s[j] != &#;e&#; && s[j] != &#;i&#; && s[j] != &#;o&#; && s[j] != &#;u&#;
&& s[j] != &#;A&#; && s[j] != &#;E&#; && s[j] != &#;I&#; && s[j] != &#;O&#; && s[j] != &#;U&#;) {
j--;
}
ch = s[i];
s[i] = s[j];
s[j] = ch;
i++;
j--;
}
return s;
}
};
说明 与上面同样的逻辑,只是出现特定的字符才交换位置

PS:存货快用完了。。。。

leetcode简单题目两道(4)的更多相关文章

  1. leetcode简单题目两道(2)

    Problem Given an integer, write a function to determine if it is a power of three. Follow up: Could ...

  2. leetcode简单题目两道(3)

    本来打算写redis的,时间上有点没顾过来,只能是又拿出点自己的存货了. Problem Given an array nums, write a function to move all 's to ...

  3. leetcode简单题目两道(5)

    Problem Given an integer (signed bits), write a function to check whether it . Example: Given num = ...

  4. leetcode简单题目两道(1)

    Problem: You are playing the following Nim Game with your friend: There is a heap of stones on the t ...

  5. CTF中关于XXE(XML外部实体注入)题目两道

    题目:UNCTF-Do you like xml? 链接:http://112.74.37.15:8008/ hint:weak password (弱密码) 1.观察后下载图片拖进WINHEX发现提 ...

  6. 两道面试题,带你解析Java类加载机制

    文章首发于[博客园-陈树义],点击跳转到原文<两道面试题,带你解析Java类加载机制> 在许多Java面试中,我们经常会看到关于Java类加载机制的考察,例如下面这道题: class Gr ...

  7. 【转】两道面试题,带你解析Java类加载机制(类初始化方法 和 对象初始化方法)

    本文转自 https://www.cnblogs.com/chanshuyi/p/the_java_class_load_mechamism.html 关键语句 我们只知道有一个构造方法,但实际上Ja ...

  8. 『ACM C++』Virtual Judge | 两道基础题 - The Architect Omar && Malek and Summer Semester

    这几天一直在宿舍跑PY模型,学校的ACM寒假集训我也没去成,来学校的时候已经18号了,突然加进去也就上一天然后排位赛了,没学什么就去打怕是要被虐成渣,今天开学前一天,看到最后有一场大的排位赛,就上去试 ...

  9. 你所不知道的库存超限做法 服务器一般达到多少qps比较好[转] JAVA格物致知基础篇:你所不知道的返回码 深入了解EntityFramework Core 2.1延迟加载(Lazy Loading) EntityFramework 6.x和EntityFramework Core关系映射中导航属性必须是public? 藏在正则表达式里的陷阱 两道面试题,带你解析Java类加载机制

    你所不知道的库存超限做法 在互联网企业中,限购的做法,多种多样,有的别出心裁,有的因循守旧,但是种种做法皆想达到的目的,无外乎几种,商品卖的完,系统抗的住,库存不超限.虽然短短数语,却有着说不完,道不 ...

随机推荐

  1. pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.

    # 背景 安装pip后发现执行pip install pytest,提示下面错误 pip is configured with locations that require TLS/SSL, howe ...

  2. C# GetHashCode、Equals函数和键值对集合的关系

    C# GetHashCode.Equals函数和键值对集合的关系 说明 HashCode:Hash码.特性:两个值,相同的的值生成的Hash肯定相同,Hash不同的值肯定不同. 下面一张图中,只有和“ ...

  3. Fiddler4无法抓取HttpWebRequest本地请求的解决办法

    网上很多解决案例是如下方代码设置代理,但在我的Fiddler4环境下无效,后寻得官方处理方法证实与代理无关. HttpWebRequest request= WebRequest.Create(&qu ...

  4. 索引+sql优化

    索引的概念: 索引是提高查询速度的一种手段.索引有很多种,以下是索引树的结构 要求查询出薪资大于5000的雇员信息,只要在树中找到5000的节点,直接查询该节点右边的数据即可,左边就不用管了,这样提高 ...

  5. RFID

    RFID 物联网必不可少会接触到RFID,国内比较常见的RFID读卡驱动芯片有两款.一款是NXP的RC522这系列的,非常稳定,当然也相对较贵,另一款是复旦微电子的FM1702系列,国产的便宜没得说, ...

  6. CAS客户端整合(二) Zabbix

    Zabbix是一个强大的服务器/交换机监控应用,有zabbix-server, zabbix-client, zabbix-web 三部分.zabbix-web管理端是用php写的. 前文参考:CAS ...

  7. 动态代理(CGLIB实现)

    CGLIB(Code Generation Library)是一个开源项目.可以直接对类进行增强,而不需要像JDK的动态代理,需要增强的类必须实现某接口 在使用Spring框架时,因为Spring框架 ...

  8. 【文文殿下】后缀自动机(Suffix Automaton,SAM)学习笔记

    前言 后缀自动机是一个强大的数据结构,能够解决很多字符串相关的(String-related)问题. 例如:他可以查询一个字符串在另一个字符串中出现的所有子串,以及查询一个字符串中本质不同的字符串的个 ...

  9. java从txt文档读写数据

    package com.abin.facade.ws.mail.function; import java.io.BufferedReader; import java.io.File; import ...

  10. CPU缓存刷新的误解

    即使是资深的技术人员,我经常听到他们谈论某些操作是如何导致一个CPU缓存的刷新.看来这是关于CPU缓存如何工作和缓存子系统如何与执行核心交互的一个常见误区.本文将致力于解释CPU缓存的功能以及执行程序 ...