zoukankan      html  css  js  c++  java
  • Reverse Integer(反转整数)(leetcode)

    题目:

    Given a 32-bit signed integer, reverse digits of an integer.

    Example 1:

    Input: 123
    Output: 321
    

    Example 2:

    Input: -123
    Output: -321
    

    Example 3:

    Input: 120
    Output: 21
    

    Note:
    Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.(给一个整型的数,反转输出)

    方法一:数学方法;

    1、java遇到数据类型的范围问题,记得看api,和c不同。我之前以为−231应该是-2^31。结果java中是Integer.MIN_VALUE。

    时间复杂度:O(n)     运行时间:26ms

    方法二:栈

    1、遇到算法题首先要想到用到数据结构的知识。数组,栈,链表,队列,树,图,堆,散列表,堆,图。

      数组:在内存中可以连续存储多个元素。内存分配连续,通过下标访问。优点:按索引查询快,按索引遍历快。缺点:增删慢,无法扩容,只能存一种类型元素。

        频繁查询,对存储空间要求不大,很少增加和删除的情况。

      栈:先进后出     实现递归功能方面的场景

      队列:先进先出    多线程阻塞队列管理

      链表:增删快查询慢  数据量较小,需要频繁增加,删除操作的场景

      树:处理大批量的动态数据方面 有链表和数组的优点

      堆:做数组中的排序

    2、感觉自己对api不是很熟悉,栈和队列不知道该怎么用。当把所有数都放入队列后,输出的还是123时竟然想着用两个栈或者一个队列实现。也没想出我存进去一个取出来一个。思维不活跃啊。

    时间复杂度:o(n)            运行时间:17ms

    苟有恒,何必三更眠五更起;最无益,莫过一日暴十日寒。
  • 相关阅读:
    爬虫(一)—— 爬取一个简单的网站
    Neutron的安全组原理
    Neutron的防火墙原理
    Openstack Mitaka 负载均衡 LoadBalancerv2
    iptables(四)iptables匹配条件总结之一
    iptables(三)iptables规则管理(增、删、改)
    iptables(二)iptables实际操作之规则查询
    iptables(一)iptables概念
    opensack-mitaka网络性能测试shaker
    neutron二层网络实现
  • 原文地址:https://www.cnblogs.com/shaer/p/10409576.html
Copyright © 2011-2022 走看看