1. 判断括号有效性

public static boolean fun5(String str) {
HashMap<Character, Character> hashMap = new HashMap<>();
hashMap.put(')', '(');
hashMap.put(']', '[');
hashMap.put('}', '{');

LinkedList<Character> stack = new LinkedList<>();
for (char c : str.toCharArray()) {
if (hashMap.containsKey(c)) {
if (stack.peek() != hashMap.get(c)) {
return false;
}
stack.pop();
} else {
stack.push(c);
}
}
return stack.isEmpty();
}

2. 输出字符串可能的组合次数
package com.example.helloworld;

import java.util.HashMap;

public class Main1
{
public static void main (String[] args)
{
// 输出字符串可能的组合次数
String str = "AABBCC";
System.out.println(fun(str));
}
public static int fun(String str) {
int count = 0;
if (str == null || "".equals(str)) {
return count;
}
HashMap<Character, Integer> hashMap = new HashMap<>();
for (Character ch : str.toCharArray()) {
if (hashMap.containsKey(ch)) {
int a = hashMap.get(ch);
hashMap.put(ch,++a);
} else {
hashMap.put(ch, 1);
}
}
int len = calculate(str.length());
int result = 1;
for (Integer integer :hashMap.values()){
result = calculate(integer) * result;
}
count = len / result;
return count;
}
public static int calculate(int value) {
if (value == 0) {
return 0;
}
if (value == 1) {
return 1;
}
return value * calculate(value - 1);
}
}

3. 大小写转换
package com.example.helloworld;

import java.util.HashMap;
import java.util.Map;

public class Main
{
public static void main (String[] args)
{
/**
* 题目大意
* 定义每个字母的位置:A->1 B->2 …Z->26… a->1 b->2 … z->26
* 给一个只包含大小写字母的字符串,做以下操作
* 1.将字符串升序排序
* 2.对应一个字符,将其位置值的平方除26取余数+1得到新位置,转换成对应位置字符,大写的和大写的转,小写的和小写的转
* 样例
* 样例1:输入:bA 输出:Be
* 解释:bA排序Ab,A对应1,(1 * 1) % 26 + 1 = 2,A转换成B, b对应2,(2 * 2) % 26 + 1 = 5,5对应e,所以最后输出Be
* 样例2: 输入:HuaweiIsBest 输出:EMDbzzdxxkzj
*/
String str= "HuaweiIsBest";
System.out.println(fun(str));
}
public static String fun(String str) {
HashMap<Character, Integer> hm1 = new HashMap<>();
hm1.put('A', 1);
hm1.put('B', 2);
hm1.put('C', 3);
hm1.put('D', 4);
hm1.put('E', 5);
hm1.put('F', 6);
hm1.put('G', 7);
hm1.put('H', 8);
hm1.put('I', 9);
hm1.put('J', 10);
hm1.put('K', 11);
hm1.put('L', 12);
hm1.put('M', 13);
hm1.put('N', 14);
hm1.put('O', 15);
hm1.put('P', 16);
hm1.put('Q', 17);
hm1.put('R', 18);
hm1.put('S', 19);
hm1.put('T', 20);
hm1.put('U', 21);
hm1.put('V', 22);
hm1.put('W', 23);
hm1.put('X', 24);
hm1.put('Y', 25);
hm1.put('Z', 26);
HashMap<Character, Integer> hm2 = new HashMap<>();
hm2.put('a', 1);
hm2.put('b', 2);
hm2.put('c', 3);
hm2.put('d', 4);
hm2.put('e', 5);
hm2.put('f', 6);
hm2.put('g', 7);
hm2.put('h', 8);
hm2.put('i', 9);
hm2.put('j', 10);
hm2.put('k', 11);
hm2.put('l', 12);
hm2.put('m', 13);
hm2.put('n', 14);
hm2.put('o', 15);
hm2.put('p', 16);
hm2.put('q', 17);
hm2.put('r', 18);
hm2.put('s', 19);
hm2.put('t', 20);
hm2.put('u', 21);
hm2.put('v', 22);
hm2.put('w', 23);
hm2.put('x', 24);
hm2.put('y', 25);
hm2.put('z', 26);
char[] chars= str.toCharArray();
for (int i = 0; i < chars.length; i++) {
for (int j = i + 1; j < chars.length; j++) {
if (chars[i] > chars[j]) {
char t = chars[i];
chars[i] = chars[j];
chars[j] = t;
}
}
}
//将其位置值的平方除26取余数+1得到新位置,转换成对应位置字符,大写的和大写的转,小写的和小写的转
StringBuilder sb = new StringBuilder();
for (Character ch : chars) {
if (hm1.containsKey(ch)) {
int v = hm1.get(ch);
int newInt = calculate(v);
for (Map.Entry<Character, Integer> entry : hm1.entrySet()) {
if (entry.getValue() == newInt) {
sb.append(entry.getKey());
}
}
} else if (hm2.containsKey(ch)) {
int v = hm2.get(ch);
int newInt = calculate(v);
for (Map.Entry<Character, Integer> entry : hm2.entrySet()) {
if (entry.getValue() == newInt) {
sb.append(entry.getKey());
}
}
}
}
return sb.toString();
}
public static int calculate(int val){
double v = Math.pow(val, 2) % (double)26;
return (int)(Math.round(v) +1);
}
}

