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 < ...
随机推荐
- G6-Editor 编辑器入门使用教程
一.前言 G6-Editor 是 AntV 官方提供的.专注于图可视化编辑器的类库,也是市面上完成度较高的图可视化编辑器.然而令人诟病的是其文档对新手极度不友好,我一度怀疑此文档只有他们自己开发人员才 ...
- jemeter批量测试
一.使用badboy录制脚本 1.下载安装badboy(参看:https://blog.csdn.net/qq_36396763/article/details/78803381),成功标志如下: 2 ...
- linux 系统⽇常管理--运维必备
[监控系统的状态] 1. w 查看当前系统的负载 相信所有的linux管理员最常⽤的命令就是这个'w' 了,该命令显⽰的信息还是蛮丰富 的.第⼀⾏从左⾯开始显⽰的信息依次为:时间,系统运⾏时间,登录⽤ ...
- SQL 用 in 大于 1000 问题解决
-- 今天生成环境数据突然多,系统异常 解决方案(必须用in 业务情况),也可以用其他函数解决 union all 或者 exists 等 1:截取list List<Integer> ...
- 05 RDD练习:词频统计
一.词频统计: 1.读文本文件生成RDD lines 2.将一行一行的文本分割成单词 words flatmap() 3.全部转换为小写 lower() 4.去掉长度小于3的单词 filter() 5 ...
- 插件和依赖的区别以及Java web开发层次结构
一:插件和依赖的区别 依赖:运行时和开发时都需要用到的包,比如项目中需要一个包,就要添加一个依赖(数据库驱动,连接池,mybatis...),这个依赖在项目运行时也需要,因此在项目打包时需要把这些依赖 ...
- linux Usb serial console
ubuntu Usb serial console 能够把下电时打印输出到串口上,可以记录,而netconsole只能输出下电到disk 之前的打印 Usb串口线,ftdi或pl2303都可以 如果是 ...
- 二叉树系列之Treap树
Treap是一棵拥有键值.优先级两种权值的树 struct node{ int size;//以这个结点为根的子树的结点总数量,用于名次树 int rank;//优先级 int key ...
- hdu1710 二叉树(C/C++)
hdu1710 题目地址:https://acm.dingbacode.com/showproblem.php?pid=1710 (最近几天杭电原网址开不进去了,之后应该可以通..吧) Binary ...
- docker安装常用软件
linux安装docker 1.安装gcc相关 yum install gcc -y yum install gcc-c++ -y 2.安装工具包 #安装工具包 yum -y install yum- ...