LeetCode刷题1

in 力扣 with 2031 comments

题目描述

给你一个32位的有符号整数x,返回将x中的数字部分反转后的结果。
如果反转后整数超过32位的有符号整数的范围 [−2^31,2^31−1],就返回 0。
假设环境不允许存储64位整数(有符号或无符号)。


思路

1.数字反转
利用%和/,将x的最后一位数字提取出来,放到返回数字rev的最前面,由于是10进制整数,所以只要每次操作时*10进一位。

int digit = x % 10;// 提取最后一位数字
x /= 10;// x去除最后一位
rev = rev*10 + digit;// rev初始值为0,赋值给最终的数字

2.整数反转的范围限制

题目要求x是32位无符号整数,环境不允许存储64位整数
反转后整数超过32位的有符号整数的范围就返回0
官方思路
-2^31 <= rev * 10 + digit <= 2^31-1
不等式不成立则返回0
然后一堆花里胡哨数学操作ToT
推导成了
(-2^31)/10 <= rev <= (2^31-1)/10
写的乱主要是不会传图,划掉

//最终代码,这里INT_MIN和INT_MAX应该是c语言中定义的int型的最大最小值
int reverse(int x) {
    int rev = 0;
    while (x != 0) {
        if (rev < INT_MIN / 10 || rev > INT_MAX / 10) {
            return 0;
        }
        int digit = x % 10;
        x /= 10;
        rev = rev * 10 + digit;
    }
    return rev;
}

我的理解
在数字转化的过程中,只要x不为0,rev就会比原来的值大10倍+,即rev = rev*10 + digit,所以为了避免超出范围,只要将rev和极值的1/10进行比较(在x!=0的前提下)。
假如还需要缩小范围,可以1、极值/100;2、条件改为while(x/10 !=0),不过该情况下对于x==0要单独考虑

今日总结

  1. 对于算法这方面还有很多需要加强的地方,主要在于做题的思路方面,需要多加练习。
  2. C、Java的基础方面还很薄弱,需要加强,多加练习
  3. 中等难度的题目就一头雾水了[o(╥﹏╥)o],我可真菜
Responses / Cancel Reply
  1. diflucan buy online usa

    Reply
  2. Digital help: Some businesses or employees, moderately than having an assistant
    of their workplace, will rent a virtual assistant to
    keep track of their conferences, set up their calendars and
    conduct other work that can be carried out remotely.

    Reply
  3. Additionally, adopting wholesome habits can present worthwhile coping mechanisms,
    increase our resilience, and reduce the risk of creating mental health points.

    Additionally, installers should put on protective gear, together with a full mask that gives clear air
    while blocking the inhalation of any foam particulates.
    In the case of preventive psychiatry, promoting wholesome lifestyles is a key component.

    Reply
  4. Electricity can travel by means of your telephone, plumbing or any wires and shock you.
    With the arrival of jet travel and increased gateways in the late 1940s and early 1950s, Dooley
    expanded Grey Line's market throughout the United States and established Gray Line companies in Canada, Mexico, Hawaii
    and Puerto Rico. Make sure to ask if unlimited snacks and
    drinks are included in the worth, and if not, ask if the cruise line offers
    a snack or soda bundle deal, which permits unlimited snacking or soda purchases for a set worth per day.
    Discover a racing sport that will put your youngster and
    his pals into humorous body positions as they head for the end line.
    The major search engines will naturally index your sitemap, so its a great place
    to put all you important internal links in order that they get picked up and listed appropriately.
    It could possibly stand up like somewhat raised platform,
    bend to help the gadget on top of a television or lay straight
    to enable you to position the Camera on a flat surface, making it
    simple to put it where you like, provided it is dealing
    with the appropriate direction. The engine considerably improves help for
    ECMA-262: ECMAScript Language Specification normal, together with
    features new to the lately finalized Fifth Edition of ECMA-262 (typically abbreviated ES5).

    Reply
  5. There is a large number of configurations for booths.
    Often, these smaller, modular versions will be damaged down and used
    as two tabletop booths as properly. Particular person-to-person transactions
    by folks you already know, or via on-line and print classifieds could be an excellent choice if you wish to avoid a dealership.

    You need your booth to look extra like a billboard than a brochure.

    Reply
  6. Notably, the Wielbark culture used each rituals. In The new Republic, Laura
    Marsh recalls the satirical, subversive coloring books from
    the 1960s that time the finger "Mad Men"-fashion at dysfunctional American culture.
    This signature tune of Starr's shot to the number one
    position on the Billboard Sizzling 100 chart and sat there for three for three weeks in 1970
    and still has a lasting influence on common tradition.
    The group was in a position to take no less than one scrap
    before they left for Buddah Records. Throughout the first few years of The Spinners' existence, the R&B group had some hits, like "That is what Women Are Made For", "I will All the time Love You",
    and "Really Yours". Made up of Berry Gordy, Alphonso Mizell, Freddie Perren, Deke Richards and generally
    with Perren's spouse, Christine Yarian, the Company wrote,
    produced and organized the Jackson 5 quantity-one
    hit singles "I want You Again", "The Love You Save" and "ABC".

    Reply
  7. A: Adults with disorganized attachment could struggle with emotional regulation, have difficulties trusting others, and
    exhibit inconsistent or unpredictable behavior in relationships.

    People with a disorganized attachment model could also be at a better risk of developing
    mental health disorders, experiencing difficulties in relationships,
    and struggling with emotional regulation. Therapy and self-reflection might help individuals understand their attachment patterns and work
    in direction of healing and constructing healthier relationships.

    Reply
  8. Viasat Discover is a 24-hour channel broadcasting in Central and
    Jap Europe, Scandinavia, Russia and Commonwealth of Independent States.

    In this text, we'll discover how far synthetic gravity has come because the Rotor
    Experience, as well as what the future would possibly hold for
    this promising technology. Are there any advancements in expertise or security measures geared toward decreasing the risk of business airplane
    crashes? Implementing rigorous security protocols, bettering technology and enhancing pilot training are important for lowering the chance of airplane crashes.
    That accounted for 26.3 p.c of crashes ascribed to pilot error.
    Connell, Rich. "Controller Says Her Error Brought on Runway Collision."Los
    Angeles Instances. They didn't notice their mistake
    until the aircraft abruptly lost power 2.Four nautical miles
    (4.Forty four kilometers) from the runway of their destination. The pilot got here on the general public-handle system to clarify that another jet was on the runway the
    place we were presupposed to land. There's also a uncommon, however much
    more disturbing, situation, wherein a suicidal pilot decides
    to take his life and those of his crew and passengers
    by intentionally crashing the aircraft. It had been a scary second, however
    I was reassured by how the pilot had noticed the problem and used his skills to keep
    us secure.

    Reply
  9. You might want to use a hooded towel for added warmth round your baby's head.
    The dragon pistol is a particular handgun that

    true to its identify - is designed like the head of a
    dragon. Special tools used is a map holder connected to the handlebar of the
    bike. However the objects which have supplied archaeologists with probably the most new details about life within the Bronze Age
    are the plant-based textiles. Shopping for the
    Engagement ring Chicago might be fairly a job if you are completely unprepared whenever you visit the store
    to purchase the ring. Each child ought to spread his ft apart, bend at the
    waist until he can touch the ground, after which grab his ankles.
    As a result of the whole lot was scorched after which sunk
    into anaerobic mud, the belongings of the Should Farmers show
    barely any decay, even after three millennia of sitting round
    in a bog. Three thousand years in the past, a small Bronze Age settlement burned down within the Cambridgeshire Fens
    close to modern day Cambridge, England. Set the rock on prime of the glue,
    holding it down until the glue cools. Discover the images you
    need to make use of and, utilizing the bottle cap as a reference,
    reduce out a number of tiny photos and put aside.

    Reply
  10. It's the village tradition of Rajasthan which is the USP of the
    state tourism. You possibly can fulfill the necessity to bond by creating
    a corporate tradition that is primarily based on mutual respect and help.
    Pole help holds the pole in place, limitations protect
    the pole and are the largest part of the protection, interference harass attackers that get
    inside the barrier, scrum disablers get rid of the offensive tactic of spring boarding,
    and the ninja (easily one of the best place) sits atop the pole and leans
    to the alternative aspect if it is being tilted to counteract the load.

    Next, we'll tell you the way to get your utility for financial aid rolling.
    England has invented implausible sports akin to soccer, cricket,
    rugby, tennis and squash, and now you can add cheese rolling to
    the listing. Every year, there is a race held close to Gloucester in England, which sees
    a nine pound wheel of Double Gloucester cheese rolled down the hill.
    Contestants must then chase after the cheese, with the primary
    particular person crossing the finish line at the underside winning, you guessed it,
    the cheese. A deeper look into the primary use of the phrase reveals the political and racial climate in the U.S.

    Reply