leetcode简单题目两道(4)
心情还是有问题,保持每日更新,只能如此了。
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() <= ) {
return s;
}
int i = , j = s.size() - ;
char ch;
while (i < 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() <= ) {
return s;
}
int i = , j = s.size() - ;
char ch;
while (i < j) {
while(i < 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 < 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)的更多相关文章
- leetcode简单题目两道(2)
Problem Given an integer, write a function to determine if it is a power of three. Follow up: Could ...
- leetcode简单题目两道(3)
本来打算写redis的,时间上有点没顾过来,只能是又拿出点自己的存货了. Problem Given an array nums, write a function to move all 's to ...
- leetcode简单题目两道(5)
Problem Given an integer (signed bits), write a function to check whether it . Example: Given num = ...
- leetcode简单题目两道(1)
Problem: You are playing the following Nim Game with your friend: There is a heap of stones on the t ...
- CTF中关于XXE(XML外部实体注入)题目两道
题目:UNCTF-Do you like xml? 链接:http://112.74.37.15:8008/ hint:weak password (弱密码) 1.观察后下载图片拖进WINHEX发现提 ...
- 两道面试题,带你解析Java类加载机制
文章首发于[博客园-陈树义],点击跳转到原文<两道面试题,带你解析Java类加载机制> 在许多Java面试中,我们经常会看到关于Java类加载机制的考察,例如下面这道题: class Gr ...
- 【转】两道面试题,带你解析Java类加载机制(类初始化方法 和 对象初始化方法)
本文转自 https://www.cnblogs.com/chanshuyi/p/the_java_class_load_mechamism.html 关键语句 我们只知道有一个构造方法,但实际上Ja ...
- 『ACM C++』Virtual Judge | 两道基础题 - The Architect Omar && Malek and Summer Semester
这几天一直在宿舍跑PY模型,学校的ACM寒假集训我也没去成,来学校的时候已经18号了,突然加进去也就上一天然后排位赛了,没学什么就去打怕是要被虐成渣,今天开学前一天,看到最后有一场大的排位赛,就上去试 ...
- 你所不知道的库存超限做法 服务器一般达到多少qps比较好[转] JAVA格物致知基础篇:你所不知道的返回码 深入了解EntityFramework Core 2.1延迟加载(Lazy Loading) EntityFramework 6.x和EntityFramework Core关系映射中导航属性必须是public? 藏在正则表达式里的陷阱 两道面试题,带你解析Java类加载机制
你所不知道的库存超限做法 在互联网企业中,限购的做法,多种多样,有的别出心裁,有的因循守旧,但是种种做法皆想达到的目的,无外乎几种,商品卖的完,系统抗的住,库存不超限.虽然短短数语,却有着说不完,道不 ...
随机推荐
- View Pi's Status on WebBrowser
1. install php and cgi support sudo apt-get install php5-common sudo apt-get install php5-cgi sudo a ...
- PostgreSQL 表空间
PostgreSQL 表空间 一 介绍使用表空间可以将不同的表放到不同的存储介质或不同的文件系统下,实际上是为表指定一个存储的目录.创建数据库,表,索引时可以指定表空间,将数据库,表,索引放到指定的目 ...
- 扫描指定ip的端口(C#)
class PingExam { public static void Main() { Ping ping = new Ping(); string ip = "192.168.1.43& ...
- easyui - using
using 是 easyloader.load 简写 using('calendar', function() { alert("加载calendar成功 ...
- (zxing.net)一维码EAN 13的简介、实现与解码
一维码EAN 13:属于国际标准条码, 由13个数字组成,为EAN的标准编码型式(EAN标准码). 依结构的不同,EAN条码可区分为: EAN 13码: 由13个数字组成,为EAN的标准编码型式(EA ...
- WPF解决方案------调用线程无法访问此对象,因为另一个线程拥有该对象
WPF [调用线程无法访问此对象,因为另一个线程拥有该对象.] 解决方案 在这里以播放图片为例进行说明,代码如下: void _Timer_Elapsed(object sender, Elapsed ...
- 【Oracle 12c】最新CUUG OCP-071考试题库(53题)
53.(12-14) choose the best answer: Examine the command to create the BOOKS table. SQL>CREATE TABL ...
- Spring Boot启动过程(三)
我已经很精简了,两篇(Spring Boot启动过程(一).pring Boot启动过程(二))依然没写完,接着来. refreshContext之后的方法是afterRefresh,这名字起的真.. ...
- java使用Redis6--sentinel单点故障主从自动切换
Redis SentinelSentinel(哨兵)是用于监控redis集群中Master状态的工具,其已经被集成在redis2.4+的版本中 一.Sentinel作用:1):Master状态检测 2 ...
- [iOS笔试600题]一、语法篇(共有147题)
[A]1. @property 的作用是申明属性及真特性?[判断题] A.正确 B.错误 [A]2. @synthesize的作用是自动笠成属性的访问器(getter/setter)方法?[判断题] ...