初级算法49题 — LeetCode(20181122 - )
Array:
class Solution {
public int singleNumber(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int res = nums[0];
for (int i = 1; i < nums.length; i++) {
res = res ^ nums[i];
}
return res;
}
}
class Solution {
public int removeDuplicates(int[] nums) {
if(nums==null || nums.length==0){
return 0;
}
int res =1;
for(int i=1;i<nums.length;i++){
if(nums[i]!=nums[i-1]){
nums[res] = nums[i];
res++;
}
}
return res;
}
}
class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
}
int res = 0;
for (int i = 0; i < prices.length - 1; i++) {
if (prices[i + 1] - prices[i] > 0) {
res += prices[i + 1] - prices[i];
}
}
return res;
}
}
这题注意处理k>nums.length的情况,直接取余
class Solution {
public void rotate(int[] nums, int k) {
if (nums == null || nums.length == 0) {
return;
}
k = k%nums.length;
rotateSubArray(nums, 0, nums.length - k - 1);
rotateSubArray(nums, nums.length - k, nums.length - 1);
rotateSubArray(nums, 0, nums.length - 1);
}
public void rotateSubArray(int[] nums, int start, int end) {
int left = start;
int right = end;
while (left < right) {
swap(nums, left, right);
left++;
right--;
}
}
public void swap(int[] nums, int aIndex, int bIndex) {
int temp = nums[aIndex];
nums[aIndex] = nums[bIndex];
nums[bIndex] = temp;
}
}
class Solution {
public boolean containsDuplicate(int[] nums) {
if (nums == null || nums.length == 0) {
return false;
}
Arrays.sort(nums);
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] == nums[i + 1])
return true;
}
return false;
}
}
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
if (nums1 == null || nums1.length == 0) {
return nums1;
}
if (nums2 == null || nums2.length == 0) {
return nums2;
}
List<Integer> list = new ArrayList<>();
HashMap<Integer, Integer> map = new HashMap<>();
for (int num : nums1) {
if (map.get(num) == null) {
map.put(num, 1);
} else {
map.put(num, map.get(num) + 1);
}
}
for (int num : nums2) {
Integer exist = map.get(num);
if (exist != null && exist > 0) {
map.put(num, exist - 1);
list.add(num);
}
}
int[] res = new int[list.size()];
for (int i = 0; i < res.length; i++) {
res[i] = list.get(i);
}
return res;
}
}
注意当结果为1000...时需重新生成长数组返回,否则WA(eg:99999 -> 00000)
class Solution {
public int[] plusOne(int[] digits) {
if (digits == null || digits.length == 0) {
return digits;
}
int carry = digits[digits.length - 1] + 1 >= 10 ? 1 : 0;
digits[digits.length - 1] = carry == 1 ? 0 : digits[digits.length - 1] + 1;
if (carry == 0) {
return digits;
}
for (int i = digits.length - 2; i >= 0; i--) {
if (digits[i] + carry >= 10) {
digits[i] = 0;
} else {
digits[i] = digits[i] + carry;
return digits;
}
}
int[] newNumber = new int[digits.length+1];
newNumber[0] = 1;
return newNumber;
}
}
class Solution {
public void moveZeroes(int[] nums) {
if (nums == null || nums.length == 0) {
return;
}
int noZeroIndex = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 0) {
nums[noZeroIndex] = nums[i];
noZeroIndex++;
}
}
for (int j = noZeroIndex; j < nums.length; j++) {
nums[j] = 0;
}
return;
}
}
class Solution {
public int[] twoSum(int[] nums, int target) {
if (nums == null || nums.length < 2) {
return null;
}
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
Integer exist = map.get(target - nums[i]);
if (exist != null) {
int[] res = new int[2];
res[0] = i;
res[1] = exist;
return res;
}
map.put(nums[i], i);
}
return null;
}
}
Strings
class Solution {
public String reverseString(String s) {
if (s == null || s.length() == 0) {
return s;
}
char[] arrays = s.toCharArray();
int start = 0;
int end = arrays.length - 1;
while (start < end) {
char temp = arrays[start];
arrays[start] = arrays[end];
arrays[end] = temp;
start++;
end--;
}
return new String(arrays);
}
}
class Solution {
public int reverse(int x) {
String s = x + "";
if (s.length() <= 1) {
return x;
}
char[] arrays = s.toCharArray();
int len = arrays.length;
int start = 0;
int end = s.length() - 1;
while (start < arrays.length && arrays[start] > '9' || arrays[start] < '0') {
start++;
}
while (end >= 0 && arrays[end] == '0') {
arrays[end] = ' ';
end--;
}
while (start < end) {
char temp = arrays[start];
arrays[start] = arrays[end];
arrays[end] = temp;
start++;
end--;
}
s = new String(arrays);
Long res = Long.valueOf(s.trim());
if (res > Integer.MAX_VALUE || res < Integer.MIN_VALUE) {
return 0;
}
return res.intValue();
}
}
class Solution {
public int firstUniqChar(String s) {
if (s == null || s.length() == 0) {
return -1;
}
char[] array = s.toCharArray();
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < array.length; i++) {
Integer exist = map.get(array[i]);
if (exist == null) {
map.put(array[i], 1);
} else {
map.put(array[i], exist + 1);
}
}
for (int i = 0; i < array.length; i++) {
if (map.get(array[i]) == 1) {
return i;
}
}
return -1;
}
}
class Solution {
public boolean isPalindrome(String s) {
if (s == null || s.length() == 0) {
return true;
}
s = s.trim();
int start = 0;
int end = s.length() - 1;
while (start < end) {
while (start < s.length() && !Character.isLetter(s.charAt(start)) && !Character.isDigit(s.charAt(start))) {
start++;
}
while (end >= 0 && !Character.isLetter(s.charAt(end)) && !Character.isDigit(s.charAt(end))) {
end--;
}
if (start >= end) {
break;
}
if (Character.toLowerCase(s.charAt(start)) != Character.toLowerCase(s.charAt(end))) {
return false;
}
start++;
end--;
}
return true;
}
}
初级算法49题 — LeetCode(20181122 - )的更多相关文章
- LeetCode初级算法的Python实现--链表
LeetCode初级算法的Python实现--链表 之前没有接触过Python编写的链表,所以这里记录一下思路.这里前面的代码是和leetcode中的一样,因为做题需要调用,所以下面会给出. 首先定义 ...
- 【LeetCode算法】LeetCode初级算法——字符串
在LeetCode初级算法的字符串专题中,共给出了九道题目,分别为:反转字符串,整数反转,字符串中的第一个唯一字符,有效的字母异位词,验证回文字符串,字符串转换整数,实现strStr(),报数,最 ...
- LeetCode探索初级算法 - 动态规划
LeetCode探索初级算法 - 动态规划 今天在LeetCode上做了几个简单的动态规划的题目,也算是对动态规划有个基本的了解了.现在对动态规划这个算法做一个简单的总结. 什么是动态规划 动态规划英 ...
- LeetCode初级算法--数组01:只出现一次的数字
LeetCode初级算法--数组01:只出现一次的数字 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn. ...
- LeetCode初级算法--数组02:旋转数组
LeetCode初级算法--数组02:旋转数组 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/ ...
- LeetCode初级算法--字符串01:反转字符串
LeetCode初级算法--字符串01:反转字符串 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.ne ...
- LeetCode初级算法--链表01:反转链表
LeetCode初级算法--链表01:反转链表 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/ ...
- LeetCode初级算法--链表02:合并两个有序链表
LeetCode初级算法--链表02:合并两个有序链表 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn. ...
- LeetCode初级算法--树01:二叉树的最大深度
LeetCode初级算法--树01:二叉树的最大深度 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.n ...
随机推荐
- easyui页签更新
1.首先引入这个js文件 <script src="/Scripts/tabs.js" type="text/javascript"></sc ...
- T Fiddler 教程 _转
阅读目录 Fiddler的基本介绍 Fiddler的工作原理 同类的其它工具 Fiddler如何捕获Firefox的会话 Fiddler如何捕获HTTPS会话 Fiddler的基本界面 Fiddler ...
- C++ 调用C++写的函数库的2种方法之一(显式调用)
一:创建C++ DLL类库,名称:Dll1 1.Dll.h _declspec(dllimport) int add(int a, int b); 2.Dll.cpp // Dll.cpp : 定义 ...
- Linq实战 之 DataSet操作详解
Linq实战 之 DataSet操作详解 一:linq to Ado.Net 1. linq为什么要扩展ado.net,原因在于给既有代码增加福利.FCL中在ado.net上扩展了一些方法. 简单一 ...
- 优化MYSQL配置文件MY.INI
table_cache=1024 物理内存越大,设置就越大.默认为2402,调到512-1024最佳.由于每个客户端连接都会至少访问一个表,因此此参数的值与max_connections有关.当某一连 ...
- Linux Qt 5.x 环境搭建
Step 1 从Qt官网下载 qt-opensource-linux-x64...run 在linux命令行中给予文件可执行权限 $ chmod u+x qt-opensource-linux...r ...
- java(二)认识类和函数
java中,声明类使用new关键字 类名 类变量名 = new 类名(构造函数形参表): 静态函数内不能有非静态类,但是可以有非静态函数. package com.company; /** * Cre ...
- MacOS安装使用Node.js
1. 到官网https://nodejs.org/zh-cn/download/下载,选择Macintosh Installer, 如下: 2. 按预设的下一步,Node.js版本为v6.10.0, ...
- 什么是C#?什么是.NET Framework?
1.什么是C#: 解1:C#就是一门开发语言,是由C及C++演变而来的,有朋友戏称之为"C四个+",这里的"#"号,不读"井",而读做&qu ...
- gitignore失效 删除 git commit记录
已追踪的文件需要清理本地缓存 git rm -r --cached . git add . git commit -m 'update .gitignore' 删除 git commit记录 不小心上 ...