Skip to content

Commit d242ee4

Browse files
committed
slove them
Signed-off-by: megatontech <techme@live.cn>
1 parent f7fed0f commit d242ee4

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

PalindromeNumber.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,23 @@
11
package main
2+
//9. Palindrome Number
3+
//Easy
4+
//
5+
//Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
6+
//
7+
//Example 1:
8+
//Input: 121
9+
//Output: true
10+
//Example 2:
11+
//Input: -121
12+
//Output: false
13+
//Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
14+
//Example 3:
15+
//Input: 10
16+
//Output: false
17+
//Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
18+
//Follow up:
19+
//
20+
//Coud you solve it without converting the integer to a string?
21+
func isPalindrome(x int) bool {
22+
23+
}

ReverseInteger.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,62 @@
11
package main
2+
import (
3+
"fmt"
4+
"math"
5+
"strconv"
6+
"strings"
7+
)
8+
//7. Reverse Integer
9+
//Easy
10+
//Given a 32-bit signed integer, reverse digits of an integer.
11+
//
12+
//Example 1:
13+
//Input: 123
14+
//Output: 321
15+
//
16+
//Example 2:
17+
//Input: -123
18+
//Output: -321
19+
//
20+
//Example 3:
21+
//Input: 120
22+
//Output: 21
23+
//Note:
24+
//Assume we are dealing with an environment which could only store integers
25+
//within the 32-bit signed integer range: [−231, 231 − 1].
26+
// For the purpose of this problem,
27+
//assume that your function returns 0 when the reversed integer overflows.
28+
func reverse(x int) int {
29+
return reverse1(x)
30+
}
31+
func reverse1(x int) int {
32+
//reverse1
33+
//整数转字符串
34+
var str = strconv.itoa(x)
35+
////字符串转整型
36+
bytes := []rune(str)
37+
for from , to := 0 , len(bytes) -1 ; from < to ; from , to = from + 1, to -1{
38+
bytes[from] , bytes[to] = bytes[to] , bytes[from]
39+
}
40+
str = string(bytes)
41+
var result = strconv.Atoi(str)
42+
return result
43+
}
44+
func reverse2(x int) int {
45+
//切割为数组,反转后按幂乘然后相加
46+
//var arr = []int
47+
var reversearr = []int
48+
//strings.Split(strconv.Itoa(x))
49+
bytes := []rune(strconv.Itoa(x))
50+
for from , to := 0 , len(bytes) -1 ; from < to ; from , to = from + 1, to -1{
51+
bytes[from] , bytes[to] = bytes[to] , bytes[from]
52+
}
53+
for i, i2 := range bytes {
54+
reversearr[i] = strconv.Atoi(i2)
55+
}
56+
var result = 0
57+
for i, i2 := range reversearr {
58+
result+=i2*math.Pow10(i)
59+
}
60+
result = (int)result
61+
return result
62+
}

RomantoInteger.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,45 @@
11
package main
2+
//13. Roman to Integer
3+
//Easy
4+
//Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
5+
//Symbol Value
6+
//I 1
7+
//V 5
8+
//X 10
9+
//L 50
10+
//C 100
11+
//D 500
12+
//M 1000
13+
//For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.
14+
//Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
15+
//
16+
//I can be placed before V (5) and X (10) to make 4 and 9.
17+
//X can be placed before L (50) and C (100) to make 40 and 90.
18+
//C can be placed before D (500) and M (1000) to make 400 and 900.
19+
//Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
20+
//
21+
//Example 1:
22+
//
23+
//Input: "III"
24+
//Output: 3
25+
//Example 2:
26+
//
27+
//Input: "IV"
28+
//Output: 4
29+
//Example 3:
30+
//
31+
//Input: "IX"
32+
//Output: 9
33+
//Example 4:
34+
//
35+
//Input: "LVIII"
36+
//Output: 58
37+
//Explanation: L = 50, V= 5, III = 3.
38+
//Example 5:
39+
//
40+
//Input: "MCMXCIV"
41+
//Output: 1994
42+
//Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
43+
func romanToInt(s string) int {
44+
45+
}

0 commit comments

Comments
 (0)