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类加载机制
你所不知道的库存超限做法 在互联网企业中,限购的做法,多种多样,有的别出心裁,有的因循守旧,但是种种做法皆想达到的目的,无外乎几种,商品卖的完,系统抗的住,库存不超限.虽然短短数语,却有着说不完,道不 ...
随机推荐
- 原创:MVC 5 实例教程(MvcMovieStore 新概念版:mvc5.0,EF6.01) - 2、数据框架 和 功能预览
说明:MvcMovieStore项目已经发布上线,想了解最新版本功能请登录 MVC 影视(MvcMovie.cn) 进行查阅.如需转载,请注明出处:http://www.cnblogs.com/Dod ...
- Python 实现图片对比检测
在写测试框架的时候,需要用到图片对比的方法来判断用例执行的情况,问了一下度娘,原来可以用PIL模块处理: from PIL import Image # 先安装Pillow, \>pip in ...
- lucene整理3 -- 排序、过滤、分词器
1. 排序 1.1. Sort类 public Sort() public Sort(String field) public Sort(String field,Boolean reverse ...
- can not update ICEAuthority file mint或则ubuntu
引起的原因为:设置了用户自动登录引起的. 1)去除自动登录 2)删除 ICEAuthority文件. 即可.
- Bootstrap变形记
bootstrap审美疲劳了,想个招换换样子,THINKING... 变形 >>> 哈,不用改已有代码,添加我的Harley.js即可,有空在玩... 真实好久不玩博客园了,200字 ...
- 「HNOI 2015」实验比较
\(Description\) 有\(n\)个元素,对于每个元素\(x_i\)最多知道一个形如\(x_j < x_i\)或\(x_j=x_i\)的条件,问有多少合法的序列.合法的序列满足每个元素 ...
- Android Library 使用规则
1. 一个项目必须声明为 is library.使用该 library 的项目导入该 library. 2. library 的 manifect 中 activity 的声明需明确包名以避免找不到. ...
- bzoj3435 [Wc2014]紫荆花之恋(动态点分治+替罪羊树)
传送门(权限) 传送门(非权限) 题解 我终终终终终终于做出来啦!!! 作为一个没有学过替罪羊树的蒟蒻现场学了一下替罪羊树,作为一个平衡树都写数组版本的看着大佬的指针题解无语只能硬去理解然后照着抄了一 ...
- python学习笔记-练手实例
1.题目:输出 9*9 乘法口诀表. 程序分析:分行与列考虑,共9行9列,i控制行,j控制列 代码: for i in range(1,10): print ('\r') for j ...
- 《快学Scala》第八章 继承