File tree Expand file tree Collapse file tree 3 files changed +62
-0
lines changed
product-of-array-except-self Expand file tree Collapse file tree 3 files changed +62
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * 시간복잡도: O(n)
3+ * 공간복잡도: O(1)
4+ * */
5+ class Solution {
6+ public int [] productExceptSelf (int [] nums ) {
7+ int n = nums .length ;
8+ int [] answer = new int [n ];
9+
10+ answer [0 ] = 1 ;
11+ for (int i = 1 ; i < n ; i ++) {
12+ answer [i ] = answer [i - 1 ] * nums [i - 1 ];
13+ }
14+
15+ int suffixProduct = 1 ;
16+ for (int i = n - 1 ; i >= 0 ; i --) {
17+ answer [i ] *= suffixProduct ;
18+ suffixProduct *= nums [i ];
19+ }
20+
21+ return answer ;
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ /*
2+ * 시간복잡도: O(1)
3+ * 공간복잡도: O(1)
4+ * */
5+ public class Solution {
6+ // you need treat n as an unsigned value
7+ public int reverseBits (int n ) {
8+ int result = 0 ;
9+ for (int i = 0 ; i < 32 ; i ++) {
10+ result <<= 1 ;
11+ result |= (n & 1 );
12+ n >>= 1 ;
13+ }
14+ return result ;
15+ }
16+ }
Original file line number Diff line number Diff line change 1+ /*
2+ * 시간복잡도 : O(n)
3+ * 공간복잡도 : O(n)
4+ * */
5+
6+ import java .util .HashMap ;
7+ import java .util .Map ;
8+
9+ class Solution {
10+ public int [] twoSum (int [] nums , int target ) {
11+ Map <Integer , Integer > map = new HashMap <>();
12+
13+ for (int i = 0 ; i < nums .length ; i ++) {
14+ int complement = target - nums [i ];
15+
16+ if (map .containsKey (complement )) {
17+ return new int [] { map .get (complement ), i };
18+ }
19+ map .put (nums [i ], i );
20+ }
21+ return null ;
22+ }
23+ }
You can’t perform that action at this time.
0 commit comments