leetcode练习:2017/09/21~09/22

Reverse Integer

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.

var min_32 = 1<<31;
var max_32 = parseInt(‘7fffffff‘,16);
var reverse = function(x) {
    var syn=0;
    if(x < 0){
        x = Math.abs(x);
        syn=1;
    }

    x = x + "";
    var len = x.length;

    var arr = x.split(‘‘);
    for(var i=0; i<len/2; i++){
        var temp = x[i];
        arr.splice(i,1,x[len-i-1]);
        arr.splice(len-i-1,1,temp);
    }

    x = parseInt(arr.join(""));

    if(x> max_32) {
        return 0;
    }

    if(x< min_32){
      return 0;
    } 

    if(!syn)
        return x;
    else
        return -x;
};

Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space.

var isPalindrome = function(x) {
    if(x<0) return false;
    var a = [];
    var n = 10;
    while(x>0){
        a.push(x%10);
        x = parseInt(x / 10);
    }
    var len = a.length;
    for(var i=0;i<len/2;i++){
      if(a[i] != a[len-1-i])
        return false;
    }
    return true;

};
时间: 2024-07-29 15:37:43

leetcode练习:2017/09/21~09/22的相关文章

2016.09.21 公司裁员想到的

公司最近裁员,好多同事都走了. 想到了,不管在那里都要努力工作,成为该领域的专家,才能立于不败之地. 得之何喜,失之何忧. 加油,最好自己,无愧我心. 不断进步,不断上升 2016.09.21 晚 于北京朝阳

Idea中使用jdbc报错:Wed Mar 21 09:28:33 CST 2018 WARN: Establishing SSL connection without server&#39;s identity verification is not recommended...

报错提示: Sat Oct 21 09:28:33 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit opti

2017年5月22号课堂笔记

2017年5月22号 星期一 大雨 内容:盒子模型,浮动 备注:5月24日补上 一.盒子模型 01.边框border 仿写老师代码: <!DOCTYPE html> <html><head lang="en"> <meta charset="UTF-8"> <title>边框</title> <style type="text/css"> div{ /* 上 to

2017年3月22号课堂笔记

2017年3月22号         星期三          重度雾霾 内容:传递参数的两种方式,对象数组的理解,面向对象思想在编写代码中的应用,方法重载 Tips:ctrl+O--->显示该类所有方法 一.Demo01(传参数(开始位置.结束位置.查找的姓名),输入新.旧姓名进行修改并显示是否修改成功) 1.老师代码: /** * 实现学生的信息管理 */public class StudentBiz { // 创建一个数组 用来保存 所有学生的姓名 String[] names = new

[LeetCode] Search Insert Position [21]

题目 Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples. [1,3,5,6], 5 → 2 [1,3,5,6]

[题解]线段树专题测试2017.1.21

很单纯的一道线段树题.稍微改一下pushDown()就行了. Code(线段树模板竟然没超100行) 1 #include<iostream> 2 #include<sstream> 3 #include<cstdio> 4 #include<cmath> 5 #include<cstdlib> 6 #include<cstring> 7 #include<cctype> 8 #include<queue> 9

2017.1.21总结

---恢复内容开始--- 今天写了2道题,一道p1227 一道p1919 p1227原题: 给出一有向图,图中每条边都被标上了关系运算符'<','>','='.现在要给图中每个顶点标上一个大于等于0,小于等于k的某个整数使所有边上的符号得到满足.若存在这样的k,则求最小的k,若任何k都无法满足则输出NO. 例如下表中最小的k为2. 结点1>结点2结点2>结点3结点2>结点4结点3=结点4 如果存在这样的k,输出最小的k值:否则输出'NO'. 这道题是差分约束+拓扑排序题目,需

2017年7月22日~23日,深圳市共创力为某上市企业提供整机设计工程内训服务!

2017年7月22日~23日,深圳市共创力咨询资深顾问李齐天老师为某上市企业提供了<整机系统设计方法与实践>的内训服务!该公司是武汉光谷著名的通信企业,研发团队超过1000多人,此次培训的引进公司领导层经过多方的选择和考察,最终选定李齐天老师为主讲老师. 2017年7月22日上午9点,公共研发部副总经理刘总宣布培训正式开始,接着共创力助教蔷薇对本次培训的目标进行宣读,正式进入了为期两天的整机系统设计培训之旅.李齐天老师分别从IPD流程.MM流程.OR流程.整机SE的开发设计活动.模板与案例演练

SICP 1.21 1.22 1.23 1.24

解:相关代码如下,时间测不出来 #lang racket (define (square x)   (* x x)) (define (smallest-divisor n)   (define (divides? a b)     (= (remainder b a) 0))   (define (next divisor)     (if (= divisor 2)         3         (+ divisor 2)))   (define (find-divisor n tes