茂升面试中遇到的算法题-2022

目录

一、删除链表重复元素

二、二叉树的右视图

三、无序数组寻找peek

四、合并两个链表L1和L2

五、链表求和

六、816消消乐

七、阿拉伯数字翻译成中文

八.有序查找数组中目标值


一、删除链表重复元素

输入:1-1-1-2-3-3-4-4-5 
输出:2-5
实现:

Class ListNode {
	int val;
	ListNode next;
}

Class Solution {
	public ListNode deleteDulElement(ListNode head) {
		if (head == null) {
			return null;
		}
		ListNode cur = head;
		ListNode pre = head;
		bool headFlag = false;
		if (heed.val == head.next.val) {
			headFlag = true;
		}

		while (cur != null && cur.next != null) {
			if (cur.val == cur.next.val) {
				int tmp = cur.val' 
				while (tmp == cur.next.val) {
					cur = cur.next;
				}
				pre.next = cur.next;
			} else {
				pre = cur; 
				cur = cur.next;
			}
		}
		// 1-2-5
		if (headFlag) {
			pre = pre.next;
			// 2-5
		}
		return pre;
	}
}

二、二叉树的右视图

给定一棵二叉树,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
输入:

  1       <---
 / \
2  3     <---
/     
5         <---
输出: [1, 3, 5]
 

/*
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */

class Solution {
	public List<Integer> print(TreeNode root) {
    	List<Integer> res = new ArrayList<>(); 
		if (root == null) {
			return ;
		}
		List
		Queue<Integer> queue = new LinkedList<>();

		queue.add(root);
		int count = 1;
		int last = 0;
		while (queue.size() > 0) {
			int c = 0
			for (int i = 0; i < count; i ++) {
				TreeNode node = queue.poll();
				if (last == i) {
					res.add(node.val);
				}
				if (node.left != null) {
					queue.add(node.left);
					c ++;
				}
				if (node.rigth != null) {
					queue.add(node.rigth);
					c ++;
				}
			}
			count = c;
			last = c - 1;
		}
		return res;
	}
}

三、无序数组寻找peek

有一个无序int数组,如果其中一个值大于等于他左右相邻的值(边界值只要满足单侧就可以),则认为他是peak点。数组可能包含多个peak点,在这种情况下,返回 任何一个peak 即可,要求时间复杂度为O(log(n))
例如
输入:{3,1,2,1,4,5,2,1,6}
输出:3 or 2 or 5 or 6 

四、合并两个链表L1和L2

输入: L1:1->2->3, L2:4->5->6

输出:1->2->3->4->5->6

五、链表求和

力扣题目链接

给你两个 非空 链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表

输入:l1 = [7,2,4,3], l2 = [5,6,4]
输出:[7,8,0,7]

六、816消消乐

输入: 88881616

输出:88

七、阿拉伯数字翻译成中文

输入:12395

输出:一万两千三百九十五

八、有序查找数组中目标值

输入:[1,2,3,4,6,7,8],6

输出:4

九、判断两个链表是否相交

十、字符串翻转

输入:"I am tom."

输出:"tom. am I"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值