medium

LeetCode 解答 #50. Pow(x, n) プログラミング練習

問題: 難易度: medium 入力: double , int 目的: 既存の関数なしで累乗の計算を実現せよ 出力: double入力はintではなくdoubleだ。 つまり小数点の部分も考えなければならない。 累乗計算は-2乗等も含むのできちんと分類しておこう。 注意すべき事: 入力が0…

LeetCode 解答 #49. Group Anagrams プログラミング練習

問題: 難易度: medium 入力: String[] strs 目的: 入力文字列群の内、同じ文字で構成されている文字列をグループ化せよ 出力: List>入力はString型のArrayです。 中には例えば"eat", "tea"等の文字列が入っています。 そのうちの同じ文字列で構成された単語…

LeetCode 解答 #48. Rotate Image プログラミング練習

問題: 難易度: medium 入力: int型Array 目的: 入力されたn*nのArrayを右に90度回転せよ 出力: なし入力はn*nの2次元行列です。 その行列を右側90度回転させる問題です。 アフィン変換等をするのではなく、 ただ単に内容の数値を回転させればいいのです。 …

LeetCode 解答 #47. Permutations II プログラミング練習

問題: 難易度: medium 入力: int型Array 目的: 入力されたArrayの数字の可能なすべての組み合わせを求めよ 出力: int型のリストのリスト前回の#46問目と殆ど同じ問題です。 どちらも可能な組み合わせをすべて探す問題です。 違いは今回の問題の入力が重複あ…

LeetCode 解答 #46. Permutations プログラミング練習

問題: 難易度: medium 入力: Array 目的: 入力されたArrayの数字の可能なすべての組み合わせを求めよ 出力: int型のリストのリスト入力Arrayのすべての可能な組み合わせを求める問題です。 すべての組み合わせを求めるのでどの道全体探索しなければなりませ…

LeetCode 解答 #43. Multiply Strings プログラミング練習

問題: 難易度: medium 入力: String 目的: 入力された二つのstrを数字に変換し、掛け算の結果を求めよ 出力: StringStringとして入力された数字を掛け算して答えを求める問題です。 但しString.valueof等のライブラリを使う事は禁じられています。 入力を一…

LeetCode 解答 #40. Combination Sum II プログラミング練習

問題: 難易度: medium 入力: int型array, (int) target 目的: 入力arrayの中から和がtargetになる組み合わせをすべて求めよ 出力: リストのリスト前回の♯39問目と殆ど同じです。 前回との違いは、入力リストの中に重複した数字が存在する事、 同じ数字は一…

LeetCode 解答 #39. Combination Sum プログラミング練習

問題: 難易度: medium 入力: int型array, (int) target 目的: 入力arrayの中から和がtargetになる組み合わせを 出力: リストのリスト例にあるように、[2,3,6,7]、target=7が入力の場合、 答えは [ [7], [2,2,3] ] になります。 入力arrayの中の数字を使い…

LeetCode 解答 #36. Valid Sudoku プログラミング練習

問題: 難易度: medium 入力: character型array 目的: 入力された数独パズルの盤面が有効か判断せよ 出力: boolean皆さんお馴染みの数独ゲームの問題です。 解らない人もいると思うのでルールとしては: 9*9の盤面をもらい、縦が1-9、横も1-9、更に9個のsub-b…

LeetCode 解答 #34. Search for a Range プログラミング練習

問題: 難易度: medium 入力: int型array、(int)target 目的: 入力されたarrayの中で、targetの値と一致するエレメントの始まりと終わりのindexを求めよ 出力: int型Array前の♯33問目と殆ど同じ内容です。 更に今回はRotateされていないので、昇順にソートさ…

LeetCode 解答 #33. Search in Rotated Sorted Array プログラミング練習

