算法学习之剑指offer(九)
一
题目描述
public class Solution {
public int Sum_Solution(int n) {
int sum=n;
boolean re = (n>0)&&((sum+=Sum_Solution(n-1))>0);
return sum;
}
}
二
题目描述
import java.math.BigInteger;
public class Solution {
public int Add(int num1,int num2) {
BigInteger a = new BigInteger (num1+"");
BigInteger b = new BigInteger (num2+"");
return a.add(b).intValue();
}
}
三
题目描述
输入描述:
输入一个字符串,包括数字字母符号,可以为空
输出描述:
如果是合法的数值表达则返回该数字,否则返回0
输入
+2147483647
1a33
输出
2147483647
0
public class Solution {
public int StrToInt(String str) {
if (str.equals("") || str.length() == 0)
return 0;
char[] chars = str.toCharArray();
int sum=0,fuhao=0,length=chars.length;
if(chars[fuhao]=='-')
fuhao=1;
for(int i=fuhao;i<length;i++){
if (chars[i] == '+')
continue;
if (chars[i] < 48 || chars[i] > 57)
return 0;
sum = sum*10+chars[i]-48;
}
return fuhao==0?sum:sum*-1;
}
}
四
题目描述
public class Solution {
// Parameters:
// numbers: an array of integers
// length: the length of array numbers
// duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
// Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
// 这里要特别注意~返回任意重复的一个,赋值duplication[0]
// Return value: true if the input is valid, and there are some duplications in the array number
// otherwise false
public boolean duplicate(int numbers[],int length,int [] duplication) {
if(numbers==null||numbers.length==0)
return false;
int[] new_numbers = new int[length];
for(int i=0;i<length;i++){
new_numbers[numbers[i]]++;
}
for(int i=0;i<length;i++){
if(new_numbers[i]>1){
duplication[0]=i;
return true;
}
}
return false;
}
}
五
题目描述
import java.util.ArrayList;
public class Solution {
public int[] multiply(int[] A) {
//弄一个矩阵,矩阵里的1代表多余的数,然后分两部分算。每部分的每一行都可以借助上一行
int length = A.length;
int[] B = new int[length];
if(length!=0){
B[0]=1;
for(int i=1;i<length;i++){
B[i]=B[i-1]*A[i-1];
}
int tmp=1;
for(int i=length-2;i>=0;i--){
tmp*=A[i+1];
B[i]*=tmp;
}
}
return B;
}
}
六
题目描述
public class Solution {
public boolean match(char[] str, char[] pattern) {
if (str == null || pattern == null) {
return false;
}
return matchCore(str, 0, pattern, 0);
}
public boolean matchCore(char[] str, int strIndex, char[] pattern, int patternIndex) {
if (strIndex == str.length && patternIndex == pattern.length) {
return true;
}
if (strIndex != str.length && patternIndex == pattern.length) {
return false;
}
if (patternIndex + 1 < pattern.length && pattern[patternIndex + 1] == '*') {
if ((strIndex != str.length && pattern[patternIndex] == str[strIndex]) || (pattern[patternIndex] == '.' && strIndex != str.length)) {
return matchCore(str, strIndex, pattern, patternIndex + 2)//模式后移2,视为x*匹配0个字符
|| matchCore(str, strIndex + 1, pattern, patternIndex + 2)//视为模式匹配1个字符
|| matchCore(str, strIndex + 1, pattern, patternIndex);//*匹配1个,再匹配str中的下一个
} else {
return matchCore(str, strIndex, pattern, patternIndex + 2);
}
}
if ((strIndex != str.length && pattern[patternIndex] == str[strIndex]) || (pattern[patternIndex] == '.' && strIndex != str.length)) {
return matchCore(str, strIndex + 1, pattern, patternIndex + 1);
}
return false;
}
/**第二种方法
// .* 匹配一切
if(pattern.length == 2
&& pattern[0] == '.'
&& pattern[1] == '*') return true;
// 两个序列入栈
Stack<Character> ori = new Stack<>();
Stack<Character> pat = new Stack<>();
for (int i = 0; i < str.length; i++) {
ori.push(str[i]);
}
for (int i = 0; i < pattern.length; i++) {
pat.push(pattern[i]);
}
// 从尾匹配,解决*重复前一个字符的问题
while (!ori.empty() && !pat.empty()){
// 如果是两个不相同的字母,匹配失败
if(Character.isLetter(pat.peek())
&& !pat.peek().equals(ori.peek()))
return false;
// 两个相同的字母,匹配成功,两个栈顶各弹出已经匹配的字符
if(Character.isLetter(pat.peek())
&& pat.peek().equals(ori.peek())){
ori.pop();
pat.pop();
}else if(pat.peek().equals('.')){ // 如果模式串是 ‘.’,直接把它替换为所需的字符入栈
pat.pop();
pat.push(ori.peek());
}else{ // 模式串是 *
pat.pop();
if(pat.peek().equals(ori.peek())){ // *的下一个是目标字符,则重复它再重新压入*
pat.push(ori.peek());
pat.push('*');
ori.pop();
}else{ // 否则从模式栈弹栈,直到找到匹配目标串的字符,或遇到.
while (!pat.empty()
&& !pat.peek().equals(ori.peek())
&& !pat.peek().equals('.')) pat.pop();
// 如果遇到了‘.’ 直接替换为目标字符,再重新压入*
if(!pat.empty() && pat.peek() == '.'){
pat.pop();
pat.push(ori.peek());
pat.push('*');
}
}
}
}
// 两栈空,则匹配成功
if(ori.empty() && pat.empty()) return true;
// 如果模式栈不空
// 仅当模式栈中的*可以‘吃掉’多余的字符时匹配成功
// 例如 aa* / aa*bb* ,而不可以是baa*
if(ori.empty() && !pat.empty()
&& pat.peek().equals('*')){
char c = pat.pop();
while (!pat.empty()){
if(c == '*'
|| pat.peek() == '*'
|| c == pat.peek())
c = pat.pop();
else return false;
}
return true;
}
// 其他情况均不成功
return false;
**/
}
算法学习之剑指offer(九)的更多相关文章
- 算法学习之剑指offer(十一)
一 题目描述 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推. import java.util.*; ...
- 算法学习之剑指offer(六)
题目1 题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. import java.util.*; public cl ...
- 算法学习之剑指offer(五)
题目1 题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. public class Solution ...
- 算法学习之剑指offer(四)
题目1 题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) /** public class TreeNode { int val = 0; Tree ...
- 算法学习之剑指offer(十二)
一 题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩 ...
- 算法学习之剑指offer(十)
一 题目描述 请实现一个函数用来判断字符串是否表示数值(包括整数和小数).例如,字符串"+100","5e2","-123","3 ...
- 算法学习之剑指offer(八)
题目一 题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没 ...
- 算法学习之剑指offer(七)
题目1 题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007取模的结果输出. 即输出P% ...
- 算法学习之剑指offer(三)
题目1 题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 如果一个整数不为0,那么这个整数至少有一位是1.如果我们把这个整数减1,那么原来处在整数最右边的1就会变为0,原来在 ...
随机推荐
- Spring boot拦截器的实现
Spring boot拦截器的实现 Spring boot自带HandlerInterceptor,可通过继承它来实现拦截功能,其的功能跟过滤器类似,但是提供更精细的的控制能力. 1.注册拦截器 @C ...
- F#周报2019年第37期
新闻 宣告ML.NET 1.4的预览版及更新模型构建器 .NET展示会:一系列的活动! Octopus入门版:对于小团队免费 宣告.NET Core 3.0预览版9 使用IntelliCode更简单地 ...
- [币严区块链]交易所钱包系统如何实现USDT自动归集操作
本文代码应用场景: 每个用户在交易所充值时,每个人都分配了独立的USDT地址,而交易所需要将所有独立的地址中USDT汇集到一个钱包地址(一般是冷钱包),从而实现资产归集与安全保障. 注意: 理解以下代 ...
- spring boot的多环境部署
需求:不同的环境有不同的开关属性,比如开发系统,需要关闭短信,微信的通知功能.而演示环境,线上环境则需要打开这些配置. 那么,如何做到呢?--->在properties.application配 ...
- Stanford公开课《编译原理》学习笔记(1~4课)
目录 一. 编译的基本流程 二. Lexical Analysis(词法分析阶段) 2.1 Lexical Specification(分词原则) 2.2 Finite Automata (典型分词算 ...
- 提交任务到spark(以wordcount为例)
1.首先需要搭建好hadoop+spark环境,并保证服务正常.本文以wordcount为例. 2.创建源文件,即输入源.hello.txt文件,内容如下: tom jerry henry jim s ...
- 【已解决】关于IDEA中 Tomcat 控制台打印日志中文乱码的解决
在 Idea 上面使用 Tomcat 时,发现控制台打印信息的时候,出行中文乱码问题; 可以通过以下几种解决办法 1:在-Dfile.encoding=UTF-8 在vm中设置编码方式 2.然后从Fi ...
- 用 C# 来守护 Python 进程
背景 目前我主要负责的一个项目是一个 C/S 架构的客户端开发,前端主要是通过 WPF 相关技术来实现,后端是通过 Python 来实现,前后端的数据通信则是通过 MQ 的方式来进行处理.由于 Pyt ...
- python学习笔记之zipfile模块
为什么学习: 在做自动化测试平台的apk上传功能部分时候,涉及到apk上传后提取apk的icon图标,通过aapt解析apk,获取对应icon在apk中的地址,通过python的zipfile模块来解 ...
- Docker系列(一):容器监控工具Weave Scope安装
项目进行容器化之后,配套的基础设施包括监控.编排.管理等都需要进行一并完善.这里也是自己一边学习一边进行记录. Weave Scope 的最大特点是会自动生成一张 Docker 容器地图,让我们能够直 ...