Skip to main content

Posts

Showing posts with the label LeetCode

Reverse String | LeetCode

  Write a function that reverses a string. The input string is given as an array of characters   char[] . Do not allocate extra space for another array, you must do this by  modifying the input array  in-place  with O(1) extra memory. You may assume all the characters consist of  printable ascii characters .   Example 1: Input: ["h","e","l","l","o"] Output: ["o","l","l","e","h"] Example 2: Input: ["H","a","n","n","a","h"] Output: ["h","a","n","n","a","H"] Solution: class Solution { public: void reverseString(vector<char>& s) { int i =0, n = s.size() - 1; char ch; while(i<n){ ch = s[i]; s[i] = s[n]; s[n] = ch; ++i; --n; } } };

Duplicate Zeros | LeetCode

Given a fixed-length array   arr   of integers, duplicate each occurrence of zero, shifting the remaining elements to the right. Note that elements beyond the length of the original array are not written. Do the above modifications to the input array  in place , do not return anything from your function.   Example 1: Input: [1,0,2,3,0,4,5,0] Output: null Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4] Example 2: Input: [1,2,3] Output: null Explanation: After calling your function, the input array is modified to: [1,2,3]   Note: 1 <= arr.length <= 10000 0 <= arr[i] <= 9 Solution: class Solution { public:     void duplicateZeros(vector<int>& arr) {         int len = arr.size();         for(int i=0;i< len; ++i){             if(arr[i] == 0){                 for(int j = ...

Squares of a Sorted Array | LeetCode

  Given an integer array   nums   sorted in   non-decreasing   order, return   an array of  the squares of each number  sorted in non-decreasing order .   Example 1: Input: nums = [-4,-1,0,3,10] Output: [0,1,9,16,100] Explanation: After squaring, the array becomes [16,1,0,9,100]. After sorting, it becomes [0,1,9,16,100]. Example 2: Input: nums = [-7,-3,2,3,11] Output: [4,9,9,49,121]   Constraints: 1 <= nums.length <=  10 4 -10 4  <= nums[i] <= 10 4 nums  is sorted in  non-decreasing  order.   Follow up:  Squaring each element and sorting the new array is very trivial, could you find an  O(n)  solution using a different approach? Solutions: class Solution { public:     vector<int> sortedSquares(vector<int>& nums) {         int len = nums.size();         for(int i = 0; i < len; ++i){        ...

Find Numbers with Even Number of Digits | LeetCode

  Given an array  nums  of integers, return how many of them contain an  even number  of digits.   Example 1: Input: nums = [12,345,2,6,7896] Output: 2 Explanation: 12 contains 2 digits (even number of digits).  345 contains 3 digits (odd number of digits).  2 contains 1 digit (odd number of digits).  6 contains 1 digit (odd number of digits).  7896 contains 4 digits (even number of digits).  Therefore only 12 and 7896 contain an even number of digits. Example 2: Input: nums = [555,901,482,1771] Output: 1 Explanation: Only 1771 contains an even number of digits.   Constraints: 1 <= nums.length <= 500 1 <= nums[i] <= 10^5 Solution: class Solution { public:     int findNumbers(vector<int>& nums) {         int evenNumber = 0, count;                  for(auto i: nums){             count = 0;   ...

Max Consecutive Ones | LeetCode

Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3. Note: The input array will only contain  0  and  1 . The length of the input array is a positive integer and will not exceed 10,000 Question from LeetCode Solutions: class Solution { public:     int findMaxConsecutiveOnes(vector<int>& nums) {         int maxOnes = 0, count = 0;         for(auto i : nums){             if(i == 1){                 ++count;             }else{                                count = 0;             }           ...