LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List
283. Move Zeroes
var moveZeroes = function(nums) {
var num1=0,num2=1;
while(num1!=num2){
nums.forEach(function(x,y){
if(x===0){
nums.splice(y,1);
nums.push(0);
}
num1 = nums ;
});
nums.forEach(function(x,y){
if(x===0){
nums.splice(y,1);
nums.push(0);
}
num2 = nums ;
});
}
};
这题本身并不难,只是方法要考虑好就最好了,用到方法forEach(),splice(),push()
349. Intersection of Two Arrays
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number[]}
*/
var intersection = function(nums1, nums2) {
var arrt1 = [], i = 0;
nums1.forEach(function(x,y){
nums2.forEach(function(z,v){
if(z==x){
arrt1[i]=x;
i++;
}
}); }); var ret = []; for (var k = 0; k < arrt1.length; k++) {
var item = arrt1[k];
if (ret.indexOf(item) === -1) {
ret.push(item);
}
} return ret; };
这题我的思路是先将两个数组递归遍历,将有重复的部分都存进某个数组,然后数组去重!但是这样在效率上很低,大约击败7%左右的人,仅仅是个可用但是不好用的方法
237. Delete Node in a Linked List
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} node
* @return {void} Do not return anything, modify node in-place instead.
*/
var deleteNode = function(node) {
node.val=node.next.val;
node.next=node.next.next;
};
【注】一道链表题,题目很简单,不过一开始没读懂。算是复习一下链表。
LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List的更多相关文章
- [LeetCode] 237. Delete Node in a Linked List 解题思路
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- [LeetCode] 237. Delete Node in a Linked List 删除链表的节点
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- 【一天一道LeetCode】#237. Delete Node in a Linked List
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- LeetCode 237. Delete Node in a Linked List (在链表中删除一个点)
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...
- [LeetCode&Python] Problem 283. Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- LeetCode Array Easy 283. Move Zeroes
Description Given an array nums, write a function to move all 0's to the end of it while maintaining ...
- LeetCode之237. Delete Node in a Linked List
------------------------------------------------ 因为不知道前序是谁,所以只好采用类似于数组实现的列表移动值, 又因为如果当前是最后一个元素了但是已经没 ...
- Java for LeetCode 237 Delete Node in a Linked List
Java实现如下: public class Solution { public void deleteNode(ListNode node) { if(node==null||node.next== ...
随机推荐
- Linux基础正则表达式字符汇整(characters)
RE 字符 意义与范例 ^word 意义:待搜寻的字串(word)在行首! 范例:搜寻行首为 # 开始的那一行,并列出行号 grep -n '^#' regular_express.txt word$ ...
- HBase多次加载-ROOT-和META的bug
执行以下case可以见到root或meta被加载两次: 1 kill掉root和meta表所在的rs 2 start该台rs 3 立即再次kill掉这台rs 4 立即再次start该台rs 原因: ...
- SharePoint 2007 文档库中的文档添加评论功能
背景:接到一个项目,要求文档管理,当然文档库就可以了,但是要求文档需要大家去读,读完以后还可以发表评论,这Moss貌似就有点困难了.和同事一起合计,想来想去也没有太好的办法,后来想到传统开发,两个表的 ...
- 关于masm中OFFSET伪指令对结构的影响
在masm中,如果offset修饰全局变量,则他返回的是变量的相对于其所在段的偏移,并且offset不能修饰局部变量哦. 若offset修饰的是文字常量则将被忽略: VAL = 1000h mov e ...
- MongoDB学习笔记(二)
一.Mongodb命令 说明:Mongodb命令是区分大小写的,使用的命名规则是驼峰命名法. 对于database和collection无需主动创建,在插入数据时,如果database和collect ...
- Odoo 学习 【二】Environment 概览
Environment 参考链接: http://odoo-new-api-guide-line.readthedocs.io/en/latest/environment.html#environme ...
- Web开发相关工具收集
FireFox相关: FireBug/GreaseMonkey/Yslow/WebDeveloperSelenium:Web应用程序测试的工具-- http://seleniumhq.org/ h ...
- Django1.10 release notes摘编
一.重点新特性: 1.面向PostgreSQL的全文搜索支持 2.新式风格的middleware 3.用户名Unicode编码的官方支持 二.一些可以提的改变: 1.用户名最大长度增加到150 2.不 ...
- ubuntu 13.10 install wireshark
ubuntu 13.10 install wireshark 今天在使用java jsoup操作remote server的是否,在本地执行可以成功返回内容,然后打成jar包,使用shell在 ser ...
- Flex 右键菜单控制
//设置监控右键菜单项 private function setUserMenuItem():void{ var contextMenu:ContextMenu = new ContextMenu() ...