The 6th Week of ARTS:7-ReverseInteger And 9-PalindromeNumber
Introduction
- Algorithm - Learning Algorithm
- Review - Learning English
- Tip - Learning Techniques
- Share - Learning Influence
Let's do it!!!
Algorithm
ReverseInteger
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
- My Solution
package com.silence.arts.leetcode.first;
/**
* <br>
* <b>Function:</b><br>
* <b>Author:</b>@author Silence<br>
* <b>Date:</b>2018-07-18 23:52<br>
* <b>Desc:</b>无<br>
*/
public class ReverseInteger_7 {
/**
* 判断数字是否是负数,负数则截取第一位符号位
* @param x
* @return
*/
public static int reverse(int x) {
int resultNext;
try {
if (x < 0) {
String string = String.valueOf(x);
String prev = string.substring(0, 1);
StringBuilder next = new StringBuilder(string.substring(1));
resultNext = Integer.parseInt(prev + Integer.parseInt(next.reverse().toString()));
}else {
StringBuilder string = new StringBuilder(String.valueOf(x));
resultNext = Integer.parseInt(string.reverse().toString());
}
}catch (Exception e) {
resultNext = 0;
}
return resultNext;
}
public static void main(String[] args) {
System.out.println(reverse(1534236469));
}
}
Thinking
- Transfer int to String
- Reverse the String and Splice minus sign if it has
思路
- 将整型转换为字符串
- 反转字符串并且组装符号位(如果为负数的话)
PalindromeNumber
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:
Input: 121
Output: true
Example 2:
Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
- My Solution
package com.silence.arts.leetcode.first;
/**
* <br>
* <b>Function:</b><br>
* <b>Author:</b>@author Silence<br>
* <b>Date:</b>2018-07-31 23:38<br>
* <b>Desc:</b>无<br>
*/
public class PalindromeNumber_9 {
/**
* 转换为字符串,然后反转进行比较
* 注意before.reverse() 会将before改变,不会生成新的内存对象
*
* @param x
* @return
*/
public static boolean isPalindrome(int x) {
String b = String.valueOf(x);
StringBuilder before = new StringBuilder(b);
String after = before.reverse().toString();
return b.equals(after);
}
public static void main(String[] args) {
System.out.println(isPalindrome(-121));
}
}
Thinking
- Transfer int to String;
- Reverse the String and then compare it with original String.
思路
- 将数字转换成字符串
- 反转字符串与反转前的字符串进行比较
Review
10 Common Software Architectural Patterns in a nutshell
This author explains 10 different kinds of Architectural Patterns briefly. Here are they.
- Layered pattern
- Client-server pattern
- Master-slave pattern
- Pipe-filter pattern
- Broker pattern
- Peer-to-peer pattern
- Event-bus pattern
- Model-view-controller pattern
- Blackboard pattern
- Interpreter pattern
Tips
- jQuery prop()
//this code is not working you should use below code.
//$('#abc').attr('checked', true);
//It's working!
$('#abc').prop('checked', true);
- This is an extension of Chrome that will help you to find the good project on Github weekly. Something like this.
- This is a very good Free online drawing website. It also can be a real-time collaboration website. You can use it for yourself also you can use it with your friends together.
Share
Sharing
Last week was a busy week. I didn't do anything extra but work. Nothing else to share.
One more thing
- Personal Medium Home Page: https://medium.com/@zhuxiang134
- Personal Website: https://zxsilence.cn/
标题:The 6th Week of ARTS:7-ReverseInteger And 9-PalindromeNumber
作者:zhuSilence
地址:https://home.zxsilence.cn/articles/2018/08/05/1570340341441.html