本来打算写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. centos升级gcc

    https://blog.csdn.net/lanwilliam/article/details/77893033 [root@DS-VM-Node239 ~]# yum install centos ...

  2. ASP.NET 常用的字符串加密

    字符串常用的加密有三种 1.MD5加密,这个常用于密码,单向加密,不可解密,有些在线解密的可以解大部份,用代码不能实现,如果不想让人解密,加密后随便截取一段就好了: 2.Base64位加密,通常加密后 ...

  3. NETCore 同步AD域组织和用户

    BitAdminCore为没有自主开发框架的小团队,提供快速项目搭建及开发能力. 框架演示:http://bit.bitdao.cn 框架源码:https://github.com/chenyinxi ...

  4. ASP .NET运行机制(visio图)

  5. PostMan工具使用之基础篇

    PostMan工具使用之基础篇 一.什么是PostMan Postman一款非常流行的API调试工具.(其他测试工具 Jmeter.soapUI) 二.下载及安装: 1.下载: 下载地址:https: ...

  6. ubuntu emacs的安装

    在终端依次输入这三条命令即可 sudo add-apt-repository ppa:ubuntu-elisp/ppa sudo apt-get update sudo apt-get install ...

  7. python网络编程--socket,网络协议,TCP

    一. 客户端/服务端架构(用到网络通信的地方) 我们使用qq.微信和别人聊天,通过浏览器来浏览页面.看京东的网站,通过优酷.快播(此处只是怀念一下)看片片啥的等等,通过无线打印机来打印一个word文档 ...

  8. c++实验5 顺序/链式队列

    链式队列及循环队列 1.循环队列的实现(请采用模板类及模板函数实现) [实现提示] 同时可参见教材p65-p67页的ADT描述及算法实现及ppt)函数.类名称等可自定义,部分变量请加上学号后3位.也可 ...

  9. junit所需要的jar包

    hamcrest-core-1.1.jar junit-4.12.jar http://central.maven.org/maven2/org/hamcrest/hamcrest-core/1.3/ ...

  10. C# RDLC报表不出现预览窗体直接输出到打印机

    #region 直接打印区域 /// <summary> /// 直接打印到打印机 /// </summary> /// <param name="report ...