本来打算写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. WebApi 插件式构建方案:IOC 容器初始化

    body { border: 1px solid #ddd; outline: 1300px solid #fff; margin: 16px auto; } body .markdown-body ...

  2. 一起学习《C#高级编程》3--运算符重载

    运算符的重载.C++的开发人员应该很熟悉这个概念,但这对Java 和 VB 开发人员确实全新的. 对于一些数值间的运算,如果通过方法来指定运算规则的话,不免会繁琐,这时就可以利用运算符的重载. 例: ...

  3. linux shell中"2>&1"含义

    在计划任务中经常可以看到.例如我们公司的计划任务举例: */ * * * * root cd /opt/xxxx/test_S1/html/xxxx/admin; php index.php task ...

  4. 【Oracle 12c】最新CUUG OCP-071考试题库(55题)

    55.(13-3) choose the best answer: Which statement is true regarding the SESSION_PRIVS dictionary vie ...

  5. 微信小程序下可以使用的MD5以及AES加密(通用)

    两段代码都来自网络 ,在小程序下的加解密结果与CS/BS等算出的结果都一致,支持汉字. 一.MD5: var rotateLeft = function (lValue, iShiftBits) { ...

  6. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option)

    今天运行Redis时发生错误,错误信息如下: org.springframework.dao.InvalidDataAccessApiUsageException: MISCONF Redis is ...

  7. 总结day23 ---- 网络编程,以及计算机基础概念

    计算机网络的发展及基础网络概念 问题:网络到底是什么?计算机之间是如何通信的? 早期 : 联机 以太网 : 局域网与交换机 广播 主机之间“一对所有”的通讯模式,网络对其中每一台主机发出的信号都进行无 ...

  8. C#生成验证码类

    using System;using System.Collections.Generic;using System.Drawing;using System.Drawing.Drawing2D;us ...

  9. SSAS Tabular模式中关系设置不支持直接设置多对多?

    在网上文档发现一篇文章 微软BI 之SSAS 系列 - 多维数据集维度用法之一 引用维度 Referenced Dimension 有涉及到SSAS模型的关系设置 但是本人的基于表格模型的 没有关系可 ...

  10. Https 客户端与服务器交互过程梳理

    本文试图以通俗易通的方式介绍Https的工作原理,不纠结具体的术语,不考证严格的流程.我相信弄懂了原理之后,到了具体操作和实现的时候,方向就不会错,然后条条大路通罗马.阅读文本需要提前大致了解对称加密 ...