Java 面试手撕代码
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 面试手撕代码的更多相关文章
- Java面试手写代码题
1.栈实现 2.Iterator实现 3.单例 4.多线和控制(暂停,恢复,停止) 5.生产者消费者
- 手撕代码:统计1到n二进制数中1出现的总次数
题目描述: 互娱手撕代码题. 统计从1到n这n个数的二进制表示中1出现的次数. 思路分析: 思路一:直接的做法是从1遍历到n,对于每个数和1做与操作,之后,对于这个数不断做右移操作,不断和1做与操作, ...
- 前端面试手写代码——call、apply、bind
1 call.apply.bind 用法及对比 1.1 Function.prototype 三者都是Function原型上的方法,所有函数都能调用它们 Function.prototype.call ...
- 前端面试手写代码——模拟实现new运算符
目录 1 new 运算符简介 2 new 究竟干了什么事 3 模拟实现 new 运算符 4 补充 预备知识: 了解原型和原型链 了解this绑定 1 new 运算符简介 MDN文档:new 运算符创建 ...
- 前端面试手写代码——JS函数柯里化
目录 1 什么是函数柯里化 2 柯里化的作用和特点 2.1 参数复用 2.2 提前返回 2.3 延迟执行 3 封装通用柯里化工具函数 4 总结和补充 1 什么是函数柯里化 在计算机科学中,柯里化(Cu ...
- js面试-手写代码实现new操作符的功能
我们要搞清楚new操作符到底做了一些什么事情? 1.创建一个新的对象 2.将构造函数的作用域赋给新对象(因此this指向了这个新对象) 3.执行构造函数中的代码(为这个新对象添加属性) 4.返回新对象 ...
- 手撕代码:leetcode 309最佳买卖股票时机含冷冻期
转载于:https://segmentfault.com/a/1190000014746613 给定一个整数数组,其中第i个元素代表了第i天的股票价格. 设计一个算法计算出最大利润.在满足以下约束条件 ...
- 手撕代码:leetcode70爬楼梯
装载于:https://blog.csdn.net/qq_35091252/article/details/90576779 题目描述 假设你正在爬楼梯.需要n阶你才能到达楼顶. 每次你可以爬1或2个 ...
- 前端面试手写代码——JS数组去重
目录 1 测试用例 2 JS 数组去重4大类型 2.1 元素比较型 2.1.1 双层 for 循环逐一比较(es5常用) 2.1.2 排序相邻比较 2.2 查找元素位置型 2.2.1 indexOf ...
- 手撕代码之线程:thread类简单使用
转载于:https://blog.csdn.net/qq_22494029/article/details/79273127 简单多线程例子: detch()启动线程: 1 #include < ...
随机推荐
- 多个git账户ssh密钥配置
假设两git网站:A.com和B.com,在这两个网站上使用的邮箱和用户名分别为a@mail, userA和b@mail, userB. 清除全局配置 git config --global --li ...
- 进程间通信测试-signal
截图 代码 #include <stdio.h> #include <unistd.h> #include <signal.h> #include <stri ...
- 【RTOS】《多任务抢占式调度器》笔记
<多任务抢占式调度器>读书笔记 1.多任务系统 在多任务调度器的作用下,多个任务轮流使用cpu,实现多任务相互独立并发运行的效果,能够充分利用硬件资源,提高cpu效率 2.任务特性 a.动 ...
- java循环中的break和continue的小笔记
代码1: for(int i=0;i<10;i++){ System.out.println(i); continue; System.out.println("flag") ...
- Dapper存储过程分页
create database Month6use Month6 --用户表create table UserInfo( UId int primary key identity, UName var ...
- Jenkins启动失败的七个问题
1.jdk版本和路径问题(注意第6个问题) which java vim /etc/init.d/jenkins 2.用户名问题 查看/etc/sysconfig/jenkins的JENKINS_US ...
- 2020第十一届蓝桥杯大赛软件类国赛题目 C/C++ B 组
试题 A: 美丽的 2 本题总分:5 分 问题描述:在公元 1 年到公元 2020 年(包含)中,有多少个年份的数位中包含数字 2? #include <stdio.h> #include ...
- DevOps Gitlab环境部署
DevOps 介绍 目录 DevOps 介绍 一.DevOps 介绍 1.1.1 DevOps 介绍 1.1.2 CI/CD简介 1.1.2 Gitlab安装与使用 一.DevOps 介绍 1.1.1 ...
- 笔记:vue.nextTick()方法的使用详解
vue.nextTick定义: 在下次DOM更新循环结束之后执行延迟回调.在修改数据之后立即使用这个方法,获取更新后的DOM. 简单理解:数据更新了,在dom渲染后立即执行该函数 举例 注意:Vue实 ...
- “jupyter notebook 不能导入python库但是终端上可以实现”的问题的解决
在使用jupyter notebook的过程中,创建了一个新的环境(anaconda中env)后遇到了这样一个问题,就是: 在jupyter notebook上运行程序,中间发现有一个python库未 ...