【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 题目讲解汇总(持续更新中...)

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

  4. [LeetCode] 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. Linux学习 -- 服务管理

    1 服务分类 服务管理内容 启动 自启动 查询已安装的服务 RPM包安装的服务  自启动的   chkconfig --list   2345中如果是启用,代表下次开机会自启动 正在运行的服务   p ...

  2. LoadRunner监控Unix、Windows方法及常用性能指标

    目  录 一.LoadRunner监控Linux资源.... 3 (一).准备工作... 3 1.可以通过两种方法验证服务器上是否配置了rstatd守护程序:... 3 (2)使用find命令... ...

  3. JavaBean--JavaBean与表单

    SimpleBean.java: package cn.mldn.lxh.demo ; public class SimpleBean { private String name ; private ...

  4. 【转载】linux环境下为firefox/chrome浏览器安装flash player

    本文转载自 http://blog.sina.com.cn/s/blog_6ad624380102v1xf.html     firefox安装flash player的方法: 先到adobe网站上下 ...

  5. 用telnet命令,SMTP发送邮件

    邮件的发送是基于smtp协议的.邮件客户端软件给smtp服务器传送邮件和smtp服务器之间传送邮件也都是基于smtp协议的.邮件客户端软件接受邮件是主要基于pop3协议的. 下面介绍利用windows ...

  6. C#入门经典第八章面向对象编程简介-1

    面向对象编程(Object-Oriented Programming,OOP)技术 本章中的OPP实际上是.NET OOP,这里讲的一些技术不能应用于其他OOP环境.

  7. mysql 查询 字段的类型

    select column_name,data_type from information_schema.columnswhere table_name = '表名'

  8. 2016大连网络赛 Football Games

    Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) P ...

  9. 制作windows镜像

    下载包含windows驱动的iso: http://222.186.58.77/virtio-win-0.1-30.iso?fid=kF46uzxlPMrgvLDErP0ohhZYwAUASLoCAA ...

  10. Android - Get Bluetooth UUID for this device

    Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join th ...