剑指offer_2.1_Day_5
输入一个链表,按链表从尾到头的顺序返回一个ArrayList。
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { ArrayList<Integer> num = new ArrayList<Integer>();
while(listNode!=null)
{
num.add(0,listNode.val);
listNode=listNode.next;
}
return num;
}
}
新建一个动态链表 ArrayList<Integer> ArrayList = new ArrayList<Integer>();
add添加是在最末尾,因为是从尾到头打印,添加一个0改为添加到首位。
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
TreeNode root=reConstructBTree(pre,0,pre.length-1,in,0,in.length-1);
return root;
}
private TreeNode reConstructBTree(int [] pre,int startPre,int endPre,int [] in,int startIn,int endIn) {
if(startPre>endPre||startIn>endIn)
return null;
TreeNode root=new TreeNode(pre[startPre]);
for(int i=startIn;i<=endIn;i++)
if(in[i]==pre[startPre]){
root.left=reConstructBTree(pre,startPre+1,startPre+i-startIn,in,startIn,i-1);
root.right=reConstructBTree(pre,i-startIn+startPre+1,endPre,in,i+1,endIn);
}
return root;
}
}
原理还是很简单的,因为是前序和中序,那么前序第一个是根节点,中序对应的位置前为左叉树,后为右叉树,以此进行递归。
求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
public class Solution {
public int Sum_Solution(int n) {
int sum =(int)(Math.pow(n,2)+n);
return sum>>1;
}
}
打个比方,1+到100是5050,×2就是10100,也就是100的平方加上100.也就是从1到100,可以看成,1到99再加上100,之后除以二。
public class Solution {
public int Sum_Solution(int n) {
int res = n;
boolean flag = (n>0)&&((res+=Sum_Solution(n-1))>0);
return res;
}
}
短路效果来实现递归。最后无法达成继续加法。
剑指offer_2.1_Day_5的更多相关文章
- 剑指offer_2.3_Day_6
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0). n<=39 public class Solution { public int Fibo ...
- 剑指Offer面试题:1.实现Singleton模式
说来惭愧,自己在毕业之前就该好好看看<剑指Offer>这本书的,但是各种原因就是没看,也因此错过了很多机会,后悔莫及.但是后悔是没用的,现在趁还有余力,把这本书好好看一遍,并通过C#通通实 ...
- 剑指Offer面试题:14.链表的倒数第k个节点
PS:这是一道出境率极高的题目,记得去年参加校园招聘时我看到了3次,但是每次写的都不完善. 一.题目:链表的倒数第k个节点 题目:输入一个链表,输出该链表中倒数第k个结点.为了符合大多数人的习惯,本题 ...
- 《剑指offer》面试题12:打印1到最大的n位数
面试题12:打印1到最大的n位数 剑指offer题目12,题目如下 输入数字n,按顺序打印出1到最大的n位十进制数,比如输入3,则打印出1,2,3一直到最大的三位数999 方法一 和面试题11< ...
- 《剑指offer》面试题11: 数值的整数次方
面试题11: 数值的整数次方 剑指offer面试题11,题目如下 实现函数double power(double base,int exponent),求base的exponent次方, 不得使用库 ...
- 剑指 Offer 题目汇总索引
剑指 Offer 总目录:(共50道大题) 1. 赋值运算符函数(或应说复制拷贝函数问题) 2. 实现 Singleton 模式 (C#) 3.二维数组中的查找 4.替换空格 ...
- 面试题目——《剑指Offer》
1.把一个字符串转换成整数——<剑指Offer>P29 2.求链表中的倒数第k个结点——<剑指Offer>P30 3.实现Singleton模式——<剑指Offer> ...
- 剑指offer习题集2
1.把数组排成最小的数 class Solution { public: static bool compare(const string& s1, const string& s2) ...
- 剑指offer习题集1
1.打印二叉树 程序很简单,但是其中犯了一个小错误,死活找不到,写代码要注意啊 这里左右子树,要注意是node->left,结果写成root->left vector<int> ...
随机推荐
- array_multisort 对关联数组进行排序的问题 PHP
我们在php的数组操作中经常用到对数组进行排序的问题,这里说的是对关联数组进行排序需要用到函数 array_multisort . array_multisort(array_column($arr, ...
- JAVA(windows)安装教程
JAVA(windows)安装教程 一.下载: https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133 ...
- spring事物(一),@EnableTransactionManagement @Transactional 启动解析
1.事物的声明阶段 @EnableTransactionManagement,是我们开启注解事物的第一步,我们来看下这个类为我们干了什么 @Target(ElementType.TYPE) @Rete ...
- 如何使用linux查看tomcat日志
- AndroidStudio3.0打开Android Device Monitor
相信很多更新了AndroidStudio3.0的小伙伴会发现无法在工具栏的的Tools->Android->device monitor,打开DeviceMonitor. 今天偶然看到 G ...
- println 与 print区别
------------恢复内容开始------------ println 与 print区别: 1.print输出之后不换行,如下: public class Newstart { publ ...
- 收藏!阿里云maven镜像配置文件
<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Soft ...
- System.Reflection.ReflectionTypeLoadException
引用了一个第三方dll, 此dll经过混淆,但是未签名. 然后,主程序无法反射了, 取消主程序的签名后正常反射.
- SQL——左连接(Left join)右连接(Right join)内连接(Inner join)
概念(定义) 首先还是介绍一下这三个的定义 Left join:即左连接,是以左表为基础,根据ON后给出的两表的条件将两表连接起来.结果会将左表所有的查询信息列出,而右表只列出ON后条件与左表满足 ...
- leetcode817 Linked List Components
""" We are given head, the head node of a linked list containing unique integer value ...