LeetCode--easy
来源:互联网 发布:java中注解有几种 编辑:程序博客网 时间:2024/05/10 12:37
453. Minimum Moves to Equal Array Elements
给定一个非空的整数数组大小为n,找到所需要的最小次数让所有数组元素相同的情况下,每次的规则是将n - 1的元素加1
public class Solution { public int minMoves(int[] nums) { if (nums.length == 0) return 0; int min = nums[0]; for (int n : nums) { // n表示的是数组中的每个元素 min = Math.min(min, n); //取出 数组中最小的 值 } int res = 0; for (int n : nums) res += n - min; //每个元素 - 最小值 核心思想 return res; }}
383. Ransom Note
任意给两个字符串str1和str2,字符串中的字符可以重复。如果str2包含str1中的所有字符(包括重复字符),则返回true,否则,返回false
public class Solution { //只适合与字符串只为 小写 或者 只是大写 public boolean canConstruct(String ransomNote, String magazine) { int[] arr = new int[26];//主要是将magazine中的所有字符转换成0 - 25 的值,刚好满足数组大小,值为非0的就表示该字符存在。含有多个重复的,则在该位置多一个加一次1 for (int i = 0; i < magazine.length(); i++) { arr[magazine.charAt(i) - 'a']++; }//如果字符不存在,则arr[ ]所在位置的值为0,在减1,为-1,则由此来判断字符不存在。 for (int i = 0; i < ransomNote.length(); i++) { if(--arr[ransomNote.charAt(i)-'a'] < 0) { return false; } } return true; }}
另外一个程序,有点复杂
public class Solution { //用到charAt次数有点多 public boolean canConstruct(String ransomNote, String magazine) { ArrayList<Character> al = new ArrayList<Character>(); //多了一个存储空间 空间浪费 int res = 1; for(int i = 0;i < magazine.length();i++) { al.add(magazine.charAt(i)); } for(int j = 0;j < ransomNote.length();j++) { if(al.contains(ransomNote.charAt(j))) al.remove(new Character(ransomNote.charAt(j))); else { res = 0; break; } } if(res == 0) return false; else return true; } }
617. Merge Two Binary Trees
给定两个树,将对应位置的节点值加到一起组成一个新的树
public class Solution { public TreeNode mergeTrees(TreeNode t1, TreeNode t2) { if (t1 == null && t2 == null) return null; int val = (t1 == null ? 0 : t1.val) + (t2 == null ? 0 : t2.val); TreeNode newNode = new TreeNode(val); //建立以一个新的树的节点 newNode.left = mergeTrees(t1 == null ? null : t1.left, t2 == null ? null : t2.left); newNode.right = mergeTrees(t1 == null ? null : t1.right, t2 == null ? null : t2.right); //两个树的左右节点的递归计算 return newNode; }}
401.Binary Watch
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).Each LED represents a zero or one, with the least significant bit on the right
Example:
Input: n = 1
Return: [“1:00”, “2:00”, “4:00”, “8:00”, “0:01”, “0:02”, “0:04”, “0:08”, “0:16”, “0:32”]
public class Solution { public List<String> readBinaryWatch(int num) { List<String> lt = new ArrayList<String>(); for(int h = 0;h < 12;h++) { for(int s = 0;s < 60;s++) { int time = h * 64 + s; if(Integer.bitCount(time) == num) { //bitCount(int i)整数i转换成2进制所包含的所有1的个数 lt.add(String.format("%d:%02d",h,s)); //format()按照指定的格式返回字符串 } } } return lt; }}
437 Path Sum III
给定一个二叉树,以及一个sum数,找出二叉树中所有满足和为sum的路径。任意一个路径不一定非得是root节点开始或者是也树的叶子节点结束的路径,可以是其中的任意两个节点之间的和满足即可。
/*解题思路:分成两部分:1、树中的任意一个节点都可以作为一个跟节点,然后查找从这个节点出发的所有满足sum的路径。例如下程序中的pathSum函数,就是每次寻找一个节点作为help的根节点 * 2、查找以某一个节点为起始节点的子树。例如:helper,每次递归是利用sum-root.val 观察下一个*root.val是否与其相等,如果相等,表示之前的路径满足要求,即count++。每次循环都找到叶子节点 */class Solution { public int pathSum(TreeNode root, int sum) { if(root == null) return 0; return helper(root,sum) + pathSum(root.left,sum) + pathSum(root.right,sum); //这个return中,helper表示通过pathSum选取的根节点作为路径的起始节点,后面两个pathSum分别是找其他的节点作为下一次计算路径的起始节点 } public int helper(TreeNode root,int sum) {//计算到达目前为止的路径是否满足sum if(root == null) return 0; int count = 0; if(root.val == sum) count++;//满足 return count + helper(root.left,sum - root.val) + helper(root.right,sum - root.val);//每个节点的左右节点都要遍历到 }}
101. Symmetric Tree
镜像二叉树,关于树的中心左右对称
//利用了递归的方法来解决这个问题。基本思想就是,每次判断的都是一个节点的左节点与右节点;依次,左节点的左节点与右节点的右节点 以及 左节点的右节点与右节点的左节点进行比较class Solution { public boolean isSymmetric(TreeNode root) { return root==null || isSymmetricHelp(root.left, root.right);//首次进行比较 } private boolean isSymmetricHelp(TreeNode left, TreeNode right){ if(left==null || right==null) return left==right; if(left.val!=right.val) return false; return isSymmetricHelp(left.left, right.right) && isSymmetricHelp(left.right, right.left);//核心算法:左子树左节点与右子树右节点 左子树右节点与右子树左节点}
459. Repeated Substring Pattern
对于字符串s,寻找出来一个子串a,如果s是由多个a拼接而成,则返回true,否则,返回false。例如:如果s=aabb,则返回true 如果s=aba,则返回false 如果s=a,则返回false 如果s=aa,则返回true
/*首先,需要找到一个子串。这个子串主要是用子串的长度一次增加来确定的。其次,通过用s中第一个子串来与剩余的子串依次判断是否相同*/class Solution { public boolean repeatedSubstringPattern(String s) { boolean isTrue = false; char[] c = s.toCharArray(); if(c.length <= 1) return false; // 首先判断 s长度小于1 返回false int l = 1; for(int i = 1;i < s.length();i++) {//判断字串长度为1 if(c[0] != c[i]) { break; }else { l++; } } if(l == s.length()) return true; for(int k = 2;k <= s.length() / 2;k++) {//判断字串长度大于2 小于s长度的一般 int count = 0; int n = s.length() / k; if(s.length() != n * k) { continue; } ONU://判断长度为k的子串,是否满足可以由这个子串拼接成s for(int j = 1;j < k;j++) { for(int i = 0;i < n;i++) { if(c[i] != c[j * n + i]) { break ONU; }else { count++; } } } if(count == (k - 1) * n) {//判断遍历结束是否为true isTrue = true; break; } } return isTrue; }}
441. Arranging Coins
给你一个整数n,求出从1+2+3+...+x<=n,求出满足要求的最大的n。
/*思路:有很多种方法: 1、可以直接利用求二次方方程的求解公式来求出求解公式 2、利用暴力求解发,利用循环,每次加一个数求和的方式来求解 3、可以利用折半的方法求解,每次取一个中间值来求解,这样比暴力求解的方法要快很多*///采取的是 利用公式求解的方法class Solution { public int arrangeCoins(int n) { return (int)((-1 + Math.sqrt(1 + 8 * (long)n)) / 2); }}
172. Factorial Trailing Zeroes
给定一个整数n,求出n的阶乘值,然后找出n的阶乘的末尾0的个数。
/*思路:1、求出n的阶乘值,然后一次利用对10整除、对10取余的方法找出来0的个数2、求出n之前的所有的数包含2以及5的个数,返回最小的个数即为最后0的个数首先,对于方法1来说,不太容易实现,毕竟阶乘出来的值会很大,没有可以盛放的变量值,所以放弃所以,采用方法2。2 * 5 是10,而2的个数明显会多于5的个数,所以只需要求出来5的个数就好*/class Solution { public int trailingZeroes(int n) { return n == 0 ? 0 : n / 5 + trailingZeroes(n / 5);//每次除以5将其加起来 /*每隔5,都会有一个可以除以5的值,所以,用n / 5,这样可以求出来有多少个单5 每隔25,都会有一个可以除以25的值,所以,用n/5/5,这样可以求出有多少个双5 ............ 依次进行就好 每次除以5,可以找出所有的5*/ }}
400.Nth Digit
一个自然序列,1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...,给定一个int值n,找出n对应的数。其中,10代表的是两个数。例子: n = 3 输出:3 n = 11 输出:0 10的1 是第10个 10 的0 是第11个
/*思想:1~9 9个数 9*1=9个digit 10~99 90个数 90*2=180个digit 100~999 900个数 900*3=2700个digit 10^k ~ k个9连成的数 9*10^k个数 (90*10^k)*k个digit所以给点n,首先确定在几位数之间,如在1000~9999还是在其他之间?然后确定是该区间的哪个数?最后确定是该数字的哪个digit? 注意防止溢出。*/class Solution { public int findNthDigit(int n) { int len = 1; long count = 9; int start = 1; while (n > len * count) {//判断剩余部分是否在下一个区间 n -= len * count;//n每次减去前面的一个区间内的个数 这样做的好处是:可以避免数据溢出(如果用求和的方法会超出int范围) len += 1; count *= 10; start *= 10;//下一个区间的起始数的大小 如10,100,1000.... } start += (n - 1) / len;//之前的n都是从前一个开始算的 所以要 -1 String s = Integer.toString(start);//n所在的数字 转为字符串 return (s.charAt((n - 1) % len) - '0'); }}
204. Count Primes
给定一个非负整数n,寻找小于n的所有质数的个数。
/*思路:给定一个数组,默认全部都是false,然后用是否为false来判断是否为质数例如:2是质数,然后用2乘以(2-n/2的数),这些积一定为非负数,然后将数组中的false改为true,每次都这样来计算,减少计算时间。*//*对于判断一个数n是否为质数方法:判断该数在2-n的算术平方根中是否可以整除,如果不可以则为质数,否则非质数。*/class Solution { public int countPrimes(int n) { boolean[] notPrime = new boolean[n];//所有元素默认为false int count = 0; for (int i = 2; i < n; i++) {//利用2,3为质数 if (notPrime[i] == false) { count++; for (int j = 2; i*j < n; j++) { notPrime[i*j] = true; } } } return count; }}
168. Excel Sheet Column Title
给定一个整数n,返回对应的excel表1 -> A2 -> B3 -> C...26 -> Z27 -> AA28 -> AB特别注意:其中的52->AZ,53->BA,26->Z
/*要特别注意那几个特殊值。可以看出来,如果用正常的取余,取对应的值,然后在取整,一次来算的话,就会出现,52得到的是Bnull,而实际是AZ。所以,在这里,每次计算的时候,需要把整除之后的数-1,这样就会避开这种问题。*/class Solution { public String convertToTitle(int n) { StringBuilder result = new StringBuilder(); while(n>0){ n--;//就是为了避免特殊值 result.insert(0, (char)('A' + n % 26));//插入对应值 n /= 26; //每次取整作为剩余的计算值 } return result.toString(); }}
阅读全文
0 0
- leetcode easy
- LeetCode--easy
- Leetcode[Easy] - Valid Palindrome
- Leetcode[Easy] - Path Sum
- Leetcode[Easy] - Two Sum
- Leetcode[easy] - Implement strStr()
- Leetcode--easy系列1
- Leetcode--easy系列2
- Leetcode--easy系列3
- Leetcode--easy系列4
- Leetcode--easy系列5
- Leetcode--easy系列6
- Leetcode--easy系列7
- Leetcode--easy系列8
- Leetcode--easy系列9
- Leetcode--easy系列10
- LeetCode Record (Easy)
- <LeetCode><Easy> 217 ContainsDuplicate
- 搜索-B
- Apache_tomcat配置域名-绑定项目
- 【python】使用urllib,requests调google translate API
- shadowssdt 地址 数量 遍历
- 使用ffmpeg步骤
- LeetCode--easy
- Pb菜单工具栏图标顺序的设置
- leetcode 529. Minesweeper
- 使用IDEA搭建Maven
- PAT1024_字符串统计
- Linux安装zookeeper集群(CentOS7+Zookeeper3.4.10)
- uva 10382
- TensorFlow编程入门(二)
- VC++6.0导入pthread库配置流程