leetcode day8

时间:2022-01-30 00:37:29

【83】 Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

思路: 使用一个临时指针next来进行while循环链表,当碰到相同的值时,指针的指针域指向next.next.next向前推进 ,如果碰到不同的,则next = next.next,让next等于当前不同的新节点。使用迭代和递归的时间复杂度是一样的‘

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head==null||head.next==null){
return head;
}
/*//recursive way
head.next = deleteDuplicates(head.next);
return head.val == head.next.val ? head.next : head;*/ //iterative way
ListNode node = head;
while(node.next!=null){
if(node.val==node.next.val){
node.next = node.next.next;
}else{
node = node.next;
}
}
return head; }
}

【70】Climbing Stairs

You are climbing a stair case. It takes n steps to reach to the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

思路:这个是斐波那契数列问题,只是前两个数是1、2.最好别用斐波那契额递归,时间会超出限制,用数组处理,只需明白思想是,后个数是前两个数的和

public class Solution {

public int climbStairs(int n) {
if(n == 0 || n == 1 || n == 2){return n;}
int[] mem = new int[n];
mem[0] = 1;
mem[1] = 2;
for(int i = 2; i < n; i++){
mem[i] = mem[i-1] + mem[i-2];
}
return mem[n-1];
}
}

leetcode day8的更多相关文章

  1. leetcode每日刷题计划-简单篇day8

    今天是纠结要不要新买手机的一天QAQ想了想还是算了吧,等自己赚钱买,加油 Num 70 爬楼梯 Climbing Stairs class Solution { public: int climbSt ...

  2. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  3. LeetCode All in One 题目讲解汇总&lpar;持续更新中&period;&period;&period;&rpar;

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  4. &lbrack;LeetCode&rsqb; Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  5. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  6. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  7. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  8. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  9. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

随机推荐

  1. MVC5 网站开发实践 2&period;2、管理员身份验证

    上次完成了管理员的登录,这次要解决对管理员登录后的验证,采用AuthorizeAttribute属性的方式.之前还要解决几个问题,然后才重写验证类,最后稍微改一下界面. 目录 MVC5 网站开发实践  ...

  2. 处理某个json文件的代码

    # encoding=utf-8 import json,re with open('E:\\weather53892_20114.json','r') as f: data= f.readlines ...

  3. TOAD FOR MYSQL 进行数据插入时乱码的解决办法---MariaDB 5&period;5

    最近使用mysql是发现插入的数据乱码,几经周折终于找到的解决方法,特作备忘. 开始有将mysql的字符集全部设置成utf8,如下: SHOW VARIABLES LIKE 'character_se ...

  4. iOS 第三方 需要 引用的库

    ================================================== AFNetWorking   是基于 nsurlconnection   所以不需要引入库 === ...

  5. vim 多行同时输入,且输入数值递增

    很有用的命令. 很给力的说. http://vim.wikia.com/wiki/Making_a_list_of_numbers 我在 html中需要增加新的标签的时候,就有用到过. 原来的html ...

  6. MegaCli 安装过程

    首先说下自己遇到的坑: 百度搜索了一篇关于安装 MegaCli 的文章,于是乎就开始安装,装完之后获取不到 raid 的信息,后来发现是版本问题,就又搜索了一堆文章,最后搞定了 [root@web-0 ...

  7. git知识总结-1&period;git基础之基本术语

    1.前言 git是一种分布式版本管理工具,本文主要是通过阅读博客中几篇讲述git的优秀文章,并对文章进行整理.提炼总结得出一份git的说明文档. 本文档介绍了git的基本原理及常用操作,目标是通过阅读 ...

  8. 2、每日复习点--ConcurrentHashMap vs HashMap vs LinkedHashMap vs HashTable

    HashMap: 查询和插入速度极快,但是线程不安全,在多线程情况下在扩容的情况下可能会形成闭环链路,耗光cpu资源. LinkedHashMap: 基本和HashMap实现类似,多了一个链表来维护元 ...

  9. mongodb cxx driver学习

    mongodb 增删改查 insert 向集合中增加一个文档 remove 删除文档 update 更新(修改)某些文档 文档替换 文档修改器,只修改文档某个部分 find 返回集合中所有文档 fin ...

  10. 1&period;Ansible安装以及配置

    本节内容以Centos7为系统环境进行讲解: 1.安装epel源,方便直接yum安装: wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun ...