目录
一、删除链表重复元素
输入: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"