Skip to content

Latest commit

 

History

History
136 lines (98 loc) · 2.2 KB

arts-0011.md

File metadata and controls

136 lines (98 loc) · 2.2 KB

1.Algorithm

[9]  Palindrome Number

Easy    Math

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example1:

Input: 121
Output: true

Example2:

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.

Example3:

Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Follow up:

Could you solve it without converting the integer to a string?

解答

主要采用简单的数学算术理论,除法与求余,从尾部迭代构造新的整数与输入参数比较,判断两个值是否相等

(1).采用迭代相除,用余数迭代构造新的整数与输入参数比较是否相等
func isPalindrome(x int) bool {
	if x < 0 {
		return false
	}

	originX, reverseX := x, 0
	for x > 0 {
		reverseX = reverseX*10 + x%10
		x /= 10
	}

	return reverseX == originX
}
(2).在方法(1)的基础上,采用类似折半的思想,优化迭代的次数
func isPalindrome(x int) bool {
	if x < 0 || (x%10 == 0 && x != 0) {
		return false
	}

	reverseX := 0
	for x > reverseX {
		reverseX = reverseX*10 + x%10
		x /= 10
	}

	return reverseX == x || x == reverseX/10
}

2.Review

Git Reference

Getting and Creating Projects

  • init
  • clone

Basic Snapshotting

  • add
  • status
  • diff
  • commit
  • reset
  • rm,mv
  • stash

Branching and Merging

  • branch
  • checkout
  • merge
  • log
  • tag

Sharing and Updating Projects

  • remote
  • fetch, pull
  • push

Inspection and Comparison

  • log
  • diff

Our Software Dependency Problem

The rest of this section outlines some considerations when inspecting a package and deciding whether to depend on it.

  • Design
  • Code Quality
  • Testing
  • Debugging
  • Maintenance
  • Usage
  • Security
  • Licensing
  • Dependencies
  • Test the dependency
  • Abstract the dependency
  • Isolate the dependency
  • Avoid the dependency
  • Upgrade the dependency
  • Watch your dependency

3.Tip

4.Share