力扣经典150题(1)

6.Z字形变换

将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行 Z 字形排列。

比如输入字符串为 “PAYPALISHIRING” 行数为 3 时,排列如下:

P   A   H   N
A P L S I I G
Y   I   R

之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:“PAHNAPLSIIGYIR”。

请你实现这个将字符串进行指定行数变换的函数:string convert(string s, int numRows);

输入:s = "PAYPALISHIRING", numRows = 3
输出:"PAHNAPLSIIGYIR"

输入:s = "PAYPALISHIRING", numRows = 4
输出:"PINALSIGYAHRPI"
解释:
P     I    N
A   L S  I G
Y A   H R
P     I

输入:s = "A", numRows = 1
输出:"A"

82.删除排序链表中的重复元素||

给定一个已排序的链表的头 head , 删除原始链表中所有重复数字的节点,只留下不同的数字 。返回 已排序的链表 。
在这里插入图片描述

输入:head = [1,2,3,3,4,4,5]
输出:[1,2,5]

在这里插入图片描述

输入:head = [1,1,1,2,3]
输出:[2,3]
/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode() {}
 * ListNode(int val) { this.val = val; }
 * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) {
            return head;
        }
        ListNode node = new ListNode(0, head);

        ListNode cur = node;

        // 遍历链表,直到倒数第二个节点
        while (cur.next != null && cur.next.next != null) {
            // 如果当前节点和下一个节点的值相同
            if (cur.next.val == cur.next.next.val) {
                // 记录重复值
                int x = cur.next.val;
                // 删除所有重复的节点
                while (cur.next != null && cur.next.val == x) {
                    cur.next = cur.next.next;
                }
            } else {
                cur = cur.next;
            }
        }

        return node.next;
    }
}

61.旋转链表

给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置。
在这里插入图片描述

输入:head = [1,2,3,4,5], k = 2
输出:[4,5,1,2,3]

在这里插入图片描述

输入:head = [0,1,2], k = 4
输出:[2,0,1]
class Solution {
    public ListNode rotateRight(ListNode head, int k) {
        if (k == 0 || head == null || head.next == null) { // 如果旋转次数为0,或者链表为空或只有一个节点,直接返回原链表
            return head;
        }
        int n = 1; // 初始化链表长度为1
        ListNode iter = head; // 初始化迭代器指向头节点
        while (iter.next != null) { // 遍历链表,计算链表长度
            iter = iter.next;
            n++;
        }

        int add = n - k % n; // 计算需要旋转的次数
        if (add == n) { // 如果需要旋转的次数等于链表长度,说明不需要旋转,直接返回原链表
            return head;
        }

        iter.next = head; // 将尾节点指向头节点,形成环状链表
        while (add-- > 0) { // 找到新的头节点和尾节点的位置
            iter = iter.next;
        }
        ListNode ret = iter.next; // 新的头节点
        iter.next = null; // 断开环状链表

        return ret; // 返回新的头节点
    }

}

100.相同的树

给你两棵二叉树的根节点 pq ,编写一个函数来检验这两棵树是否相同。
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
在这里插入图片描述

输入:p = [1,2,3], q = [1,2,3]
输出:true

在这里插入图片描述

输入:p = [1,2], q = [1,null,2]
输出:false

在这里插入图片描述

输入:p = [1,2,1], q = [1,1,2]
输出:false
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 * int val;
 * TreeNode left;
 * TreeNode right;
 * TreeNode() {}
 * TreeNode(int val) { this.val = val; }
 * TreeNode(int val, TreeNode left, TreeNode right) {
 * this.val = val;
 * this.left = left;
 * this.right = right;
 * }
 * }
 */
class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        // 如果两个节点都为空,说明它们相同
        if(p==null && q==null){
            return true;
        }else if(p==null || q==null){ // 如果其中一个节点为空,另一个不为空,说明它们不相同
            return false;
        }else if(p.val!=q.val){ // 如果两个节点的值不相等,说明它们不相同
            return false;
        }else{ // 如果两个节点的值相等,递归比较它们的左右子树
            return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值