每周完成一个ARTS:
Algorithm:每周至少做一个LeetCode算法题;
Review:阅读并评论至少一篇英文技术文章;
Tip:学习至少一个技术技巧;
Share:分享一篇有观点和思考的技术文章;
Algorithm
7. Reverse Integer:Given a 32-bit signed integer, reverse digits of an integer.
Note:
Assume we are dealing with an environment which could only store
integers within the 32-bit signed integer range: [−2^31, 2^31 − 1].
For the purpose of this problem, assume that your function returns 0
when the reversed integer overflows.
即将一个32位有符号整数反转输出。
package leetcode_Alg;
import java.util.ArrayList;
import java.util.Arrays;
public class L7_Reverse_Integer {
/*
* 1:首先判断正负,然后将整数的每一位存到数组里面,然后逆序输出
*/
public static int reverse1(int x) {
int y=0;
/*int[] a=new int[8]; */
//如何定义一个数组
ArrayList list = new ArrayList();
if (x>0){
int i=0;
while((x/((int)(Math.pow(10,i))))>=1){
// a[i]=(x/((int)(Math.pow(10,i))))%10;
list.add((x/((int)(Math.pow(10,i))))%10);
i++;
};
}else if (x==0){
y=0;
}else if(x<0){
int i=0;
int xx=Math.abs(x);
while((xx/((int)(Math.pow(10,i))))>=1){
list.add((xx/((int)(Math.pow(10,i))))%10);
i++;
};
}
int l=0;
for (int j=list.size()-1;j>=0;j--){
// y=(int) (y+Math.pow(10,j)*a[l]);
y=(int) (y+Math.pow(10,j)*( Integer.parseInt(list.get(l).toString())));
l++;
}
if (x>=0){
return y;
}else{return -y;}
}
/**
* 2别人方法:
* @param x
* @return
*/
public static int reverse2(int x) {
int sum = 0;
while (x != 0) {
//Integer.MAX_VALUE=2^31 − 1 , Integer.MIN_VALUE=−2^31
//&& 优先级高于||
if (sum > Integer.MAX_VALUE / 10 || sum == Integer.MAX_VALUE / 10 && x % 10 > 7
|| sum < Integer.MIN_VALUE / 10 || sum == Integer.MIN_VALUE / 10 && x % 10 < -8) {
return 0;
}
sum = 10 * sum + x % 10;
x /= 10;
}
return sum;
}
/**
* 3别人方法:
* @param x
* @return
*/
public static int reverse3(int x)
{
int result = 0;
while (x != 0)
{
int tail = x % 10;
int newResult = result * 10 + tail;
if ((newResult - tail) / 10 != result)
{ return 0; }
result = newResult;
x = x / 10;
}
return result;
}
public static void main(String[] args){
int x=2147483647;
System.out.println(x);
System.out.println(reverse2(x));
}
}
其实这个题目最大的坑就在于32位有符号的整数逆转导致的溢出问题,但是我一开始就想着怎么逆转,写了个贼啰嗦的程序,然后完全想不到溢出问题,所以我的基础真的是个又菜又坑。
Review
https://dzone.com/articles/the-difference-between-microservices-and-web-servi
仓促间看了一篇,作者以一个小的零售商系统为例引出microservices,并简单比较了microservices和web services,简单讲述了在开发一个工程时如何选择一个这两个框架,因为这两个都是将一个整体划分成能够通过中介相互通信的个体的架构,只不过web services是活跃在network上的。
最后,作者告诉我们,anyway,条条大路通罗马。
感觉下次可以看一些专题相关的经典原文,不然仅仅做了个速读。
Tip
在做算法练习的时候遇到两个问题
如何定义不定长数组
如何判断整数溢出
(1) 如何定义不定长数组
是一种容器,其中所有的元素具有相同的数据类型;一旦创建,不能改变容器大小。
<类型>[]<名字>=new <类型>[元素个数];
int[] arr=new int[10];
但是有的时候我们需要的是一个不定长数组,可以使用使用java.util.Vector 或 ArrayList、LinkedList在使用之后,数组长度不再变化时,用toArray方法转成固定数组。
Vector a = new Vector(0,1);
a.add(int index, Object element);
ArrayList a=new ArrayList();
a.add(Object x);
(2)如何判断整数溢出
在java中int整数型一般占4个字节,32位,计算机中以补码的形式存储,正数补码为源码,负数为反码加1,所以,有符号取值范围为-2^31 ~ 2^31-1,整数溢出可分为上溢和下溢,即大于 2^31-1和小于-2^31。比如abs(-2^31)将会会产生溢出异常从而超时。
以相加a1+a2为例
if(a1>Integer.MAX_VALUE-a2)
System.out.println("上溢出");
if(a1<Integer.MIN_VALUE-a2)
System.out.println("下溢出");
JDK8中已经有写好的函数可以判断是否溢出,可以根据情况引用。
addExact
subtractExact
multiplyExact
Share
本周分享近期看的关于微服务的内容,因为近期在跟一个微服务平台的项目POC,跟了这么多团队,可能因为行业原因,感觉只看见了分布式的应用,而在应用交易层面上与以前并无差别,目前还是云里雾里,等我弄懂了再总结吧。
https://blog.csdn.net/bntX2jSQfEHy7/article/details/79081314