本来打算写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)的更多相关文章

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

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

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

    心情还是有问题,保持每日更新,只能如此了. Problem Given a binary tree, return the level order traversal of its nodes' va ...

  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. struts中request传递中文乱码问题

    系统本来是好好地,这两天升级后,各种问题不断,总而言之,一句话,心惊胆战. 今天,搜索任何中文,都是有乱码,在action中转码就ok了.公司系统那么多action,都转码,要累死吧.配置的过滤器都不 ...

  2. springboot整合websocket后运行测试类报错:javax.websocket.server.ServerContainer not available

    springboot项目添加websocket依赖后运行测试类报如下错误: org.springframework.beans.factory.BeanCreationException: Error ...

  3. let、const和var的区别

    1.let是块级作用域,函数内部使用let定义后,对函数外部无影响. <!DOCTYPE html> <html lang="en"> <head&g ...

  4. “全栈2019”Java多线程第十章:Thread.State线程状态详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  5. BZOJ4012 [HNOI2015]开店 (动态点分治)

    Description 风见幽香有一个好朋友叫八云紫,她们经常一起看星星看月亮从诗词歌赋谈到 人生哲学.最近她们灵机一动,打算在幻想乡开一家小店来做生意赚点钱.这样的 想法当然非常好啦,但是她们也发现 ...

  6. 查看python中已安装的包

    pip list 现在我又知道了个:rpm -qa | grep XXXX(moudle name)

  7. leetcode-350-Intersection of Two Arrays II(求两个数组的交集)

    题目描述: Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, ...

  8. 第一站:CLion安装教程与环境配置

    原文来自:http://www.sunmey.cn/thread-129-1-1.html 本人:找了很久才找到的CLion安装教程与环境配置,这里分享给大家~ 这里要说明的一点是CLion是要钱的, ...

  9. springmvc常遇到的错误

    错误1: HTTP Status 500 - Handler processing failed; nested exception is java.lang.NoClassDefFoundError ...

  10. QuantLib 金融计算——基本组件之 Date 类

    目录 QuantLib 金融计算--基本组件之 Date 类 Date 对象的构造 一些常用的成员函数 一些常用的静态函数 为估值计算配置日期 如果未做特别说明,文中的程序都是 Python3 代码. ...