5
5
6
6
import java .io .File ;
7
7
import java .io .IOException ;
8
+ import java .lang .reflect .Array ;
8
9
import java .lang .reflect .Modifier ;
10
+ import java .util .Arrays ;
11
+ import java .util .List ;
9
12
10
13
public class BaseTestDemo {
11
14
@@ -32,9 +35,132 @@ public static void main(String[] args) {
32
35
boolean b1 = baseAlgorithm .checkArrayIsMonotonous (new int []{1 , 2 , 3 });
33
36
System .out .println ("b = " + b + "--b1 = " + b1 );
34
37
38
+ // 两个数之和等于目标值
39
+ try {
40
+ int [] ints = TwoNumSum .twoSum (new int []{2 , 7 , 11 , 15 }, 9 );
41
+ printLog (Arrays .toString (ints ));
42
+ } catch (IllegalAccessException e ) {
43
+ e .printStackTrace ();
44
+ }
35
45
46
+ // 链表中求两个数的和
47
+ TwoNumAdd .print ();
48
+ }
49
+
50
+
51
+ /**
52
+ * 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
53
+ * 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
54
+ * 示例:
55
+ * 给定 nums = [2, 7, 11, 15], target = 9
56
+ * <p>
57
+ * 因为 nums[0] + nums[1] = 2 + 7 = 9
58
+ * 所以返回 [0, 1]
59
+ */
60
+ private static class TwoNumSum {
61
+ public static int [] twoSum (int [] nums , int target ) throws IllegalAccessException {
62
+ int [] result = new int [2 ];
63
+
64
+ for (int i = 0 ; i < nums .length ; i ++) {
65
+ for (int j = i + 1 ; j < nums .length ; j ++) {
66
+ if (nums [i ] + nums [j ] == target ) {
67
+ result [0 ] = i ;
68
+ result [1 ] = j ;
69
+ return result ;
70
+ }
71
+ }
72
+ }
73
+ throw new IllegalAccessException ("没找到两个数之和" );
74
+ }
75
+ }
76
+
77
+ /**
78
+ * 给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
79
+ * 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
80
+ * 您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
81
+ * 示例:
82
+ * 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
83
+ * 输出:7 -> 0 -> 8
84
+ * 原因:342 + 465 = 807
85
+ */
86
+ private static class TwoNumAdd {
87
+ private static class ListNode {
88
+ int value ;
89
+ ListNode next ;
90
+
91
+ public ListNode (int value ) {
92
+ this .value = value ;
93
+ }
94
+ }
95
+
96
+ public static ListNode addTwoNumbers (ListNode l1 , ListNode l2 ) {
97
+ ListNode headResult = new ListNode (0 );
98
+ ListNode m = l1 , n = l2 , current = headResult ;
99
+ int outOfNum = 0 ;
100
+ while (m != null || n != null ) {
101
+ int num1 = m != null ? m .value : 0 ;
102
+ int num2 = n != null ? n .value : 0 ;
103
+ int sum = outOfNum + num1 + num2 ;
104
+ outOfNum = sum / 10 ;
105
+ current .next = new ListNode (sum % 10 );
106
+ current = current .next ;
107
+ if (m != null ) m = m .next ;
108
+ if (n != null ) n = n .next ;
109
+ }
110
+ if (outOfNum > 0 ) {
111
+ current = new ListNode (outOfNum );
112
+ }
113
+ return headResult .next ;
114
+ }
115
+
116
+ private static ListNode toListNodeByNum (int num ) {
117
+ String s = String .valueOf (num );
118
+ StringBuilder sb = new StringBuilder (s );
119
+ s = sb .reverse ().toString ();
120
+ ListNode result = new ListNode (Integer .parseInt (String .valueOf (s .charAt (0 ))));
121
+ ListNode temp = result ;
122
+ for (int i = 1 ; i < s .length (); i ++) {
123
+ temp .next = new ListNode (Integer .parseInt (String .valueOf (s .charAt (i ))));
124
+ temp = temp .next ;
125
+ }
126
+ return result ;
127
+ }
128
+
129
+ private static int getNumByListNode (ListNode listNode ) {
130
+ StringBuilder sb = new StringBuilder ();
131
+ ListNode temp = listNode ;
132
+ while (temp != null ) {
133
+ sb .append (temp .value );
134
+ temp = temp .next ;
135
+ }
136
+ return Integer .parseInt (sb .reverse ().toString ());
137
+ }
138
+
139
+ static void print () {
140
+ ListNode l1 = generateListNodeByArray (new int []{2 , 4 , 3 });
141
+ ListNode l2 = generateListNodeByArray (new int []{5 , 6 , 4 });
142
+ ListNode node = addTwoNumbers (l1 , l2 );
143
+ while (node != null ) {
144
+ printLog (node .value + "" );
145
+ node = node .next ;
146
+ }
147
+ }
148
+
149
+ private static ListNode generateListNodeByArray (int [] array ) {
150
+ ListNode listNode = new ListNode (array [0 ]);
151
+ ListNode temp = listNode ;
152
+ for (int i = 1 ; i < array .length ; i ++) {
153
+ temp .next = new ListNode (array [i ]);
154
+ temp = temp .next ;
155
+ }
156
+ return listNode ;
157
+ }
36
158
37
159
}
38
160
39
161
162
+ private static void printLog (String str ) {
163
+ System .out .println (str );
164
+ }
165
+
40
166
}
0 commit comments