记录0916和0917两天的笔试

0916

题目一
打印斐波那契数列

描述:输入一个整数n,输出是一个斐波那契数列的一个三角形

1
1,1,1
1,1,2,1,1
1,1,2,3,2,1,1
1,1,2,3,5,3,2,1,1

感觉自己的程序还是有点乱

代码如下
public static void print(TreeNode root) {
		System.out.print(root.val + ",");
		if (root.left != null)
			print(root.left);
		if (root.right != null)
			print(root.right);
	}

	public static void print(int n) {
		StringBuilder sb = new StringBuilder();
		//将该数字进行打印记录
		for (int i = 1; i <= n; i++) {
			int[] num = new int[2 * i - 1];
			int j = 0, m = 0;
			//将斐波那契每个元素计算出来,放置在数组中
			for (; j < i; j++) {
				if (j >= 2)
					num[j] = num[j - 1] + num[j - 2];
				else
					num[j] = 1;
				if (i != 1)
					sb.append(num[j] + ",");
				else
					sb.append(num[j]);
			}
			//斐波那契堆成数组元素计算
			for (int k = num.length - 1; k >= j; k--) {
				num[k] = num[m++];
			}
			//将后面的元素放入sb中
			for (; j < num.length; j++) {
				if (j != num.length - 1)
					sb.append(num[j] + ",");
				else
					sb.append(num[j]);
			}
			sb.append("\n");
		}
		System.out.println(sb.toString());
	}
二叉树找公共祖先

没做出来,在交卷后试着做了一下
题目描述如下:
在R星球有一类昆虫它是可以无性繁殖的,身为昆虫调查员的你接到了命令前往R星球去调查昆虫的繁殖情况
经过一系列的调查,你发现该种昆虫一生最多可以繁殖出两个子代。现在你发现了这种昆虫身上可能会带有一种病毒,上级已经给你发来通知,告诉你了两只带有该病毒昆虫的编号。
为了快速找出这种病毒你必须找到这两只昆虫的最近公共父辈(不会存在两只昆虫出现相同编号)。

其实就是一颗树求其最近的公共祖先,之前听左神的算法课,听得还挺清楚的,但是一做题就紧张,做不出来了[笑哭]。

代码如下
class TreeNode {
	TreeNode left;
	TreeNode right;
	int val;
	int deep;

	public TreeNode(int val, int deep) {
		this.val = val;
		this.deep = deep;
	}
}
public class Main{

	public static void main(String[] args) throws IOException {
		int[] num = { 3, 5, 6, -1, -1, 2, 7, -1, -1, 4, -1, -1, 1, 9, -1, -1, 8, -1, -1 };
		getNum();
	}
	//获取一颗树的高度
	public static TreeNode getdeep(TreeNode root, int n, int m) {
		if (root == null)
			return null;
		if (root.val == n || root.val == m)
			return root;

		TreeNode left = getdeep(root.left, n, m);
		TreeNode right = getdeep(root.right, n, m);
		if (left != null && right == null)
			return left;
		else if (left == null && right != null)
			return right;
		else
			return root;
	}
	//求两个节点的最近公共祖先,并打印
	public static void getNum() throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		String[] numstr = bf.readLine().split(", ");
		int[] num = new int[numstr.length];
		for (int i = 0; i < num.length; i++) {
			num[i] = Integer.valueOf(numstr[i]);
		}
		TreeNode root = createTree(num, 0);
		String[] temp = bf.readLine().split(" ");
		int n = Integer.valueOf(temp[0]);
		int m = Integer.valueOf(temp[1]);
		root = getdeep(root, n, m);
		System.out.println(root.val);
	}
	//根据一个数组构建一颗树,i表示当前数组所到达的位置
	static int index = 0;
	public static TreeNode createTree(int[] num, int i) {
		if (index > num.length)
			return null;

		if (num[index] == -1) {
			index++;
			return new TreeNode(-1, i);
		}

		TreeNode root = new TreeNode(num[index], i);
		index++;
		root.left = createTree(num, i + 1);
		root.right = createTree(num, i + 1);
		return root;
	}
}

0917

二分查找

一个简单的二分查找,做的时候有点小紧张,把mid放错位置了,真实尴尬。
还好今天的题目简单,给了一点信息

代码如下
public static int search(int[] nums, int target) {
		int n = nums.length;
		if (n <= 0)
			return -1;
		// write code here
		int left = 0;
		int right = n - 1;
		int mid = (left + right) / 2;
		while (left <= right) {
			if (nums[mid] == target)
				return mid;
			else if (nums[mid] > target) {
				right = mid - 1;
			} else {
				left = mid + 1;
			}
		}
		return -1;
	}
2的幂
代码如下
public static boolean isPowerOfTwo(int n) {
		// write code here
		if (n == 1)
			return true;
		int temp = 2;
		for (int i = 1; i <= n / 2; i++) {
			if (temp == n)
				return true;
			else if (temp > n)
				return false;
			else
				temp *= 2;
		}
		return false;
	}

这道题目是leetcode的原题,其实有更简单的方法,没想出来,贼气。

 public boolean isPowerOfTwo(int n) {
        if(n==0||n == Integer.MIN_VALUE)return false;
      else  return  (n&(n-1))==0;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值