Leetcode -- Day 45

Reverse num/word/bits

最近回国了 有点懒

Question 1

Reverse Integer

Reverse digits of an integer.

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

Be careful about some interesting cases. i.e.negative number, overflow case.

 1     public int reverse(int x) {
 2         if (x >= -9 && x <= 9){
 3             return x;
 4         }
 5
 6         boolean isNeg = false;
 7         if (x < 0){
 8             isNeg = true;
 9             x = 0 - x;
10         }
11
12         double result = 0;
13
14         while(x > 0){
15             result = result * 10 + x % 10;
16             x = x / 10;
17         }
18
19         if (isNeg){
20             return -result < Integer.MIN_VALUE ? 0 : -(int)result;
21         }
22         else{
23             return result > Integer.MAX_VALUE ? 0 : (int)result;
24         }
25     } 

Question 2

Reverse Words in a String

Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".

I use split to change it an word array, or you can use toCharArray() to change it to char array.

 1     public String reverseWords(String s) {
 2         StringBuffer result = "";
 3         String[] array = s.split(" ");
 4         for (int i = array.length - 1; i >= 0; i --){
 5             String temp = array[i].trim();
 6             if (temp.equals("") == false){
 7                 if (i != array.length - 1){
 8                     result += " " + temp;
 9                 }
10                 else{
11                     result += temp;
12                 }
13             }
14         }
15         return result.trim();
16     }
时间: 2024-07-29 07:02:22

Leetcode -- Day 45的相关文章

【一天一道LeetCode】#45. Jump Game II

一天一道LeetCode系列 (一)题目 Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the mi

LeetCode 55/45 Jump Game I/II-----Greedy**

一:Jump Game 题目: Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index.

[LeetCode#55, 45]Jump Game, Jump Game II

The problem: Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For

笔试题58. LeetCode OJ (45)

这个题目是一个特殊的跳台阶问题,给定一个数组,每个数组里面存放的是该位置可以跳的步数,求出跳到最后位置所需要的最少的步数.我看到这个题目的时候脑子里面的第一印象是递归,递归去求解肯定是可以的,于是我写了下面的代码(结果是超时!) 解法一,递归查找,时间效率太低了...不行 class Solution { public: int jump(vector<int>& nums) { /* 数组里面的内容代表给位置所能跳的最大步数,找出跳到最后位置所需要的最少步数 */ int len =

LeetCode(45): 跳跃游戏 II

Hard! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [2,3,1,1,4] 输出: 2 解释: 跳到最后一个位置的最小跳跃数是 2.   从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置. 说明: 假设你总是可以到达数组的最后一个位置. 解题思路: 这题是之前那道Jump Game 跳跃游戏 的延伸,那题是问能不能

LeetCode Python 位操作 1

Python 位操作: 按位与 &, 按位或 | 体会不到 按位异或 ^ num ^ num = 0 左移 << num << 1 == num * 2**1 右移 >> num >> 2 == num / 2**2 取反 ~ ~num == -(num + 1) 1. Single Number Given an array of integers, every element appears twice except for one. Find

BFS问题-LeetCode 55、45、5297、127、433、434(BFS)

[LeetCode #55]跳跃游戏 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: true 解释: 我们可以先跳 1 步,从位置 0 到达 位置 1, 然后再从位置 1 跳 3 步到达最后一个位置. class Solution { public: bool canJump(vector& nums) { int maxReach = nums[0];

LeetCode 45 Jump Game II(按照数组进行移动)

题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description 给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. 求解出从下标为0开始到下标到数组最后一个所需要的最少跳动次数! 1.当数组为空或者数组长度等于1时,不需要跳动,因此返回0 否则初始化step=1 2.初始化left=0 right = nums[0] 当left<=right时进入循环体中. 3.从第i=0开始,如果当前跳动距离大于数组长度则返回st

leetcode 45 Jump Game II ---- java

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps