├── Determine Color of a Chessboard Square.txt ├── First letter to appear twice.txt ├── Largest Odd Number in String.txt ├── Longest Common prefix.txt ├── Minimum Operations to Make a Uni-Value Grid.txt └── Roman to Integer.txt /Determine Color of a Chessboard Square.txt: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean squareIsWhite(String coordinates) { 3 | return (coordinates.charAt(0) + coordinates.charAt(1)) % 2 == 1; 4 | } 5 | } -------------------------------------------------------------------------------- /First letter to appear twice.txt: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public char repeatedCharacter(String s) { 3 | int[] cnt = new int[26]; 4 | for (int i = 0;; ++i) { 5 | char c = s.charAt(i); 6 | if (++cnt[c - 'a'] == 2) { 7 | return c; 8 | } 9 | } 10 | } 11 | } -------------------------------------------------------------------------------- /Largest Odd Number in String.txt: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String largestOddNumber(String num) { 3 | for (int i = num.length() - 1; i >= 0; --i) { 4 | int c = num.charAt(i) - '0'; 5 | if ((c & 1) == 1) { 6 | return num.substring(0, i + 1); 7 | } 8 | } 9 | return ""; 10 | } 11 | } -------------------------------------------------------------------------------- /Longest Common prefix.txt: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String longestCommonPrefix(String[] strs) { 3 | int n = strs.length; 4 | for (int i = 0; i < strs[0].length(); ++i) { 5 | for (int j = 1; j < n; ++j) { 6 | if (strs[j].length() <= i || strs[j].charAt(i) != strs[0].charAt(i)) { 7 | return strs[0].substring(0, i); 8 | } 9 | } 10 | } 11 | return strs[0]; 12 | } 13 | } -------------------------------------------------------------------------------- /Minimum Operations to Make a Uni-Value Grid.txt: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minOperations(int[][] grid, int x) { 3 | int m = grid.length, n = grid[0].length; 4 | int[] nums = new int[m * n]; 5 | int mod = grid[0][0] % x; 6 | for (int i = 0; i < m; ++i) { 7 | for (int j = 0; j < n; ++j) { 8 | if (grid[i][j] % x != mod) { 9 | return -1; 10 | } 11 | nums[i * n + j] = grid[i][j]; 12 | } 13 | } 14 | Arrays.sort(nums); 15 | int mid = nums[nums.length >> 1]; 16 | int ans = 0; 17 | for (int v : nums) { 18 | ans += Math.abs(v - mid) / x; 19 | } 20 | return ans; 21 | } 22 | } -------------------------------------------------------------------------------- /Roman to Integer.txt: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int romanToInt(String s) { 3 | String cs = "IVXLCDM"; 4 | int[] vs = {1, 5, 10, 50, 100, 500, 1000}; 5 | Map d = new HashMap<>(); 6 | for (int i = 0; i < vs.length; ++i) { 7 | d.put(cs.charAt(i), vs[i]); 8 | } 9 | int n = s.length(); 10 | int ans = d.get(s.charAt(n - 1)); 11 | for (int i = 0; i < n - 1; ++i) { 12 | int sign = d.get(s.charAt(i)) < d.get(s.charAt(i + 1)) ? -1 : 1; 13 | ans += sign * d.get(s.charAt(i)); 14 | } 15 | return ans; 16 | } 17 | } --------------------------------------------------------------------------------