Java 面试手撕代码的更多相关文章

  1. Java面试手写代码题

    1.栈实现 2.Iterator实现 3.单例 4.多线和控制(暂停,恢复,停止) 5.生产者消费者

  2. 手撕代码:统计1到n二进制数中1出现的总次数

    题目描述: 互娱手撕代码题. 统计从1到n这n个数的二进制表示中1出现的次数. 思路分析: 思路一:直接的做法是从1遍历到n,对于每个数和1做与操作,之后,对于这个数不断做右移操作,不断和1做与操作, ...

  3. 前端面试手写代码——call、apply、bind

    1 call.apply.bind 用法及对比 1.1 Function.prototype 三者都是Function原型上的方法,所有函数都能调用它们 Function.prototype.call ...

  4. 前端面试手写代码——模拟实现new运算符

    目录 1 new 运算符简介 2 new 究竟干了什么事 3 模拟实现 new 运算符 4 补充 预备知识: 了解原型和原型链 了解this绑定 1 new 运算符简介 MDN文档:new 运算符创建 ...

  5. 前端面试手写代码——JS函数柯里化

    目录 1 什么是函数柯里化 2 柯里化的作用和特点 2.1 参数复用 2.2 提前返回 2.3 延迟执行 3 封装通用柯里化工具函数 4 总结和补充 1 什么是函数柯里化 在计算机科学中,柯里化(Cu ...

  6. js面试-手写代码实现new操作符的功能

    我们要搞清楚new操作符到底做了一些什么事情? 1.创建一个新的对象 2.将构造函数的作用域赋给新对象(因此this指向了这个新对象) 3.执行构造函数中的代码(为这个新对象添加属性) 4.返回新对象 ...

  7. 手撕代码:leetcode 309最佳买卖股票时机含冷冻期

    转载于:https://segmentfault.com/a/1190000014746613 给定一个整数数组,其中第i个元素代表了第i天的股票价格. 设计一个算法计算出最大利润.在满足以下约束条件 ...

  8. 手撕代码:leetcode70爬楼梯

    装载于:https://blog.csdn.net/qq_35091252/article/details/90576779 题目描述 假设你正在爬楼梯.需要n阶你才能到达楼顶. 每次你可以爬1或2个 ...

  9. 前端面试手写代码——JS数组去重

    目录 1 测试用例 2 JS 数组去重4大类型 2.1 元素比较型 2.1.1 双层 for 循环逐一比较(es5常用) 2.1.2 排序相邻比较 2.2 查找元素位置型 2.2.1 indexOf ...

  10. 手撕代码之线程:thread类简单使用

    转载于:https://blog.csdn.net/qq_22494029/article/details/79273127 简单多线程例子: detch()启动线程: 1 #include < ...

随机推荐

  1. 基础篇之DOS命令

    对于编程的小朋友来说,在DOS中输入一些命令也是常有的事情. 今天就来学习一些常见的命令.[该篇是在B站学习Siki学院的课程时做的笔记] DOS命令目录操作 常用DOS命令(输入命令后按下回车) d ...

  2. Oracle UNDOTBS表空间的查看与扩容

    1.查看UNDO表空间使用情况 select tablespace_name,       round(sum(decode(status, 'ACTIVE', bytes, 'UNEXPIRED', ...

  3. Ubunutu的apt-get软件下载工具的国内镜像源

    Ubunutu安装时,默认没有给apt-get配置国内镜像源,其默认配置的镜像源国内不能访问到,安装软件处处失败,所以我们需要配置国内镜像源: 首先避免出错,我们先备份原sources.list文件, ...

  4. Python编码转换图

  5. PHP开发支付时开启OPENSSL扩展

    开发支付功能时,发现openssl类的方法都找不到,大概知道是没有扩展,在网上收集了PHP开启openssl扩展的方法. windows下开启方法: 1: 首先检查php.ini中:extension ...

  6. 排球计分程序的uml图

  7. APP性能测试——首次启动耗时测试

    首次启动耗时: 即第一次安装(清除数据也可以),打开软件,直到进入到首页activity页面,并计算耗时. 示例代码: import os import time # 测试首次启动时间 # 创建App ...

  8. redis存储类型-数字和带双引号的数字

    这个是不一样的值,出现了转换异常

  9. 在NCBI中下载SRA数据

    目前,在NCBI中下载SRA数据主要有三种方式: 利用Aspera工具下载. 利用SRA Toolkit下载. 利用wget命令直接下载 第三种最为方便.其中的关键是得到下载数据的链接,即ftp的地址 ...

  10. 人森第一个iOS app,写给我家baby的!纪念一下

    用python写的,对于非专业iOS开发来说,py是个不错的选择,使用beeware框架,感觉和写前端差不多