ARTS是什么? Algorithm:每周至少做一个LeetCode的算法题 Review:阅读并点评至少一篇英文文章 Tip:学习至少一个技术技巧 Share:分享一篇有观点和思考的技术文章

Algorithm

  1. /**
  2. * @className: LeetCode02
  3. * @description: 给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。
  4. * 回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。例如,121 是回文,而 123 不是。
  5. *
  6. * 示例 1:
  7. * 输入:x = 121
  8. * 输出:true
  9. *
  10. * 示例 2:
  11. * 输入:x = -121
  12. * 输出:false
  13. * 解释:从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
  14. * @author: Miluo
  15. * @date: 2021/3/2
  16. **/
  17. public class LeetCode02 {
  18. public boolean isPalindrome(int x) {
  19. if(x < 0){
  20. return false;
  21. }
  22. String s = String.valueOf(x);
  23. char[] chars = s.toCharArray();
  24. if (chars.length == 1){
  25. //个位数为回文数
  26. return true;
  27. }else{
  28. StringBuilder sb = new StringBuilder();
  29. for (int i = chars.length - 1; i >= 0 ; i--) {
  30. sb.append(chars[i]);
  31. }
  32. return Long.parseLong(sb.toString()) == x;
  33. }
  34. }
  35. -----------------------------------------------------------------------
  36. //参考题解
  37. public boolean isPalindrome1(int x) {
  38. // 特殊情况:
  39. // 如上所述,当 x < 0 时,x 不是回文数。
  40. // 同样地,如果数字的最后一位是 0,为了使该数字为回文,
  41. // 则其第一位数字也应该是 0
  42. // 只有 0 满足这一属性
  43. if (x < 0 || (x % 10 == 0 && x != 0)) {
  44. return false;
  45. }
  46. int revertedNumber = 0;
  47. while (x > revertedNumber) {
  48. revertedNumber = revertedNumber * 10 + x % 10;
  49. x /= 10;
  50. }
  51. // 当数字长度为奇数时,我们可以通过 revertedNumber/10 去除处于中位的数字。
  52. // 例如,当输入为 12321 时,在 while 循环的末尾我们可以得到 x = 12,revertedNumber = 123,
  53. // 由于处于中位的数字不影响回文(它总是与自己相等),所以我们可以简单地将其去除。
  54. return x == revertedNumber || x == revertedNumber / 10;
  55. }
  56. }

Review

front-of-the-front-end and back-of-the-front-end web development

Tip

分布式事务保证幂等性的全局ID
image.png

Share

无代码和低代码是什么?会抢走程序员的工作吗?