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

Commit46b20e1

Browse files
author
zhenyu zhang
committed
update LeetCode 14、100、151、190、1344
1 parent99973f4 commit46b20e1

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed

‎src/com/hadley/_014.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
packagecom.hadley;
2+
3+
/*
4+
2020.07.10
5+
14、Longest Common Prefix
6+
Write a function to find the longest common prefix string amongst an array of strings.
7+
8+
If there is no common prefix, return an empty string "".
9+
10+
Example 1:
11+
12+
Input: ["flower","flow","flight"]
13+
Output: "fl"
14+
Example 2:
15+
16+
Input: ["dog","racecar","car"]
17+
Output: ""
18+
Explanation: There is no common prefix among the input strings.
19+
*/
20+
21+
publicclass_014 {
22+
publicStringlongestCommonPrefix(String[]strs) {
23+
if(strs.length ==0 ||strs ==null)return"";
24+
if(strs.length ==1)returnstrs[0];
25+
//process strs[0]
26+
Stringresult ="";
27+
for(inti =0;i <strs[0].length();i ++){
28+
for(intj =1;j <strs.length;j ++){
29+
if(strs[j].length() -1 <i ||strs[j].charAt(i) !=strs[0].charAt(i)){
30+
returnresult;
31+
}
32+
33+
}
34+
result =result +strs[0].charAt(i);
35+
}
36+
37+
returnresult;
38+
}
39+
40+
}

‎src/com/hadley/_100.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
packagecom.hadley;
2+
3+
/*
4+
2020.07.13
5+
100、Same Tree
6+
*/
7+
8+
publicclass_100 {
9+
10+
publicclassTreeNode {
11+
intval;
12+
TreeNodeleft;
13+
TreeNoderight;
14+
TreeNode() {}
15+
TreeNode(intval) {this.val =val; }
16+
TreeNode(intval,TreeNodeleft,TreeNoderight) {
17+
this.val =val;
18+
this.left =left;
19+
this.right =right;
20+
}
21+
}
22+
23+
publicbooleanisSameTree(TreeNodep,TreeNodeq) {
24+
if(p ==null &&q ==null)returntrue;
25+
if((p !=null &&q ==null)||(p ==null &&q !=null)||(p.val !=q.val))returnfalse;
26+
returnisSameTree(p.left,q.left) &&isSameTree(p.right,q.right);
27+
}
28+
}

‎src/com/hadley/_1344.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
packagecom.hadley;
2+
3+
/*
4+
2020.07.14
5+
1344、Angle Between Hands of a Clock
6+
Given two numbers, hour and minutes. Return the smaller angle (in degrees) formed between the hour and the minute hand.
7+
8+
9+
10+
Example 1:
11+
Input: hour = 12, minutes = 30
12+
Output: 165
13+
14+
Example 2:
15+
Input: hour = 3, minutes = 30
16+
Output: 75
17+
18+
Example 3:
19+
Input: hour = 3, minutes = 15
20+
Output: 7.5
21+
22+
Example 4:
23+
Input: hour = 4, minutes = 50
24+
Output: 155
25+
26+
Example 5:
27+
Input: hour = 12, minutes = 0
28+
Output: 0
29+
*/
30+
31+
publicclass_1344 {
32+
publicdoubleangleClock(inthour,intminutes) {
33+
doubleminAngle = ((double)minutes/60) *360;
34+
doublehourAngle = (((double)hour %12) + (double)minutes/60 ) *30;
35+
36+
doubleresult =Math.abs(hourAngle -minAngle);
37+
returnresult >180 ?360 -result:result;
38+
}
39+
}

‎src/com/hadley/_151.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
packagecom.hadley;
2+
3+
/*
4+
2020.07.15
5+
151、 Reverse Words in a String
6+
*/
7+
8+
publicclass_151 {
9+
10+
publicStringreverseWords(Strings) {
11+
Stringresult ="";
12+
s =s.trim();
13+
String[]temp =s.split("\\s+");
14+
System.out.println(temp.length);
15+
for(inti =temp.length -1;i >=0;i--){
16+
System.out.println(temp[i]);
17+
result =result +temp[i] +" ";
18+
}
19+
returnresult.substring(0,result.length()-1);
20+
}
21+
}

‎src/com/hadley/_190.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
packagecom.hadley;
2+
3+
/*
4+
2020.07.12
5+
190、Reverse bits
6+
*/
7+
8+
publicclass_190 {
9+
10+
publicintreverseBits(intn) {
11+
returnInteger.reverse(n);
12+
}
13+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp