LeetCode 简单版
LeetCode 简单版
白昼先生
这个作者很懒,什么都没留下…
展开
-
392. 判断子序列 Java版
class Solution { public boolean isSubsequence(String s, String t) { if(s.length() == 0) return true; char[] chars = s.toCharArray(); char[] chart = t.toCharArray(); int j=0; for(int i=0; i<cha.原创 2021-11-11 18:29:41 · 78 阅读 · 0 评论 -
389. 找不同 Java版
class Solution { public char findTheDifference(String s, String t) { int res = 0; for(char ch : s.toCharArray()) res ^= ch; for(char ch : t.toCharArray()) res ^= ch; return (char)res; }}.原创 2021-11-11 16:26:01 · 237 阅读 · 0 评论 -
387. 字符串中的第一个唯一字符 Java版
class Solution { public int firstUniqChar(String s) { if(s == null || s.length() == 0) return -1; int[] c= new int[26]; for(int i=0; i<s.length(); i++) c[s.charAt(i) - 'a']++; for(int i=0;.原创 2021-11-11 16:18:42 · 287 阅读 · 0 评论 -
383. 赎金信 Java版
class Solution { public boolean canConstruct(String ransomNote, String magazine) { int []record = new int[26]; char [] r = ransomNote.toCharArray(); char [] m = magazine.toCharArray(); for(int i=0; i<m.length; i+.原创 2021-11-11 16:09:52 · 296 阅读 · 0 评论 -
374. 猜数字大小 Java版
/** * Forward declaration of guess API. * @param num your guess * @return -1 if num is lower than the guess number * 1 if num is higher than the guess number * otherwise return 0 * int guess(int num); */public clas.原创 2021-11-11 15:46:56 · 226 阅读 · 0 评论 -
367. 有效的完全平方数 Java版
class Solution { public boolean isPerfectSquare(int num) { int low = 1; int high = num; while(low <= high){ int mid = low + (high - low)/2; int t = num/mid; if(t == mid){ .原创 2021-11-10 12:45:40 · 94 阅读 · 0 评论 -
350. 两个数组的交集 II Java版
class Solution { public int[] intersect(int[] nums1, int[] nums2) { Map<Integer,Integer> map = new HashMap<>(nums1.length); for(int num:nums1){ Integer count = map.get(num); if(count == null) .原创 2021-11-10 12:28:18 · 344 阅读 · 0 评论 -
349. 两个数组的交集 Java版
class Solution { public int[] intersection(int[] nums1, int[] nums2) { if(nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0){ return new int[0]; } Set<Integer> set1 = new HashSet<&g.原创 2021-11-06 15:00:14 · 112 阅读 · 0 评论 -
345. 反转字符串中的元音字母 Java版
class Solution { public String reverseVowels(String s) { HashSet<Character> set=new HashSet<>(); set.add('a'); set.add('e'); set.add('i'); set.add('o'); set.add('u'); set.add('A'); .原创 2021-11-06 14:36:09 · 159 阅读 · 0 评论 -
344. 反转字符串 Java版
class Solution { public void reverseString(char[] s) { int n = s.length; for(int i=0; i<n/2; i++){ int j= n -1 -i; s[i]^=s[j]; s[j]^=s[i]; s[i]^=s[j]; } }}原创 2021-11-04 16:08:11 · 106 阅读 · 0 评论 -
342. 4的幂 Java版
class Solution { public boolean isPowerOfFour(int n) { // 0x55555555 = 1010101010101010101010101010101 (偶数位为0,奇数位为1) // 先检查是否大于0,再看二进制位是不是只有一个1,然后检查这个1是不是在4的次方位上 return n > 0 && (n & -n) == n && (n & .原创 2021-10-13 16:59:37 · 58 阅读 · 0 评论 -
338. 比特位计数 Java版
class Solution { public int[] countBits(int n) { int[] res = new int[n + 1]; for(int i=0; i<=n; i++){ if(i%2 == 0) res[i] = res[i/2]; else res[i] = res[i/2] + 1; }.原创 2021-10-13 13:38:37 · 125 阅读 · 0 评论 -
326. 3的幂 Java版
class Solution { public boolean isPowerOfThree(int n) { if(n<=0) return false; while(n>1){ if(n%3 != 0) return false; n = n/3; } return true; }}原创 2021-10-11 19:07:56 · 65 阅读 · 0 评论 -
303. 区域和检索 - 数组不可变 Java版
class NumArray { private int[] sums; public NumArray(int[] nums) { sums = new int[nums.length]; if(nums.length == 0){ return; } sums[0] = nums[0]; for(int i=1; i<nums.length; i++){ .原创 2021-10-11 18:46:52 · 87 阅读 · 0 评论 -
292. Nim 游戏 Java版
class Solution { public boolean canWinNim(int n) { if(n%4 == 0) return false; else return true; }}原创 2021-10-11 16:36:58 · 583 阅读 · 0 评论 -
290. 单词规律 Java版
class Solution { public boolean wordPattern(String pattern, String s) { if(pattern == null || s == null) return false; String[] string = s.split(" "); if(pattern.length() != string.length) return fals.原创 2021-10-10 21:36:58 · 144 阅读 · 0 评论 -
283. 移动零 Java版
class Solution { public void moveZeroes(int[] nums) { for(int i=0,count=0;i<nums.length;i++){ if(nums[i] != 0){ if(count != i){ nums[count] = nums[i]; num.原创 2021-10-10 21:13:20 · 52 阅读 · 0 评论 -
278. 第一个错误的版本 Java版
/* The isBadVersion API is defined in the parent class VersionControl. boolean isBadVersion(int version); */public class Solution extends VersionControl { public int firstBadVersion(int n) { int lo = 1; int hi = n; .原创 2021-10-10 20:52:44 · 97 阅读 · 0 评论 -
268. 丢失的数字 Java版
class Solution { public int missingNumber(int[] nums) { int res = nums.length; for(int i = 0; i<nums.length; i++){ res ^= nums[i]; res ^= i; } return res; }}原创 2021-10-10 20:43:31 · 96 阅读 · 0 评论 -
263. 丑数 Java版
class Solution { public boolean isUgly(int n) { if(n == 0) return false; else if(n%2 == 0){ return isUgly(n/2); } else if(n%3 == 0){ return isUgly(n/3); } else if(.原创 2021-10-10 20:32:31 · 54 阅读 · 0 评论 -
258. 各位相加 Java版
class Solution { public int addDigits(int num) { if(num<10) return num; num = num/10+num%10; return addDigits(num); }}原创 2021-10-10 20:12:58 · 77 阅读 · 0 评论 -
257. 二叉树的所有路径 Java版
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { .原创 2021-10-10 19:44:16 · 141 阅读 · 0 评论 -
242. 有效的字母异位词 Java版
class Solution { public boolean isAnagram(String s, String t) { int[] sCounts = new int[26]; int[] tCounts = new int[26]; for(char ch : s.toCharArray()){ sCounts[ch - 'a']++; } for(char ch : t.toC.原创 2021-10-10 19:24:33 · 60 阅读 · 0 评论 -
237. 删除链表中的节点 Java版
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public void deleteNode(ListNode node) { node.val = node.next.val; .原创 2021-10-10 19:15:53 · 148 阅读 · 0 评论 -
235. 二叉搜索树的最近公共祖先 Java版
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { private TreeNode res; public TreeNode lowestCommonAn.原创 2021-10-10 19:11:45 · 66 阅读 · 0 评论 -
234. 回文链表 Java版
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * }.原创 2021-10-08 22:00:27 · 54 阅读 · 0 评论 -
232. 用栈实现队列 Java版
class MyQueue { private Stack<Integer> a; private Stack<Integer> b; public MyQueue() { a = new Stack<>(); b = new Stack<>(); } public void push(int x) { a.push(x); } pub.原创 2021-10-08 21:51:04 · 122 阅读 · 0 评论 -
231. 2 的幂 Java版
class Solution { public boolean isPowerOfTwo(int n) { if(n<1) return false; return (n & n-1)==0; }}原创 2021-10-08 21:35:06 · 55 阅读 · 0 评论 -
228. 汇总区间 Java版
class Solution { public List<String> summaryRanges(int[] nums) { List<String> res = new ArrayList<>(); int n = nums.length; if(n==0) return res; int i=0; StringBuilder s; .原创 2021-10-08 21:28:31 · 99 阅读 · 0 评论 -
226. 翻转二叉树 Java版
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { .原创 2021-10-08 20:59:04 · 120 阅读 · 0 评论 -
225. 用队列实现栈 Java版
class MyStack { private Queue<Integer> a; private Queue<Integer> b; public MyStack() { a = new LinkedList<>(); b = new LinkedList<>(); } public void push(int x) { a.offer(x); .原创 2021-10-08 20:43:34 · 64 阅读 · 0 评论 -
219. 存在重复元素 II Java版
class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { HashMap<Integer,Integer> map = new HashMap<>(); int len = nums.length; for(int i=0; i<len; i++){ if(!map.containsKey(nu.原创 2021-10-08 19:33:33 · 97 阅读 · 0 评论 -
217. 存在重复元素 Java版
class Solution { public boolean containsDuplicate(int[] nums) { Set<Integer> set = new HashSet<>(); for(int i =0; i<nums.length; i++){ if(set.contains(nums[i])){ return true; .原创 2021-10-07 14:15:14 · 50 阅读 · 0 评论 -
206. 反转链表 Java版
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * }.原创 2021-10-07 13:46:09 · 74 阅读 · 0 评论 -
205. 同构字符串 Java版
class Solution { public boolean isIsomorphic(String s, String t) { if(s.length() != t.length()){ return false; } HashMap<Character,Character> map = new HashMap<>(); for(int i=0; i<s.length(.原创 2021-10-07 13:27:55 · 135 阅读 · 0 评论 -
203. 移除链表元素 Java版
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * }.原创 2021-10-07 12:55:50 · 87 阅读 · 0 评论 -
202. 快乐数 Java版
class Solution { public boolean isHappy(int n) { Set<Integer> record = new HashSet<>(); while( n!=1 && !record.contains(n)){ record.add(n); n = getNextNumber(n); } .原创 2021-10-07 12:09:43 · 168 阅读 · 0 评论 -
197. 上升的温度 MySQL
# Write your MySQL query statement belowselect a.idfrom Weather a, Weather bwhere a.Temperature > b.Temperatureand datediff(a.recordDate,b.recordDate)=1原创 2021-10-07 11:58:08 · 67 阅读 · 0 评论 -
196. 删除重复的电子邮箱 MySQL
# Write your MySQL query statement belowdelete p1from person p1inner join person p2on p1.Email = p2.EmailAnd p1.Id > p2.Id原创 2021-10-07 11:41:18 · 91 阅读 · 0 评论 -
191. 位1的个数 Java版
public class Solution { // you need to treat n as an unsigned value public int hammingWeight(int n) { int count=0; for(int i=1; i<=32; i++){ count +=n&1; n>>=1; } return coun.原创 2021-10-07 11:33:33 · 76 阅读 · 0 评论