1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
| package December;
import javax.rmi.CORBA.Util; import java.util.*;
class Solution { public static void main(String[] args) { Solution solution = new Solution(); int[] arr = new int[]{1,2,3,4}; runningSum(arr); canConstruct("aab","baa"); solution.minOperations("110"); System.out.println(solution.secondHighest("ck077")); int[] arr1 = new int[]{-1,0,3,9,11,13,22,27,33,57,66,77};
int i = searchInsert(arr1, 1); System.out.println(i); int[] nums = new int[]{-2,1,-3,4,-1,2,1,-5,4}; solution.twoSum(nums,8); solution.maxSubArray(nums); solution.lengthOfLongestSubstring("pwwkep"); System.out.println("123456789".substring(2, 4)); String s1 = "ab",s2 = "eidboaoo"; solution.checkInclusion(s1, s2); solution.mostCompetitive(new int[]{2,4,3,3,5,4,9,6},4); }
public int[] mostCompetitive(int[] nums, int k) { ArrayList<Integer> stack = new ArrayList<Integer>(); int n = nums.length; int last = n-k; for (int i = 0; i < n; i ++) { while (stack.size()>0 && last>0 && stack.get(stack.size()-1)>nums[i]) { stack.remove(stack.size()-1); last --; } stack.add(nums[i]); } int[] res = new int[k]; for(int i = 0; i < k; i++){ res[i] = stack.get(i); } return res; }
public boolean checkInclusion(String s1, String s2) { int n1=s1.length(),n2=s2.length(); if (n1>n2) return false; int[] cnt = new int[26]; for(int x = 0;x<n1;x++){ cnt[s1.charAt(x) - 'a'] -= 1; } int left = 0; for (int i = 0; i < n2; i++) { int str = s2.charAt(i) - 'a'; cnt[str] += 1; while (cnt[str] > 0){ cnt[s2.charAt(left) - 'a'] -= 1; left++; } if (i-left+1 == n1){ return true; } } return false; } public static int[] runningSum(int[] nums) { for (int i = 1; i < nums.length; i++) { nums[i] += nums[i-1]; } return nums; }
public static boolean canConstruct(String ransomNote, String magazine) { if(ransomNote.length() > magazine.length()) { return false; } int[] cnt = new int[26]; for(char c : magazine.toCharArray()) { cnt[c - 'a'] ++; } for(char c : ransomNote.toCharArray()) { cnt[c - 'a'] --; if(cnt[c - 'a'] < 0) { return false; } } return true; } public List<String> fizzBuzz(int n) { ArrayList<String> arrayList = new ArrayList<>(); for (int i = 1; i <= n; i++) { if (i % 3 == 0 && i % 5 == 0){ arrayList.add("FizzBuzz"); }else if (i % 3 ==0){ arrayList.add("Fizz"); }else if (i % 5 == 0){ arrayList.add("Buzz"); }else { arrayList.add(String.valueOf(i)); } } return arrayList; } public ListNode middleNode(ListNode head) { ListNode slow = head; ListNode fast = head;
while (fast != null && fast.next != null){ slow = slow.next; fast = fast.next.next;
} return slow; } public int[] minOperations(String boxes) { String[] split = boxes.split(""); int[] ints = new int[split.length]; for (int i=0;i<split.length;i++){ int count = 0; for (int x=0;x<split.length;x++){ if (split[x].equals("1")){ count += Math.abs(x-i); } } ints[i] = count; } return ints; }
public int secondHighest(String s) { ArrayList<Integer> ay = new ArrayList<>(); for (char i : s.toCharArray()){ if (i > 47 && i < 58){ if (!ay.contains(i - 48)){ ay.add(i-48); } } } Collections.sort(ay); if (ay.size()>1){ return ay.get(ay.size()-2); } return -1; } public static int searchInsert(int[] nums, int target) { int left = 0; int right = nums.length -1; while (left <= right){ int middle = left + (right - left) / 2; if (nums[middle] > target){ right = middle -1; }else if (nums[middle] < target){ left = middle + 1; }else { return middle; } } return right+1; } public int search(int[] nums, int target) { int left = 0, right = nums.length - 1; while (left <= right) { int mid = (right - left) / 2 + left; int num = nums[mid]; if (num == target) { return mid; } else if (num > target) { right = mid - 1; } else { left = mid + 1; } } return -1; } public int[] sortedSquares(int[] nums) { int[] result = new int[nums.length]; int k = result.length-1; int right = result.length-1; int left = 0; while(left <= right){ if (nums[left]*nums[left] < nums[right]*nums[right]){ result[k--] = nums[right]*nums[right]; right--; }else{ result[k--] = nums[left]*nums[left]; left++; } } return result; } public void rotate(int[] nums, int k) { reversal(nums,0,nums.length-1); reversal(nums,0,k-1); reversal(nums,k,nums.length-1);
} public int[] reversal(int[] nums,int start,int end){ while (start<end){ int temp = nums[start]; nums[start] = nums[end]; nums[end] = temp; start++; end--; } return nums; } public void moveZeroes(int[] nums) { int n = nums.length,left = 0,right = 0; while (right < n) { if(nums[right] != 0){ res(nums,left,right); left++; } right++; } } public void res(int[] nums,int x,int y){ int temp = nums[x]; nums[x] = nums[y]; nums[y] = temp; } public void reverseString(char[] s) { int left = 0,right = s.length-1; while(left <= right){ char temp = s[left]; s[left] = s[right]; s[right] = temp; left++; right--; } } public boolean checkIfPangram(String sentence) { boolean[] booleans = new boolean[26]; for (int i = 0; i < sentence.length(); i++) { char c = sentence.charAt(i); booleans[c - 'a'] = true; } for (boolean x : booleans){ if (!x){ return false; } } return true; } public boolean containsDuplicate(int[] nums) { Set<Integer> set = new HashSet<Integer>(); for (int x : nums) { if (!set.add(x)) { return true; } } return false; } public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>(); for (int i = 0; i < nums.length; ++i) { if (hashtable.containsKey(target - nums[i])) { return new int[]{hashtable.get(target - nums[i]), i}; } hashtable.put(nums[i], i); } return new int[0]; } public int lengthOfLongestSubstring(String s) { HashMap<Character, Integer> map = new HashMap<>(); int max = 0, start = 0; for (int end = 0; end < s.length(); end++) { char ch = s.charAt(end); if (map.containsKey(ch)){ start = Math.max(map.get(ch)+1,start); } max = Math.max(max,end - start + 1); map.put(ch,end); } return max; }
public int maxSubArray(int[] nums) { int ans = nums[0], previous = nums[0]; for(int i :nums){ previous = Math.max(previous+i, i); ans = Math.max(previous, ans); } return ans; } public void merge(int[] nums1, int m, int[] nums2, int n) { int p1 = m -1,p2 = n -1,length = m + n -1,i; while (p1>=0 || p2 >= 0){ if (p1 == -1){ i = nums2[p2--]; }else if(p2 == -1){ i = nums1[p1--]; } else if (nums1[p1] < nums2[p2]){ i = nums2[p2--]; }else { i = nums1[p1--]; } nums1[length--] = i; } } public String reverseWords(String x) { String[] s = x.split(" "); for (int i = 0; i < s.length; i++) { StringBuffer stringBuffer = new StringBuffer(s[i]); s[i] = stringBuffer.reverse().toString(); } return String.join(" ",s); } public ListNode removeNthFromEnd(ListNode head, int n) { ListNode dummy = new ListNode(0, head); ListNode first = head; ListNode second = dummy; for (int i = 0; i < n; ++i) { first = first.next; } while (first != null) { first = first.next; second = second.next; } second.next = second.next.next; return dummy.next; } public int maximumWealth(int[][] accounts) { int max = 0; for (int[] i: accounts){ int max1 =0; for (int k : i){ max1 += k; } if (max1 > max){ max = max1; } } return max; }
}
|