leetcode简单题目两道(3)
本来打算写redis的,时间上有点没顾过来,只能是又拿出点自己的存货了。
Problem
Given an array nums, write a function to move all 's to the end of it while maintaining the relative order of the non-zero elements.
Example:
Given nums = [, , , , ], after calling your function, nums should be [, , , , ].
Note:
You must do this in-place without making a copy of the array. Minimize the total number of operations.
Code:
class Solution {
public:
void moveZeroes(vector<int>& nums) {
if (nums.size() == || nums.size() == ) {
return;
}
int i = , j,k;
while(i < nums.size()) {
while (nums[i] != ) {
i++;
}
if (i < nums.size()) {
j = i + ;
while (j < nums.size() && nums[j] == ) {
j++;
}
if (j < nums.size()) {
k = nums[i];
nums[i] = nums[j];
nums[j] = k;
}
i++;
}
}
}
};
说明:
用两个下标,i保存碰到的0的位置,j表示从i之后第一个非0,交换之后i++,j继续。
Problem:
Given a pattern and a string str, find if str follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
Examples:
pattern = "abba", str = "dog cat cat dog" should return true. pattern = "abba", str = "dog cat cat fish" should return false. pattern = "aaaa", str = "dog cat cat dog" should return false. pattern = "abba", str = "dog dog dog dog" should return false.
Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.
Code:
public class Solution {
public boolean wordPattern(String pattern, String str) {
if (pattern == null && str == null) {
return true;
}
if (pattern == null) {
return false;
}
if (str == null) {
return false;
}
String[] array = str.split(" ");
if (pattern.length() != array.length) {
return false;
}
Map<String, String> map = new HashMap<String, String>();
Set<String> value = new HashSet<String>();
for (int i = 0; i < pattern.length(); i++) {
String tmp = pattern.substring(i, i + 1);
if (map.containsKey(tmp)) {
if (array[i].compareTo(map.get(tmp)) != 0) {
return false;
}
} else {
if (value.contains(array[i])) {
return false;
}
map.put(tmp, array[i]);
value.add(array[i]);
}
}
return true;
}
}
说明:
发现字符串的操作还是java的String类比较好用,此题的思路在于对待唯一,就是一个字符对应唯一的字符串,且一个串对应唯一的字符。
leetcode简单题目两道(3)的更多相关文章
- leetcode简单题目两道(2)
Problem Given an integer, write a function to determine if it is a power of three. Follow up: Could ...
- leetcode简单题目两道(4)
心情还是有问题,保持每日更新,只能如此了. Problem Given a binary tree, return the level order traversal of its nodes' va ...
- 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类加载机制
你所不知道的库存超限做法 在互联网企业中,限购的做法,多种多样,有的别出心裁,有的因循守旧,但是种种做法皆想达到的目的,无外乎几种,商品卖的完,系统抗的住,库存不超限.虽然短短数语,却有着说不完,道不 ...
随机推荐
- /usr/bin/curl: Argument list too long的解决方法
使用curl发送http请求时,会出现-bash: /usr/bin/curl: Argument list too long的错误,此时,可用采用httpie代替curl发送请求: pip inst ...
- 自己从0开始学习Unity的笔记 IV (C#循环练习-数字猜谜游戏)
想起来现在基础的已经学了不少了,那么这次试一下用while写一个数字猜谜的. Random roll = new Random(); //建立一个骰子 , ); //让骰子在1-100内随机一个数 ; ...
- 设置TeeChart的提示文本
使用第三方Steema的TeeChart控件,设置鼠标放在某一线条点上,显示某一点的数据标签问题(虚线型十字光标基准线,放在线上显示对应点的二维坐标轴数据数据),调用InitTeeChartTipTo ...
- Mysql 中日期类型bigint和datetime互转
MySql数据库中字段类型bigint 长度是10位的 mysql> select (from_unixtime(1554047999))as datatime;+--------------- ...
- ADB 源码分析(一) ——ADB模块简述【转】
ADB源码分析(一)——ADB模块简述 1.Adb 源码路径(system/core/adb). 2.要想很快的了解一个模块的基本情况,最直接的就是查看该模块的Android.mk文件,下面就来看看a ...
- 12c ocp 062新考题(之前没出现过)-1
1.One of your databases has archive logging enabled and RMAN backups are taken at regular intervals. ...
- centos7修改静态ip地址
今天逛园的时候突然发现这篇有关网络参数修改的文章写的很好,简单又使用,格式也很好的,所以就引用过来了. http://www.cnblogs.com/hongdada/p/6666932.html
- HTTP 缓存机制详解
从这里看的 http://mp.weixin.qq.com/s/8UXEMQBkV9hHwtu9R7mV5w
- JAVA数组的遍历和取最值
1.获取数组中的所有元素,会用到数组的遍历 数组的遍历,通常用for循环. public class ArrayDemo { public static void main(String[] args ...
- html中文字溢出处理(text-overflow)
文字溢出处理有两种方式: 一.css overflow:hidden; white-space: nowrap; text-overflow: ellips ...