Grind 75+
Array
// c++ int a[3] = {0}; // init with default value, same as `int a[3] = {};` or `int a[3]{}` or `int a[3]{0}` int length = 3; // not a constant int a[length]; // cannot init with default value when the length is not a constant std::fill(a, a+length, 1); // init with default value using std::fill() int a[] = {1, 1}; // init with length 2 and default values as 1// java int[] a = new int[3]; // init with default value 0 for int int[] a = new int[] {1, 1, 1}; // init with length 3 and default values as 1 int[] a = {1, 2, 3};# python import numpy as np a = np.zeros(3) # init with default value 0 for int a = np.array([1, 1, 1]) # int with default values// c++ int a[] = {1, 2, 3}; int size = sizeof(a) / sizeof(a[0]); // classic for (int i = 0; i < size; i++) { cout<< a[i] << endl; } // range-based for loop for (auto& e : a) { cout<< e << endl; } // iterator for (auto it = std::begin(a); it != std::end(a); ++it) { cout << *it << endl; } // algorithm, note here the output array is also a std::transform(std::begin(a), std::end(a), std::begin(a), [](int x) { cout << x << endl; return x; });// java int[] a = {1, 2, 3}; // classic for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } // we have to transform it to list to use iterator Iterator<Integer> it = Arrays.asList(a).iterator(); while (it.hasNext()) { System.out.println(it.next()); } // or use stream api a = IntStream.of(numbers).map(x -> { System.out.println(x); return x; }).toArray();// c++ int a[3] = {0}; // {0, 0, 0} std::fill(a, a+3, 1); // {1, 1, 1}// java int[] a = new int[3]; Arrays.fill(a, 1); // {1, 1, 1}# python a = np.array([0, 0, 0]) a.fill(1) # {1, 1, 1}// c++ int a[6]{}; int length = sizeof(a) / sizeof(a[0]);// java int[] a = new int[6]; int length = a.length;# python import numpy as np a = np.zeros(6) length = len(a)// c++ int a[] = {3, 2, 1}; std::sort(a, a+3); // {1, 2, 3} std::sort(arr, arr + size, [](int a, int b) { return a > b; // Custom comparison logic for descending order }); // {3, 2, 1} std::sort(std::begin(a), std::end(a)); // {1, 2, 3} std::sort(std::begin(a), std::end(a), [] (int a, int b) { return a > b; }); // {3, 2, 1}// java int[] a = {3, 2, 1}; // !! you cannot apply the comparator to a Arrays.sort(a); // {1, 2, 3} // Note it's important to note that Collections.reverseOrder() cannot be directly applied to primitive arrays (like int[]). Instead, you need to use wrapper classes (like Integer[]) for this purpose. Integer[] arr = {1, 2, 3}; Arrays.sort(arr, Collections.reverseOrder()); // {3, 2, 1} // stream API a = IntStream.of(a).sorted().toArray(); a = IntStream.of(a).boxed().sorted(Comparator.reverseOrder()).mapToInt(i -> i).toArray();# python import numpy as np a = np.array([3, 1, 2]) np.sort(a) a = np.sort(a)[::-1] # create a new array a[::-1].sort() # sort in place// c++ int a[] = {1, 2, 3}; int max = *std::max_element(a, a+3); int min = *std::min_element(a, a+3);// java int[] a = {1, 2, 3}; int max = Arrays.stream(a).max().getAsInt(); // note that stream may auto box the int to Integer int max = Arrays.stream(a).max().orElse(Integer.MIN_VALUE); // note that in the end it will auto unbox the Integer to int int min = Arrays.stream(a).min().getAsInt(); int min = Arrays.stream(a).min().getAsInt();# python import numpy as np a = np.array([1, 2, 3]) max = np.max(a) min = np.min(a)// c++ int a[] = {2, 3, 4, 10, 10, 10, 40}; bool found = std::binary_search(a, a + n, 10); // true // lower_bound finds the first position where the value could be inserted without violating the order, // while upper_bound finds the last position where the value could be inserted without violating the order. auto it = std::lower_bound(a, a + n, 10); // element >= 10, (it - a) == 3 if (it != a + n && *it == key) { // found } else { // not found } auto it2 = std::upper_bound(a, a+n, 10); // element > 10, (it - a) == 6 if (it2 != a + n && *it2 == key) { // found } else { // not found }// java int result = Arrays.binarySearch(arr, key); if (result >= 0) { System.out.println("Element found at index: " + result); } else { int insertPos = ~result; System.out.println("Element not found, insert position is: " + insertPos); }# python import numpy as np a = np.array([1, 2, 2, 3, 3, 3, 4, 5, 6, 6]) left_index = np.searchsorted(arr, 3, side='left', sorter=None) # 3, it returns the index of the first suitable location found right_index = np.searchsorted(arr, 3, side='right') # 6, it returns the index of the last suitable location
String
IO
List
Vector / ArrayList
Linked List
Heap
Map
Set
Queue
Stack
Template / Generics / metaclass
Last updated