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. gRPC service 和client需要引用的包

    gPRC service 在ASP.NET Core中使用 直接引用Grpc.AspNetCore,该包含有Google.Protobuf.Grpc.AspNetCore.Server.ClientF ...

  2. (Jmeter笔记)设置全局变量,跨线程调用变量,函数助手使用方法__setProperty和__p

    需求: 线程2获取线程1的Token成功,并可用 1.使用方法__setProperty定义一个内置函数 2.添加BeanShell后置处理程序 String Token=bsh.args[0]; / ...

  3. 【Java】RocketMQ

    <dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>rocketmq-cli ...

  4. ubuntu18.04.3新装系统安装QT5.14.1和环境配置

    第一步:下载QT: http://download.qt.io/archive/qt/ 或者 https://mirrors.tuna.tsinghua.edu.cn/qt/archive/qt/ 下 ...

  5. Windows MFC HTTP 函数流程

    Windows MFC HTTP 函数流程 1 //建立连接 2 pInternetSession = new CInternetSession(AfxGetAppName()); 3 4 5 6 / ...

  6. Unity 简易聊天室(基于TCP)(1)

    为了准备毕业设计,学习了服务器与客户端之间传输的一些简单的知识,并跟着网络上的教程制作了一个简易的Unity聊天室 服务器:用C# .Net Framework写的 结构分为:main(主函数).Se ...

  7. 突然连不上虚拟机,本地网络里没有VMnet8

    今天打开虚拟机,突然发现无法ping通网络了,但是能ping通虚拟机ip,打开我的window的网络适配器发现居然没有vmnet 8虚拟网卡了,防火墙什么的都设置好了,仍然不行,后来发现,在网络和共享 ...

  8. Linux系统安装&VMware安装三

    第十四步: 开始安装

  9. Markdown基本使用教程

    Markdown学习 标题 一个'#+空格'是一级标题,'##+空格'二级标题 字体 hello: 斜体'+内容+' hello:加粗'+内容+' hello hello 引入 引用来源 分割线 '三 ...

  10. python读取Excel文件的操作

    ①通过xlutils在已有表中写数据(这种方法会改变excel的样式) import xlrd,xlwt from xlutils.copy import copy 将已存在的Excel表格赋值给变量 ...