Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5a49375

Browse files
author
zhenyu zhang
committed
update LeetCode 8、35、50
1 parent27848e2 commit5a49375

File tree

3 files changed

+200
-0
lines changed

3 files changed

+200
-0
lines changed

‎src/com/hadley/_008.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
packagecom.hadley;
2+
3+
/*
4+
2020.06.25
5+
8 String to Integer
6+
7+
Example 1:
8+
Input: "42"
9+
Output: 42
10+
11+
Example 2:
12+
Input: " -42"
13+
Output: -42
14+
Explanation: The first non-whitespace character is '-', which is the minus sign.
15+
Then take as many numerical digits as possible, which gets 42.
16+
17+
Example 3:
18+
Input: "4193 with words"
19+
Output: 4193
20+
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.
21+
22+
Example 4:
23+
Input: "words and 987"
24+
Output: 0
25+
Explanation: The first non-whitespace character is 'w', which is not a numerical
26+
digit or a +/- sign. Therefore no valid conversion could be performed.
27+
28+
Example 5:
29+
Input: "-91283472332"
30+
Output: -2147483648
31+
Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
32+
Thefore INT_MIN (−231) is returned.
33+
*/
34+
35+
importjava.math.BigInteger;
36+
37+
publicclass_008 {
38+
publicstaticintmyAtoi(Stringstr) {
39+
40+
for (inti =0;i <str.length();i++) {
41+
charc =str.charAt(i);
42+
43+
if (!Character.isSpaceChar(c))
44+
returnconvertToInteger(str.substring(i));
45+
46+
}
47+
48+
return0;
49+
}
50+
51+
publicstaticintconvertToInteger(Stringstr) {
52+
doubleresult =0;
53+
intsign =1,i =0;
54+
55+
if (str.charAt(0) =='-' ||str.charAt(0) =='+') {
56+
sign =str.charAt(0) =='+' ?1 : -1;
57+
i++;
58+
}
59+
60+
while (i <str.length()) {
61+
charc =str.charAt(i);
62+
63+
if ((c >='0' &&c <='9')) {
64+
65+
result =10 *result + (c -'0');
66+
67+
}elsebreak;
68+
69+
i++;
70+
71+
}
72+
73+
if (sign==-1)
74+
result = -result;
75+
76+
if (result >Integer.MAX_VALUE)
77+
returnInteger.MAX_VALUE;
78+
elseif (result <Integer.MIN_VALUE)
79+
returnInteger.MIN_VALUE;
80+
81+
82+
83+
return (int)result;
84+
}
85+
}

‎src/com/hadley/_035.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
packagecom.hadley;
2+
3+
/*
4+
2020.06.27
5+
35 Search Insect Position
6+
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
7+
8+
You may assume no duplicates in the array.
9+
10+
Example 1:
11+
12+
Input: [1,3,5,6], 5
13+
Output: 2
14+
Example 2:
15+
16+
Input: [1,3,5,6], 2
17+
Output: 1
18+
Example 3:
19+
20+
Input: [1,3,5,6], 7
21+
Output: 4
22+
Example 4:
23+
24+
Input: [1,3,5,6], 0
25+
Output: 0
26+
*/
27+
28+
publicclass_035 {
29+
30+
//brute solution: O(n) binary search: O(logN)
31+
publicintsearchInsert(int[]nums,inttarget) {
32+
33+
for(inti =0;i <nums.length;i++){
34+
if(nums[i] >=target){
35+
returni;
36+
}
37+
}
38+
39+
if(target >nums[nums.length -1]){
40+
returnnums.length -1;
41+
}
42+
43+
44+
return0;
45+
46+
}
47+
48+
publicintsearchInsectByBinarySearch(int[]nums,inttarget){
49+
intlow =0,high =nums.length -1;
50+
if(target >nums[nums.length -1]){
51+
returnnums.length;
52+
}
53+
54+
while(low <high){
55+
intmid =low + (high -low) /2;
56+
if(nums[mid] <target){
57+
low =mid +1;
58+
}else{
59+
high =mid;
60+
}
61+
}
62+
63+
returnlow;
64+
}
65+
}

‎src/com/hadley/_050.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
packagecom.hadley;
2+
3+
/*
4+
2020.06.26
5+
50. Pow(x,n)
6+
mplement pow(x, n), which calculates x raised to the power n (xn).
7+
8+
Example 1:
9+
10+
Input: 2.00000, 10
11+
Output: 1024.00000
12+
Example 2:
13+
14+
Input: 2.10000, 3
15+
Output: 9.26100
16+
Example 3:
17+
18+
Input: 2.00000, -2
19+
Output: 0.25000
20+
Explanation: 2-2 = 1/22 = 1/4 = 0.25
21+
Note:
22+
23+
-100.0 < x < 100.0
24+
n is a 32-bit signed integer, within the range [−231, 231 − 1]
25+
*/
26+
publicclass_050 {
27+
28+
publicdoublemyPow(doublex,intn) {
29+
//main problem: |Integer.MIN_VALUE| = Integer.MAX_VALUE + 1;
30+
doubley =x;
31+
if(n ==0){
32+
return1;
33+
}else{
34+
if(n ==Integer.MIN_VALUE){
35+
n =n +2;
36+
}
37+
intabs =Math.abs(n);
38+
doubletemp =myPow(y,abs/2);
39+
if(abs %2 ==0){
40+
y =temp *temp;
41+
}else{
42+
y *= (temp *temp);
43+
}
44+
returnn <0 ?Math.max(1/y,0.0):y;
45+
46+
}
47+
}
48+
49+
50+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp