File tree Expand file tree Collapse file tree 5 files changed +206
-0
lines changed
java/com/chen/algorithm/study Expand file tree Collapse file tree 5 files changed +206
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .chen .algorithm .study .test4 ;
2+
3+ import org .junit .Test ;
4+
5+ import java .util .ArrayList ;
6+ import java .util .List ;
7+
8+ /**
9+ * @author : chen weijie
10+ * @Date: 2019-09-03 23:10
11+ */
12+ public class Solution {
13+
14+ public double findMedianSortedArrays (int [] nums1 , int [] nums2 ) {
15+
16+ List <Integer > resultList = new ArrayList <>();
17+
18+
19+ if (nums1 == null ) {
20+ for (int value : nums2 ) {
21+ resultList .add (value );
22+ }
23+ } else if (nums2 == null ) {
24+ for (int value : nums1 ) {
25+ resultList .add (value );
26+ }
27+ } else {
28+
29+
30+
31+
32+ }
33+
34+
35+ int n = resultList .size ();
36+
37+
38+ double result = 0d ;
39+
40+ if (n % 2 == 0 ) {
41+ result = resultList .get (resultList .size () / 2 ) + (resultList .get ((resultList .size () / 2 ) - 1 )) / 2 ;
42+ } else {
43+ double index = resultList .size () / 2 ;
44+ result = resultList .get ((int ) Math .ceil (index ));
45+ }
46+ return result ;
47+ }
48+
49+
50+ @ Test
51+ public void testCase () {
52+
53+ int [] nums1 = {1 , 2 };
54+ int [] nums2 = {3 , 4 };
55+
56+ double d = findMedianSortedArrays (nums1 , nums2 );
57+ System .out .println (d );
58+
59+
60+ }
61+
62+
63+ }
Original file line number Diff line number Diff line change 1+ package com .chen .algorithm .study .test5 ;
2+
3+ import org .junit .Test ;
4+
5+ /**
6+ * 中心扩展算法
7+ *
8+ * @author : chen weijie
9+ * @Date: 2019-09-03 23:58
10+ */
11+ public class Solution {
12+
13+
14+ public String longestPalindrome (String s ) {
15+
16+ if (s == null || s .length () < 1 ) {
17+ return "" ;
18+ }
19+
20+ int start = 0 , end = 0 ;
21+
22+
23+ for (int i = 0 ; i < s .length (); i ++) {
24+ int len1 = expandArroundCenter (s , i , i );
25+ int len2 = expandArroundCenter (s , i , i + 1 );
26+
27+ int len = Math .max (len1 , len2 );
28+
29+ if (len > end - start ) {
30+ //中间值-长度的一半
31+ start = i - (len - 1 ) / 2 ;
32+ //中间值+长度的一半
33+ end = i + len / 2 ;
34+ }
35+ }
36+ return s .substring (start , end + 1 );
37+ }
38+
39+
40+ private int expandArroundCenter (String s , int left , int right ) {
41+
42+ int L = left , R = right ;
43+ while (L >= 0 && R < s .length () && s .charAt (L ) == s .charAt (R )) {
44+ L --;
45+ R ++;
46+ }
47+ return R - L - 1 ;
48+ }
49+
50+
51+
52+ @ Test
53+ public void testCase (){
54+ System .out .println (longestPalindrome ("dcacdefd" ));
55+ }
56+
57+
58+ }
Original file line number Diff line number Diff line change 1+ package com .chen .algorithm .study .test7 ;
2+
3+ import org .junit .Test ;
4+
5+ /**
6+ * @author : chen weijie
7+ * @Date: 2019-09-04 01:17
8+ */
9+ public class Solution {
10+
11+ public int reverse (int x ) {
12+
13+ boolean belowZero = false ;
14+ Long l = Long .parseLong (x + "" );
15+ if (l < 0 ) {
16+ l = Math .abs (l );
17+ belowZero = true ;
18+ }
19+
20+ char [] chars = (l + "" ).toCharArray ();
21+
22+ StringBuilder sb = new StringBuilder ();
23+ for (int i = chars .length - 1 ; i >= 0 ; i --) {
24+ sb .append (chars [i ]);
25+ }
26+
27+ Long result = Long .parseLong (sb .toString ());
28+ if (result > 2147483647 || result <-2147483648 ) {
29+ return 0 ;
30+ }
31+
32+ if (!belowZero ) {
33+ return result .intValue ();
34+ } else {
35+ return -result .intValue ();
36+ }
37+
38+ }
39+
40+
41+ @ Test
42+ public void testCase () {
43+ // -2147483648~2147483647
44+ System .out .println (reverse (-2143847412 ));
45+
46+ }
47+
48+
49+ }
Original file line number Diff line number Diff line change 1+ package com .chen .algorithm .study .test7 ;
2+
3+ import org .junit .Test ;
4+
5+ /**
6+ * @author : chen weijie
7+ * @Date: 2019-09-04 01:51
8+ */
9+ public class Solution2 {
10+
11+ public int reverse (int x ) {
12+ int rev = 0 ;
13+ while (x != 0 ) {
14+ int pop = x % 10 ;
15+ x /= 10 ;
16+ if (rev > Integer .MAX_VALUE / 10 || (rev == Integer .MAX_VALUE / 10 && pop > 7 )) {
17+ return 0 ;
18+ }
19+ if (rev < Integer .MIN_VALUE / 10 || (rev == Integer .MIN_VALUE / 10 && pop < -8 )) {
20+ return 0 ;
21+ }
22+ rev = rev * 10 + pop ;
23+ }
24+ return rev ;
25+ }
26+
27+
28+ @ Test
29+ public void testCase () {
30+
31+ System .out .println (reverse (9083 ));
32+
33+ }
34+
35+ }
Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ public static void main(String[] args) {
2121 System .out .println ("socre===" +score );
2222
2323
24+ System .out .println (1 <<31 -1 );
2425
2526
2627
You can’t perform that action at this time.
0 commit comments