1. 程式人生 > >LeetCode 演算法刷題(7)

LeetCode 演算法刷題(7)

7. 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.

Subscribe to see which companies asked this question.

public class Test7 {
    public static void main(String[] args){
        Test7 test = new Test7();
        System.out.print(test.reverse(-10));
    }

    public int reverse(int x) {
        try{
            int index = 0;
            if(x<0){
                index = 1;
            }
            String s = String.valueOf(x);
            char[] c = s.toCharArray();
            int last = s.length()-1;
            for(int i=index,j=last;i<=j;i++,j--){
                char temp = c[i];
                c[i] = c[j];
                c[j] = temp;
            }
            return new Integer(Integer.parseInt(new String(c)));
        }catch(NumberFormatException e){
            return 0;
        }

    }
}


相關推薦

LeetCode 演算法(7)

7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123,

leetcode演算法-持續更新

最近,聽了同學的建議準備刷leetcode的演算法題目,提高下自己的演算法能力。 不多說,直接來題: 一. Given an array of integers, return indices of the two numbers such that they add

演算法LeetCode中常見的動態規劃題目

動態規劃刷題筆記 53. Maximum Subarray 題目描述 Find the contiguous subarray within an array (containing at least one number) which has t

[LeetCode][Python]記錄 1. 兩數之和

ron 題記 細節 重復 給定 假設 利用 tar 分享圖片 第一次做發現很多小細節以前都沒註意過,感覺還是蠻頭疼的。 題目: 給定一個整數數組和一個目標值,找出數組中和為目標值的兩個數。 你可以假設每個輸入只對應一種答案,且同樣的元素不能被重復利用。 根據題目要求

leetcode 110筆記——dfs——平衡二叉樹判斷問題

  在這道題中,我整整是做了一天的時間來完成它,首先我先了解了什麼事平衡二叉樹,複習了一遍二叉樹的相關知識,也算是鞏固吧,這道題中我用了整整兩邊遞迴,一個遞迴是求樹的高度,一個是判斷樹是否為平衡二叉樹,在這個題中為了更好的瞭解遞迴,可以把遞迴看做成一個黑盒子,不必細瞭解其內部的呼

leetcode easy心得

持續更新 67.二進位制求和 給定兩個二進位制,返回他們的和。輸入為非空字串並且只包含1和0. class Solution { public String addBinary(String a, String b) { StringBuilder result = new

LeetCode-Easy(32) Linked List Cycle

Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 給定一個連結串列,確定它是否有一個迴圈

LeetCode-Easy(31) Single Number

Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a li

LeetCode-Easy(30) Best Time to Buy and Sell Stock II

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximu

LeetCode-Easy(29) Best Time to Buy and Sell Stock

Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete

LeetCode-Easy(28) Pascal's Triangle II

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. N

LeetCode-Easy(27) Pascal's Triangle

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1]

LeetCode-Easy(26) Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given

LeetCode-Easy(25) Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the

LeetCode-Easy(24) Balanced Binary Tree

Given a binary tree, determine if it is height-balanced. For this pro

LeetCode-Easy(23) Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 將有序陣列轉化為二分查詢樹

LeetCode-Easy(33) Min Stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop()

機器學習,大資料,深度學習 競賽網站,學習網站,演算法網站

資料競賽類網站 Kaggle 阿里巴巴天池大資料比賽 DataCastle CCF大資料與計算智慧大賽 DataFountain Di-Tech演算法大賽 KDD-Cup KDnuggets Competition 全國高校雲端計算應用創

leetcode(python & java)解析:【兩數之和】 重點【Hash】

題目描述 給定一個整數陣列和一個目標值,找出陣列中和為目標值的兩個數。 你可以假設每個輸入只對應一種答案,且同樣的元素不能被重複利用。 Given an array of integers, return indices of the two numbers s

leetcode(python & java)解析:【無重複字元的最長字串】 重點【滑動視窗】

給定一個字串,找出不含有重複字元的最長子串的長度。 Given a string, find the length of the longest substring without repeating characters. 示例1 輸入: "abcabcbb"