leetcode

【LeetCode】242. Valid Anagram

只愿长相守 提交于 2019-12-01 01:42:12
Difficulty:easy More: 【目录】LeetCode Java实现 Description https://leetcode.com/problems/valid-anagram/ Given two strings s and t , write a function to determine if t is an anagram of s . Example 1: Input: s = "anagram", t = "nagaram" Output: true Example 2: Input: s = "rat", t = "car" Output: false Note: You may assume the string contains only lowercase alphabets. Follow up: What if the inputs contain unicode characters? How would you adapt your solution to such case? Intuition use int[26] or HashMap Solution public boolean isAnagram(String s, String t) { int[] freq = new int[26]; for(int i=0; i<s

【LeetCode】387. First Unique Character in a String

梦想的初衷 提交于 2019-12-01 01:40:12
Difficulty:easy More: 【目录】LeetCode Java实现 Description https://leetcode.com/problems/first-unique-character-in-a-string/ Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowercase letters. Intuition Use HashMap( or int[256] ) to store the frequency of each character. Solution public int firstUniqChar(String s) { int[] times = new int[26]; for(int i=0; i<s.length(); i++) times[s.charAt(i)-'a']+=1; for(int i=0; i<s.length(

Leetcode(4)寻找两个有序数组的中位数

断了今生、忘了曾经 提交于 2019-11-30 23:51:14
Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定两个大小为 m 和 n 的有序数组 nums1 和* nums2 。 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。 你可以假设 nums1* 和 nums2 不会同时为空。 第一种方法:list拼接排列取中位数 执行用时:116 ms ; 内存消耗:11.8MB 效果:还行 class Solution(object): def findMedianSortedArrays(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: float """ nums1.extend(nums2) sort_nums1=sorted(nums1) number=len(sort_nums1) if number%2==1 : average_nums=sort_nums1[number/2] else: average_nums=float((sort_nums1[number/2]+sort_nums1[number/2-1]))/2 return average_nums 学习 整数/2在leetcode上 取整有点迷 分奇偶情况 第二种方法:归并排序 执行用时:124 ms;

【LeetCode】344. Reverse String

梦想与她 提交于 2019-11-30 23:32:39
Difficulty:easy More: 【目录】LeetCode Java实现 Description https://leetcode.com/problems/reverse-string/ Write a function that reverses a string. The input string is given as an array of characters char[] . Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. You may assume all the characters consist of printable ascii characters . Example 1: Input: ["h","e","l","l","o"] Output: ["o","l","l","e","h"] Example 2: Input: ["H","a","n","n","a","h"] Output: ["h","a","n","n","a","H"] Intuition use a pointer Solution public void

LeetCode--139--单词拆分(python)

 ̄綄美尐妖づ 提交于 2019-11-30 19:31:05
给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词。 说明: 拆分时可以重复使用字典中的单词。 你可以假设字典中没有重复的单词。 示例 1: 输入: s = "leetcode", wordDict = ["leet", "code"] 输出: true 解释: 返回 true 因为 "leetcode" 可以被拆分成 "leet code"。 示例 2: 输入: s = "applepenapple", wordDict = ["apple", "pen"] 输出: true 解释: 返回 true 因为 "applepenapple" 可以被拆分成 "apple pen apple"。 注意你可以重复使用字典中的单词。 示例 3: 输入: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"] 输出: false 1 class Solution: 2 def wordBreak(self, s: str, wordDict: List[str]) -> bool: 3 dp = [False for _ in range(len(s)+1)] 4 dp[0]=True 5 for i in range(1,len(s)+1

【LeetCode】217. Contains Duplicate

て烟熏妆下的殇ゞ 提交于 2019-11-30 19:03:54
Difficulty:easy More: 【目录】LeetCode Java实现 Description https://leetcode.com/problems/contains-duplicate/ Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. Example 1: Input: [1,2,3,1] Output: true Example 2: Input: [1,2,3,4] Output: false Example 3: Input: [1,1,1,3,3,4,3,2,4,2] Output: true Intuition Use HashSet. Solution public boolean containsDuplicate(int[] nums) { if(nums==null || nums.length<=1) return false; HashSet<Integer> set =

【LeetCode】189. Rotate Array

删除回忆录丶 提交于 2019-11-30 19:01:25
Difficulty:easy More: 【目录】LeetCode Java实现 Description https://leetcode.com/problems/rotate-array/ Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: [1,2,3,4,5,6,7] and k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] Example 2: Input: [-1,-100,3,99] and k = 2 Output: [3,99,-1,-100] Explanation: rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100] Note: Try to come up as many

【LeetCode】26. Remove Duplicates from Sorted Array

社会主义新天地 提交于 2019-11-30 18:51:28
Difficulty:easy More: 【目录】LeetCode Java实现 Description https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array nums , remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Example 1: Given nums = [1,1,2], Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the returned length. Example 2: Given nums = [0,0

本周学习小结(07/10 - 13/10)

对着背影说爱祢 提交于 2019-11-30 17:57:05
LeetCode 本周有进展。需要及时复习。 学习笔记之LeetCode - 浩然119 - 博客园 https://www.cnblogs.com/pegasus923/p/5528520.html Explore - LeetCode - Design https://leetcode.com/explore/interview/card/top-interview-questions-medium/112/design/ Explore - LeetCode - Math https://leetcode.com/explore/interview/card/top-interview-questions-medium/113/math/ 学习笔记之Problem Solving with Algorithms and Data Structures using Python - 浩然119 - 博客园 https://www.cnblogs.com/pegasus923/p/10454395.html C++ / Database / Git / Linux / Python / MISC 本周有进展。 学习笔记之C / C++ - 浩然119 - 博客园 https://www.cnblogs.com/pegasus923/p/10437163.html

九月第四周总结

坚强是说给别人听的谎言 提交于 2019-11-30 17:02:34
1. 用递归自己写pow函数常见的正确写法和错误写法 正确写法: n为奇数:return pow(a*a, n/2) * a; n为偶数:return pow(a*a, n/2); 错误写法: return pow(pow(a,2), n/2); return pow(pow(a, n/2), 2); 有可能会陷入死循环,提示Segmentation fault 2. 127.0.0.1是什么?为什么使用它? 127.0.0.1是回送地址,指本地机,一般用来测试使用。回送地址(127.x.x.x)是本机回送地址(Loopback Address),即主机IP堆栈内部的IP地址,主要用于网络软件测试以及本地机进程间通信,无论什么程序,一旦使用回送地址发送数据,协议软件立即返回,不进行任何网络传输。(待补充。。。) 3. 关于String和char[] String是一个对象类型,String定义的字符串是不能改变的 如果想要逐个字符地操作,可以使用 char[] ch = str.toCharArray(); 将str转化为字符数组来进行操作。 另外,字符数组和String在操作上面也有不同。 4. java里面怎么找出三个数的最小值? 使用嵌套的形式: Math.min(a, Math.min(b,c)) 5. linux 0.11里面的汇编代码对中文不友好,不要写中文注释