Python

문제 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: 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 greater or equal to m + n) to hold additional elements from nums2. Example: Input: nums1 = [1,2,3,0,0,0], m = 3 nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6]풀이 ..
개요 문자열(String)은 하나 또는 여러 개의 문자(글자, 숫자, 기호)가 일렬로 나열된 것입니다. 텍스트는 일상 생활에서 사용하는 일반적인 데이터 형식으로 이를 표현하는 문자열 자료형은 파이썬(Python)을 포함한 여러 프로그래밍 언어에서 매우 중요한 구성요소입니다. 이번 글에서는 파이썬에서 문자열을 생성하고 출력하는 방법, 문자열을 병합(연결)하고 복제하는 방법, 변수에 문자열을 저장하는 방법을 설명합니다. 문자열 생성 및 출력 파이썬에서 문자열은 문자(글자, 숫자, 기호)를 나열한 후, 작은따옴표 '또는 큰 따옴표 "로 묶어서 만듭니다. 'This is a string in single quotes.' "This is a string in double quotes." 작은 따옴표 또는 큰 따옴..
문제 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 풀이 Divide & Conquer 전략에 따라 작은 문제를 해결하고, 이를 조합하여 큰 문제를 해결한다. class Solution: def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: if l1 is None : return l2 if l2 is None : return l1 if..
문제 Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1: Input: 1->1->2 Output: 1->2Example 2: Input: 1->1->2->3->3 Output: 1->2->3풀이 정렬된 단일연결리스트를 순회하며 중복(동일값) 여부를 확인하여 제거한다. class Solution: def deleteDuplicates(self, head: ListNode) -> ListNode: cur = head while cur : if cur.next and cur.val == cur.next.val : cur.next = cur.next.next else : cur = ..
문제 You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Note: Given n will be a positive integer. Example 1: Input: 2 Output: 2 Explanation: There are two ways to climb to the top. 1. 1 step + 1 step 2. 2 stepsExample 2: Input: 3 Output: 3 Explanation: There are three ways to climb to..
문제 Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a non-negative integer. Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned. Example 1: Input: 4 Output: 2Example 2: Input: 8 Output: 2 Explanation: The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 i..
문제 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word (last word means the last appearing word if we loop from left to right) in the string. If the last word does not exist, return 0. Note: A word is defined as a maximal substring consisting of non-space characters only. Example: Input: "Hello World" Output: 5풀이 class So..
문제 Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Example 1: Input: [1,3,5,6], 5 Output: 2Example 2: Input: [1,3,5,6], 2 Output: 1Example 3: Input: [1,3,5,6], 7 Output: 4Example 4: Input: [1,3,5,6], 0 Output: 0풀이 class Solution: def searchInsert(s..
쓴웃음
'Python' 태그의 글 목록 (2 Page)