본문 바로가기

알고리즘 문제풀이/leetcode142

[leetcode 2] Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example 1: Input: l1 = [2,4,3], l2 = [5,6,4] Output: [7,0,8] Explanation: 342 +.. 2020. 10. 13.
[leetcode 283] Move Zeroes Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. Example: Input: [0,1,0,3,12] Output: [1,3,12,0,0] 문제: 0을 전부 뒤로 미루는 문제 풀이 : 0이 아닌 숫자를 앞에서 부터 채우고 나머지 배열에 0을 채운다. class Solution { public: void moveZeroes(vector& nums) { int last = 0; //0이 아닌 숫자를 앞으로 밀어넣기 for(int i=0; i 2020. 10. 12.
[leetcode 438] Find All Anagrams in a String Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100. The order of output does not matter. Example 1: Input: s: "cbaebabacd" p: "abc" Output: [0, 6] Explanation: The substring with start index = 0 is "cba", which is an anagram of "abc". T.. 2020. 10. 12.
[leetcode 448] Find All Numbers Disappeared in an Array Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this array. Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space. Example: Input: [4,3,2,7,8,2,3,1] Output: [5,6] 문제 설명: 1부터 n까지 숫자로 구성되어 있는 배열에서 없는 .. 2020. 10. 12.