Posts

Number of 1 Bits

Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the  Hamming weight ). Note: Note that in some languages such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned. In Java, the compiler represents the signed integers using  2's complement notation . Therefore, in  Example 3  above, the input represents the signed integer.  -3 . Follow up : If this function is called many times, how would you optimize it? Example 1: Input: n = 00000000000000000000000000001011 Output: 3 Explanation: The input binary string 00000000000000000000000000001011 has a total of three '1' bits. Example 2: Input: n = 00000000000000000000000010000000 Output: 1 Explanation: The inp

Get Maximum in Generated Array

  You are given an integer   n . An array   nums   of length   n + 1   is generated in the following way: nums[0] = 0 nums[1] = 1 nums[2 * i] = nums[i]  when  2 <= 2 * i <= n nums[2 * i + 1] = nums[i] + nums[i + 1]  when  2 <= 2 * i + 1 <= n Return   the  maximum  integer in the array  nums ​​​. Example 1: Input: n = 7 Output: 3 Explanation: According to the given rules: nums[0] = 0 nums[1] = 1 nums[(1 * 2) = 2] = nums[1] = 1 nums[(1 * 2) + 1 = 3] = nums[1] + nums[2] = 1 + 1 = 2 nums[(2 * 2) = 4] = nums[2] = 1 nums[(2 * 2) + 1 = 5] = nums[2] + nums[3] = 1 + 2 = 3 nums[(3 * 2) = 6] = nums[3] = 2 nums[(3 * 2) + 1 = 7] = nums[3] + nums[4] = 2 + 1 = 3 Hence, nums = [0,1,1,2,1,3,2,3], and the maximum is 3. Example 2: Input: n = 2 Output: 1 Explanation: According to the given rules, the maximum between nums[0], nums[1], and nums[2] is 1. Example 3: Input: n = 3 Output: 2 Explanation:

Merge Sorted Array

Given two sorted integer arrays  nums1  and  nums2 , merge  nums2  into  nums1  as one sorted array. The number of elements initialized in  nums1  and  nums2  are  m  and  n  respectively. You may assume that  nums1  has enough space (size that is equal to  m + n ) to hold additional elements from  nums2 . Example 1: Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6] Example 2: Input: nums1 = [1], m = 1, nums2 = [], n = 0 Output: [1] Constraints: 0 <= n, m <= 200 1 <= n + m <= 200 nums1.length == m + n nums2.length == n -10 9  <= nums1[i], nums2[i] <= 10 9 Solution : class Solution {     public void merge(int[] nums1, int m, int[] nums2, int n) {         int i;         for(i=0;i<n;i++){             nums1[m+i] = nums2[i];         }         Arrays.sort(nums1);     } } Try it

Check If Two String Arrays are Equivalent

Given two string arrays  word1  and  word2 , return   true  if the two arrays  represent  the same string, and  false  otherwise. A string is  represented  by an array if the array elements concatenated  in order  forms the string. Example 1: Input: word1 = ["ab", "c"], word2 = ["a", "bc"] Output: true Explanation: word1 represents string "ab" + "c" -> "abc" word2 represents string "a" + "bc" -> "abc" The strings are the same, so return true. Example 2: Input: word1 = ["a", "cb"], word2 = ["ab", "c"] Output: false Example 3: Input: word1 = ["abc", "d", "defg"], word2 = ["abcddefg"] Output: true   Constraints: 1 <= word1.length, word2.length <= 10 3 1 <= word1[i].length, word2[i].length <= 10 3 1 <= sum(word1[i].leng

Merge Two Sorted Lists

Image
Merge two sorted linked lists and return it as a  sorted  list. The list should be made by splicing together the nodes of the first two lists. Example 1: Input: l1 = [1,2,4], l2 = [1,3,4] Output: [1,1,2,3,4,4] Example 2: Input: l1 = [], l2 = [] Output: [] Example 3: Input: l1 = [], l2 = [0] Output: [0] Constraints: The number of nodes in both lists is in the range  [0, 50] . -100 <= Node.val <= 100 Both  l1  and  l2  are sorted in  non-decreasing  order. Solution: /**  * Definition for singly-linked list.  * public class ListNode {  *     int val;  *     ListNode next;  *     ListNode() {}  *     ListNode(int val) { this.val = val; }  *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }  * }  */ class Solution {     public ListNode mergeTwoLists(ListNode l1, ListNode l2) {         ListNode temp = new ListNode();        

Determine if String Halves Are Alike

You are given a string   s   of even length. Split this string into two halves of equal lengths, and let   a   be the first half and   b   be the second half. Two strings are  alike  if they have the same number of vowels ( 'a' ,  'e' ,  'i' ,  'o' ,  'u' ,  'A' ,  'E' ,  'I' ,  'O' ,  'U' ). Notice that  s  contains uppercase and lowercase letters. Return  true  if  a  and  b  are  alike . Otherwise, return  false . Example 1: Input: s = "book" Output: true Explanation:  a = "b o " and b = " o k". a has 1 vowel and b has 1 vowel. Therefore, they are alike. Example 2: Input: s = "textbook" Output: false Explanation:  a = "t e xt" and b = "b oo k". a has 1 vowel whereas b has 2. Therefore, they are not alike. Notice that the vowel o is counted twice. Example 3: Input: s = "MerryChristmas" Output: false