ProblemSolving

Add Strings

Problem Problem_Link My Answer 1 hour limit no search on internet My code class Solution { public String addStrings(String num1, String num2) { StringBuilder sb = new StringBuilder(); int num1Leng = num1.length() - 1; int num2Leng = num2.length() - 1; int round = 0; for (int i = Math.max(num1.length(), num2.length()) - 1; i > -1; --i) { if (num1Leng < 0) { sb.append(String.valueOf(((num2.charAt(num2Leng) - 48) + round)%10)); if ((((num2.

Remove Duplicates from Sorted Array

Problem Problem_Link My Answer 1 hour limit no search on internet My code class Solution { public int removeDuplicates(int[] nums) { if(nums==null ||nums.length==0) return 0; int temp = -101; int count=0; int pos=0; for(int i=0;i<nums.length;++i){ if(temp!=nums[i]){ temp=nums[i]; nums[pos]=nums[i]; ++count; ++pos; } } return count; } } Time complexity : $O(n)$ Space complexity : $O(1)$ My result Runtime: 2 ms, faster than 22.76% of Java online submissions for Remove Duplicates from Sorted Array.

Count Binary Substrings

Problem Problem_Link My Answer 1 hour limit no search on internet My result Compile Error Best Answer Search from internet and modify class Solution { public int countBinarySubstrings(String s) { int curRun = 1; int preRun = 0; int count = 0; for (int i = 1; i < s.length(); i++) { if (s.charAt(i) == s.charAt(i - 1)) ++curRun; else { count += Math.min(curRun, preRun); preRun = curRun; curRun = 1; } } return count + Math.

Two Sum

Problem Problem_Link My Answer 1 hour limit no search on internet Overview input : int[] nums, int target output : int[] My code class Solution { public int[] twoSum(int[] nums, int target) { int temp[] = new int[nums.length]; int rtn[] = new int[2]; for (int i = 0; i < nums.length; i++) { temp[i] = target - nums[i]; for (int j = 0; j < i; j++) { if (temp[j] == nums[i]) { rtn[0] = j; rtn[1] = i; return rtn; } } } rtn[0] = 0; rtn[1] = 0; return rtn; } } Time complexity : $O(n^2)$ Space complexity : $O(1)$ My result Runtime: 76 ms, faster than 12.