問題: 難易度: medium 入力: int型array、(int)target 目的: 入力されたarrayの中で、targetの値と一致するエレメントのindexを求めよ 出力: int入力のArrayはソートされているが、どこかでRotateされている。 例えば[1,2,3,4,5]がどこかでくねり、[3,4,5,1…

LeetCode 解答 #31. Next Permutation プログラミング練習

問題: 難易度: medium 入力: int型array 目的: 入力されたarrayの数字を操作し次に多きい数字になるように順番を組み直せ 出力: なし出力はなしですがちゃんと入力Arrayをテストで見ています。 少し解りにくい問題ですが、要するに 入力が[1,3,2]の場合、 …

LeetCode #29. Divide Two Integers プログラミング練習

問題: 難易度: medium 入力: (int) devidend, (int) devidsor 目的: devidendをdevidsorで割った結果を返せ。 出力: int入力は二つの整数、除数と被除数、つまりは割る数字と割られる数字です。 /を使ってしまえば一発と言いたいところですが当然そんな簡単…

LeetCode #24. Swap Nodes in Pairs プログラミング練習

問題: 難易度: medium 入力: ListNode 目的: 隣接の二つのノードをすべてスワップして返せ 出力: ListNodeLinkedListの問題が立て込んでますね。 やはりArrayとは違い、LinkedLIstはノード毎に繋がりをもっているので、 ただ隣接のノードの値を交換するので…

LeetCode #22. Generate Parentheses プログラミング練習

問題: 難易度: medium 入力: 正数n 目的: n個の括弧で有効組み合わせをすべて求めよ 出力: Stringのリスト前回の♯21で括弧群の有効かどうかを判断しました。 今回は有効な組み合わせをすべて求める問題です。 21問目と違い括弧は”()”のみなので求めやすく…

LeetCode #19. Remove Nth Node From End of List プログラミング練習

問題: 難易度: medium 入力: ListNode, n 目的: 入力リストの後ろからn番目のノードを取り除け 出力: ListNodeLinkedListの問題です。 非情報系出身者にはあまり見ないデータ型かもしれませんが、 コーディング面接にはよく出るタイプなので必ず知っておき…

LeetCode #18. 4Sum プログラミング練習

問題: 難易度: medium 入力: int型Array 目的: 入力された整数の内、和がtargetになる四つの整数の組み合わせをすべて出力せよ 出力: int型リストのリスト#15の3Sumの延長問題です。 基本思想は全く同じで、♯15を解けた人なら同じ方法で解けると思います。 …

LeetCode #17. Letter Combinations of a Phone Number プログラミング練習

問題: 難易度: medium 入力: String 目的: 入力されたケータイのボタンを押した時可能な出力をすべて探せ 出力: Stringのリストガラケー時代を思い出しましょう。画像にあるように1は何も表しません。2はabc, 3はdef, 4-ghi, 5-jkl, 6-mno, 7-pqrs, 8-tuv, …

LeetCode #16. 3Sum Closest プログラミング練習

問題: Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.Exampl…

LeetCode #15. 3Sum プログラミング練習

問題: Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:The solution set must not contain duplicate triplets.Example:Given…

LeetCode #12. Integer to Roman プログラミング練習

問題: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written…

LeetCode #11. Container With Most Water プログラミング練習

問題: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axi…

LeetCode #8. String to Integer (atoi) プログラミング練習

問題: Implement atoi which converts a string to an integer.The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional ini…

LeetCode #5. Longest Palindromic Substring プログラミング練習

問題: Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example 1:Input: "babad" Output: "bab" Note: "aba" is also a valid answer. Example 2:Input: "cbbd" Output: "bb" 難易…

LeetCode #4. Median of Two Sorted Arrays プログラミング練習

問題: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).Example 1: nums1 = [1, 3] nums2 = [2]The median is 2.0 Example…

Leetcode #3. Longest Substring Without Repeating Characters プログラミング練習

問題: Given a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc", which the length is 3.Given "bbbbb", the answer is "b", with the length of 1.Given "pwwkew", the a…

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 contain a single digit. Add the two numbers and return it as a linked list.You may assum…