博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
初级算法49题 — LeetCode(20181122 - )
阅读量:4591 次
发布时间:2019-06-09

本文共 8602 字,大约阅读时间需要 28 分钟。

Array:

Single Number
1 class Solution { 2     public int singleNumber(int[] nums) { 3         if (nums == null || nums.length == 0) { 4             return 0; 5         } 6         int res = nums[0]; 7         for (int i = 1; i < nums.length; i++) { 8             res = res ^ nums[i]; 9         }10 11         return res;12     }13 }
View Code
Remove Duplicates from Sorted Array
1 class Solution { 2     public int removeDuplicates(int[] nums) { 3         if(nums==null || nums.length==0){ 4             return 0; 5         } 6         int res =1; 7         for(int i=1;i
View Code
Best Time to Buy and Sell Stock II
1 class Solution { 2     public int maxProfit(int[] prices) { 3         if (prices == null || prices.length == 0) { 4             return 0; 5         } 6         int res = 0; 7         for (int i = 0; i < prices.length - 1; i++) { 8             if (prices[i + 1] - prices[i] > 0) { 9                 res += prices[i + 1] - prices[i];10             }11         }12         return res;13     }14 }
View Code
Rotate Array

 这题注意处理k>nums.length的情况,直接取余

1 class Solution { 2     public void rotate(int[] nums, int k) { 3         if (nums == null || nums.length == 0) { 4             return; 5         } 6         k = k%nums.length; 7         rotateSubArray(nums, 0, nums.length - k - 1); 8         rotateSubArray(nums, nums.length - k, nums.length - 1); 9         rotateSubArray(nums, 0, nums.length - 1);10     }11 12     public void rotateSubArray(int[] nums, int start, int end) {13         int left = start;14         int right = end;15         while (left < right) {16             swap(nums, left, right);17             left++;18             right--;19         }20     }21 22     public void swap(int[] nums, int aIndex, int bIndex) {23         int temp = nums[aIndex];24         nums[aIndex] = nums[bIndex];25         nums[bIndex] = temp;26     }27 }
View Code
Contains Duplicate
1 class Solution { 2     public boolean containsDuplicate(int[] nums) { 3         if (nums == null || nums.length == 0) { 4             return false; 5         } 6  7         Arrays.sort(nums); 8         for (int i = 0; i < nums.length - 1; i++) { 9             if (nums[i] == nums[i + 1])10                 return true;11         }12 13         return false;14     }15 }
View Code
 Intersection of Two Arrays II
1 class Solution { 2     public int[] intersect(int[] nums1, int[] nums2) { 3         if (nums1 == null || nums1.length == 0) { 4             return nums1; 5         } 6  7         if (nums2 == null || nums2.length == 0) { 8             return nums2; 9         }10         List
list = new ArrayList<>();11 HashMap
map = new HashMap<>();12 for (int num : nums1) {13 if (map.get(num) == null) {14 map.put(num, 1);15 } else {16 map.put(num, map.get(num) + 1);17 }18 }19 20 for (int num : nums2) {21 Integer exist = map.get(num);22 if (exist != null && exist > 0) {23 map.put(num, exist - 1);24 list.add(num);25 }26 }27 28 int[] res = new int[list.size()];29 for (int i = 0; i < res.length; i++) {30 res[i] = list.get(i);31 }32 return res;33 }34 }
View Code
Plus One

 注意当结果为1000...时需重新生成长数组返回,否则WA(eg:99999 -> 00000)

1 class Solution { 2     public int[] plusOne(int[] digits) { 3         if (digits == null || digits.length == 0) { 4             return digits; 5         } 6  7         int carry = digits[digits.length - 1] + 1 >= 10 ? 1 : 0; 8         digits[digits.length - 1] = carry == 1 ? 0 : digits[digits.length - 1] + 1; 9         if (carry == 0) {10             return digits;11         }12         for (int i = digits.length - 2; i >= 0; i--) {13             if (digits[i] + carry >= 10) {14                 digits[i] = 0;15             } else {16                 digits[i] = digits[i] + carry;17                 return digits;18             }19         }20         21         int[] newNumber = new int[digits.length+1];22         newNumber[0] = 1;23         return newNumber;24     }25 }
View Code
Move Zeroes
1 class Solution { 2     public void moveZeroes(int[] nums) { 3         if (nums == null || nums.length == 0) { 4             return; 5         } 6  7         int noZeroIndex = 0; 8         for (int i = 0; i < nums.length; i++) { 9             if (nums[i] != 0) {10                 nums[noZeroIndex] = nums[i];11                 noZeroIndex++;12             }13         }14 15         for (int j = noZeroIndex; j < nums.length; j++) {16             nums[j] = 0;17         }18 19         return;20     }21 }
View Code
Two Sum
1 class Solution { 2     public int[] twoSum(int[] nums, int target) { 3         if (nums == null || nums.length < 2) { 4             return null; 5         } 6  7         Map
map = new HashMap<>(); 8 for (int i = 0; i < nums.length; i++) { 9 Integer exist = map.get(target - nums[i]);10 if (exist != null) {11 int[] res = new int[2];12 res[0] = i;13 res[1] = exist;14 return res;15 }16 map.put(nums[i], i);17 }18 19 return null;20 }21 }
View Code

 

 

Strings

Reverse String
1 class Solution { 2     public String reverseString(String s) { 3         if (s == null || s.length() == 0) { 4             return s; 5         } 6         char[] arrays = s.toCharArray(); 7         int start = 0; 8         int end = arrays.length - 1; 9         while (start < end) {10             char temp = arrays[start];11             arrays[start] = arrays[end];12             arrays[end] = temp;13             start++;14             end--;15         }16 17         return new String(arrays);18     }19 }
View Code
Reverse Integer
1 class Solution { 2     public int reverse(int x) { 3         String s = x + ""; 4         if (s.length() <= 1) { 5             return x; 6         } 7         char[] arrays = s.toCharArray(); 8         int len = arrays.length; 9         int start = 0;10         int end = s.length() - 1;11         while (start < arrays.length && arrays[start] > '9' || arrays[start] < '0') {12             start++;13         }14         while (end >= 0 && arrays[end] == '0') {15             arrays[end] = ' ';16             end--;17         }18         while (start < end) {19             char temp = arrays[start];20             arrays[start] = arrays[end];21             arrays[end] = temp;22             start++;23             end--;24         }25 26         s = new String(arrays);27         Long res = Long.valueOf(s.trim());28         if (res > Integer.MAX_VALUE || res < Integer.MIN_VALUE) {29             return 0;30         }31         return res.intValue();32     }33 }
View Code
 First Unique Character in a String
1 class Solution { 2     public int firstUniqChar(String s) { 3         if (s == null || s.length() == 0) { 4             return -1; 5         } 6  7         char[] array = s.toCharArray(); 8         Map
map = new HashMap<>(); 9 for (int i = 0; i < array.length; i++) {10 Integer exist = map.get(array[i]);11 if (exist == null) {12 map.put(array[i], 1);13 } else {14 map.put(array[i], exist + 1);15 }16 }17 18 for (int i = 0; i < array.length; i++) {19 if (map.get(array[i]) == 1) {20 return i;21 }22 }23 return -1;24 }25 }
View Code
Valid Palindrome
1 class Solution { 2     public boolean isPalindrome(String s) { 3         if (s == null || s.length() == 0) { 4             return true; 5         } 6         s = s.trim(); 7         int start = 0; 8         int end = s.length() - 1; 9         while (start < end) {10             while (start < s.length() && !Character.isLetter(s.charAt(start)) && !Character.isDigit(s.charAt(start))) {11                 start++;12             }13 14             while (end >= 0 && !Character.isLetter(s.charAt(end)) && !Character.isDigit(s.charAt(end))) {15                 end--;16             }17             if (start >= end) {18                 break;19             }20             if (Character.toLowerCase(s.charAt(start)) != Character.toLowerCase(s.charAt(end))) {21                 return false;22             }23             start++;24             end--;25         }26 27         return true;28     }29 }
View Code

 

转载于:https://www.cnblogs.com/lizzyluvcoding/p/10001100.html

你可能感兴趣的文章
SqlSerch 查找不到数据
查看>>
集合相关概念
查看>>
Memcache 统计分析!
查看>>
(Python第四天)字符串
查看>>
个人介绍
查看>>
使用python动态特性时,让pycharm自动补全
查看>>
NSDate
查看>>
堆排序
查看>>
java架构《Socket网络编程基础篇》
查看>>
HASH、HASH函数、HASH算法的通俗理解
查看>>
easyui学习日记20141213
查看>>
getopt()函数
查看>>
第八届极客大挑战 Re
查看>>
ZOJ3471--Most Powerful(状压DP)
查看>>
POJ3666-Making the Grade(左偏树 or DP)
查看>>
杭电2084数塔
查看>>
ISE中FPGA的实现流程
查看>>
虚拟机centos笔记整理,持续更新~~
查看>>
Spring MVC访问静态资源
查看>>
jquery实现的个性网站首页 详细信息
查看>>