leetcode day8
【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的更多相关文章
- leetcode每日刷题计划-简单篇day8
今天是纠结要不要新买手机的一天QAQ想了想还是算了吧,等自己赚钱买,加油 Num 70 爬楼梯 Climbing Stairs class Solution { public: int climbSt ...
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
随机推荐
- 转:Warning -26490: File name in a multipart submit is missing or empty.解决方法
录制测试上传文件脚本,回放报Warning -26490: File name in a multipart submit is missing or empty. Using an empty fi ...
- 43个优秀的Swift开源项目推荐(转载)
["轮子"] 工具类 SwiftyJSON:GitHub 上最为开发者认可的 JSON 解析类 Dollar.swift:Swift 版 Lo-Dash (或 underscore ...
- HDU 4685 Prince and Princess
强连通分量,看大神的题解才会写的.... http://www.cnblogs.com/kuangbin/p/3261157.html 数据量有点大,第一次Submit 2995ms过的,时限3000 ...
- R语言——绘图函数深入学习
利用R自带数据集 通过data()函数可以查看R自带数据集. > data() 返回以下结果,每一条记录都是一个数据,键入相应的数据名称可以查看具体信息. Data sets in packag ...
- docker入门实战笔记
1.什么是docker: docker翻译为搬运工,在这里应该可以理解为搬运应用的工具,也就是云.先了解其运用场景之后更容易对他形成深刻理解. Docker提供了一种可移植的配置标准化机制,允许你一致 ...
- Java中使用poi导入、导出Excel
一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际 ...
- (转)Permission denied: win7下面eclipse上传本地文件到DFS && 运行M/R程序时出现的同样的错误解决方法
原文地址: http://mntms.iteye.com/blog/2095651 hadoopeclipse远程控制权限 情景一: 当在win7下面的eclipse装好插件,首次运行M/R程序的时 ...
- 各种命令,以及FAQ..持续更新.....
Linux 篇: CentOs 7 修改主机名 hostnamectl --static set-hostname <host-name> 统计最多的10条记录 awk '{print $ ...
- Sybase数据库的分页功能
项目中需要用到Sybase数据库的分页功能,想尽各种办法都没有成功,最后用如下的存储过程成功实现功能,记录备忘. ),@start int, @pageSize int as begin declar ...
- 树列复选框Extjs
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...