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 ...
随机推荐
- haxe 配置
安装所有类库: 命令提示符 haxelib install 类库名 如:haxelib install openfl 配置安卓: 命令提示符 haxelib run openfl setup andr ...
- Python -- OOP高级 -- 枚举类
Enum可以把一组相关常量定义在一个class中,且class不可变,而且成员可以直接比较. from enum import Enum Month = Enum('Month', ('Jan', ' ...
- oracle xe client 如何设置 tnsnames.ora(解决无法使用pl/sql developer的问题)
10.2版本xe的服务器和客户端安装都很方便,由于xe的服务器只允许建立一个实例,实例名字会直接默认为xe,客户端默认安装在C:\XEClient目录下,使用sqlplus连接服务器: sqlplus ...
- Android 学习 之 无需类名启动其他程序
在网上搜索了一会相关的实现代码,发现所有的文章都说是需要包名和类名.但是人家的程序,我们怎么可能知道哪个是第一个启动的Activity?所以,真正用在项目上,那种方法基本上没什么用的.于是查看官方文档 ...
- 【转】使用gulp 进行ES6开发
原谅地址:https://segmentfault.com/a/1190000004394726 一说起ES6,总会顺带看到webpack.babel.browserify还有一些认都不认识的blab ...
- Chapter 1 First Sight——12
Breakfast with Charlie was a quiet event. 和查理斯吃早饭时一件安静的事情 He wished me good luck at school. I thanke ...
- EM阅读资料
1,从最大似然到EM算法浅解 2,(EM算法)The EM Algorithm 3,数据挖掘十大算法----EM算法(最大期望算法) (番外)最大后验估计(MAP)
- sqlserver2008用bat脚本备份时报错因为库名有中横杠【原创】
提示原因是数据库名字有中横岗“-” 解决方法:用中括号把名字括起来就可以了 call :backupone [数据库名-new] 完整备份脚本如下 @ECHO ON set DATE=%date:/= ...
- Mysql中int(2)和int(10)的区别
int(N)中的N不是限制字段取值范围的,int的取值范围是固定的(0至4294967295)或(-2147483648至2147483647) 那么N这个值是为了在字段中的值不够时补零的,但是必须含 ...
- javascript 中 apply(或call)方法的用途----对象的继承
一直以来,我的理解就是 js中的Function.apply(或者是Function.call)方法是来改变Function 这个函数的执行上下文(excute Context),说白了,就是改变执 ...