- 博客(7)
- 收藏
- 关注
原创 leetcode(第五十八题 最后一个单词的长度 && 第六十六题 加一 && 第六十七题 二进制求和 )
需要注意几种特殊情况:字符串为空串字符串中仅有空格字符public static void main(String[] args) { String str = "a"; System.out.println(lengthOfLastWord2(str)); } //将字符串转化为字符数组,内存占用较大 public static int lengthOfLastWord1(String s) { if( s=="" ){.
2020-08-19 14:17:03 124
原创 leetcode(第三十五题 搜索插入位置 && 第三十八题 外观数列 && 第五十三题 最大子序和 )
class Solution { public int searchInsert(int[] nums, int target) { int low = 0, high = nums.length-1, mid = 0; while( low<=high ){ mid = (low+high)/2; if( nums[mid] < target ){ low = mid+1;.
2020-08-15 23:28:20 73
原创 leetcode( 第十四题 最长公共前缀 & 第二十一题 合并两个有序链表 )
class Solution { public String longestCommonPrefix(String[] strs) { if( strs.length==0 || strs==null){ return ""; } String firstStr = strs[0]; if( firstStr==""){ return ""; } Stri.
2020-08-12 15:49:55 116
原创 leetcode(第十三题 罗马数字转整数)
class Solution { public int romanToInt(String s) { char[] chars = s.toCharArray(); int result = 0; for(int i=s.length()-1; i>=0; i--){ switch(chars[i]){ case'I': if(i==s.length()-1){ ..
2020-08-12 15:47:58 96
原创 leetcode(第九题 回文数)
class Solution { public boolean isPalindrome(int x) { if(x<0){ return false; } int result = 0; int initX = x; while( x != 0 ){ int temp = x%10; result = result*10 + temp; .
2020-08-12 10:43:36 61
原创 leetcode(第七题 整数反转)
class Solution { public int reverse(int x) { int result = 0; while(x != 0){ //不能通过除数判断,因为剩下的最后一个数字除以10后除数一定为0,直接退出循环 //也不能用余数,如120,直接第一个循环都进不去 int newResult = result*10 + x%10; //x % 10是余.
2020-08-11 22:58:49 104
原创 leetcode(第二十题 有效的括号)
class Solution { public boolean isValid(String s) { Stack <Character> stack = new Stack<>(); if(s.length() % 2 == 1){ return false; } char[] arrays = s.toCharArray(); for(int i = 0; i<.
2020-08-10 23:06:49 120
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人