leetcode

[leetcode][JavaScript]Length of Last Word

依然范特西╮ 提交于 2021-02-13 16:35:33
1、leetcode问题描述: Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space characters ' ' , return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space characters only. For example, Given s = "Hello World" , return 5 . 2、解题思路: 用JavaScript内置的split()分割字符串s,定义变量len=0。从后往前遍历,若分割后得到的数组的最后一个值不是 '' (这是为了排除字符串s在最后一个单词后面还带着空格的情况,如s='a '),则该值的字符串长度赋值给len并结束循环,否则继续向前遍历。最后返回len。 3、JavaScript解题代码: /** * @param {string} s * @return {number} */ var lengthOfLastWord = function(s)

[leetcode] Longest Common Prefix

回眸只為那壹抹淺笑 提交于 2021-02-13 06:14:23
Write a function to find the longest common prefix string amongst an array of strings. https://oj.leetcode.com/problems/longest-common-prefix/ 思路1:以第一个字符串为基准,依次比较后面字符串每一位的情况,如有不相同或者有字符串提前结束即返回。 public class Solution { public String longestCommonPrefix(String[] strs) { int n = strs.length; if(n<1) return ""; int len0 = strs[0].length(); int i, j; outer: for (i = 0; i < len0; i++) { char cur = strs[0].charAt(i); for (j = 1; j < n; j++) { if (i >= strs[j].length() || strs[j].charAt(i) != cur) { break outer; } } } return strs[0].substring(0, i); } public static void main(String[] args) { System

[leetcode] 3Sum Closest

这一生的挚爱 提交于 2021-02-12 13:30:28
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2 1 -4}, and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). https://oj.leetcode.com/problems/3sum-closest/ 思路1:类似3Sum,区别是不需要去重,另外需要维护一个最小差值。 import java.util.Arrays; public class Solution { public int threeSumClosest(int[] num, int target) { int n = num.length; Arrays.sort(num); int min = Integer.MAX_VALUE; int result = 0;

[leetcode] Spiral Matrix II

て烟熏妆下的殇ゞ 提交于 2021-02-10 16:29:11
Given an integer n , generate a square matrix filled with elements from 1 to n 2 in spiral order. For example, Given n =3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] https://oj.leetcode.com/problems/spiral-matrix-ii/ 思路:按要求输出,注意边界。 public class Solution { public int[][] generateMatrix(int n) { int[][] mat = new int[n][n]; if (n <= 0) return mat; int num = 1; int left = 0; int right = n - 1; int top = 0; int bottom = n - 1; while (num <= n * n) { int i; for (i = left; i <= right; i++) mat[top][i] = num++; for (i = top + 1; i <= bottom; i++) mat[i][right] =

数组中找到和为给定值的两个数

馋奶兔 提交于 2021-01-09 02:14:18
problem 167 & 170 from leetcode; https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ https://leetcode.com/problems/two-sum-iii-data-structure-design/ 先来看简单的167:给定一个排好序的数组,以及一个值,找出数组中相加等于该值的两个数,假设这样的值始终存在; 这个只要依次遍历该数组,假设当前值为x,那么只需要在数组中找到value - x;如果存在,直接返回,如果不存在,检查下一个值;因为数组是sorted,所以第二步查找只需要log N的时间;最坏情况是 n * log N; public int[] twoSum(int[] numbers, int target) { int[] result = new int[2]; for(int i = 1; i <= numbers.length; i++) { int x = numbers[i - 1]; int y = target - x; int j = Arrays.binarySearch(numbers, i, numbers.length, y); if(j >= i) { result[0] = i; result[1] = j + 1;

LeetCode:Happy Number

别等时光非礼了梦想. 提交于 2020-12-24 05:15:21
1、题目名称 Happy Number(快乐数) 2、题目地址 https://leetcode.com/problems/happy-number/ 3、题目内容 英文: Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers. 中文: 设计一个算法,判断一个数字是否“快乐”。快乐数可以被如下流程定义:一个正整数,计算出它各位数字的平方和,得到一个新的数字,再对这个新的数字重复这一过程,直到最后得到数字1或是其他某几个数字的无限循环

LeetCode 107. Binary Tree Level Order Traversal II

大憨熊 提交于 2020-12-15 06:16:01
107. Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree [3,9,20,null,null,15,7] , 3 / \ 9 20 / \ 15 7 return its bottom-up level order traversal as: [ [15,7], [9,20], [3] ] 就是复习以下树的分层遍历 我发现leetcode上基本上不用前序、中序、后序、分层遍历这些词,大多数都只有dfs和bfs 练习一下图和树的遍历,可以顺便把队列和栈之类的一起复习了 bfs用一个队列,而需要返回的列表用链表头插法就行了 public class Solution { public List<List<Integer>> levelOrderBottom(TreeNode root) { List<List<Integer>> list = new LinkedList<>(); if(root==null){

[leetcode] Length of Last Word

早过忘川 提交于 2020-12-12 14:58:01
Given a string s consists of upper/lower-case alphabets and empty space characters' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space characters only. For example, Given s ="Hello World", return5. https://oj.leetcode.com/problems/length-of-last-word/ 思路:从后向前扫描,定位lastword然后记录其长度。 public class Solution { public int lengthOfLastWord(String s) { if (s == null || s.length() < 1) return 0; int n = s.length(); int i; int len = 0; boolean in = false; for (i = n - 1; i >= 0; i--) { if (!in &&

【LeetCode】TreeNode类实现解析(java实现)

丶灬走出姿态 提交于 2020-11-26 07:15:07
在LeetCode中,TreeNode是经常用到的一个结构体,表示数据结构树(Tree)中的一个节点。其官方定义如下: public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } }   在Tree的题目中,常会给出一些测试用例,用一些特定的格式来表示一棵树,如[3,9,20,null,null,15,7]就表示如下的一棵树: 3 / \ 9 20 / \ 15 7   因此,我扩展了一下这个TreeNode的一些实现,使其可以通过官方给出的格式方便的构建出一棵树,从而使得我们在自己写玩代码后能很方便地调试。 package MakeLeetCodeClass; public class TreeNode { public int val; public TreeNode left; public TreeNode right; TreeNode(int x) { val = x; } public String toString(){ return Integer.toString(val); } // int []arr = {3, 9, 20, Integer.MAX_VALUE, Integer.MAX_VALUE, 15, 7}; private

LeetCode:Bulb Switcher

我的未来我决定 提交于 2020-05-04 10:15:26
1、题目名称 Bulb Switcher(灯泡开关) 2、题目地址 https://leetcode.com/problems/bulb-switcher/ 3、题目内容 英文:There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the n th round, you only toggle the last bulb. Find how many bulbs are on after n rounds. 中文:现有n个灯泡,默认都是关闭的。第一轮会打开所有的灯泡,第二轮关闭所有偶数次序的灯泡,第三轮翻转所有次序为三的倍数位置的灯泡,直到第n轮拨动最后一个灯泡的开关。试确定第n轮后还有几盏灯是亮的。 4、解题方法1(TLE) 一开始我还是尝试着写了一段用于暴力破解问题的代码,这段代码毫无悬念地会导致TLE(Time Limit Exceeded)。 Java代码如下: /** * 灯泡开关测试 *