Excel Sheet & Remove Duplicates
Excel Sheet Column Title
public class Solution {
public String convertToTitle(int n) {
if (n <= 0) {
return "";
}
StringBuffer sb = new StringBuffer();
while (--n >= 0) {
sb.insert(0, (char) (n % 26 + 'A'));
n /= 26;
}
return sb.toString();
}
}
Excel Sheet Column Number
public class Solution {
public int titleToNumber(String s) {
StringBuffer sb = new StringBuffer();
int sum = 0;
for (int i = 0; i < s.length(); i++) {
sum = sum * 26;
char c = s.charAt(i);
sum += c - 'A' + 1;
}
return sum;
}
}
Remove Element
public class Solution {
public int removeElement(int[] nums, int val) {
int i = 0;
for (int j = 0; j < nums.length; j++) {
if (nums[j] != val) {
nums[i++] = nums[j];
}
}
return i;
}
}
Move Zeroes
public class Solution {
public void moveZeroes(int[] nums) {
int i = 0;
for (int j = 0; j < nums.length; j++) {
if (nums[j] != 0) {
nums[i++] = nums[j];
}
}
for (int j = i; j < nums.length; j++) {
nums[j] = 0;
}
}
}
Remove Duplicates from Sorted Array
public class Solution {
public int removeDuplicates(int[] nums) {
int i = 0;
for (int j = 1; j < nums.length; j++) {
if (nums[j] != nums[i]) {
nums[++i] = nums[j];
}
}
return i + 1;
}
}
Remove Duplicates from Sorted Array II
public class Solution {
public int removeDuplicates(int[] nums) {
int i = 0, count = 1;
for (int j = 1; j < nums.length; j++) {
if (nums[j] != nums[i] || count < 2) {
if (nums[j] == nums[i]) {
count++;
} else {
count = 1;
}
nums[++i] = nums[j];
}
}
return i + 1;
}
}