LeetCode-Algorithms-[Easy]1165. 单行键盘

1165. 单行键盘

	public int calculateTime(String keyboard, String word) {
		HashMap<Character, Integer> map = getMap(keyboard);
		int sum = 0, pre = 0;
		for (int i = 0; i < word.length(); ++i) {
			char c = word.charAt(i);
			int crr = map.get(c);
			sum += Math.abs(crr - pre);
			pre = crr;
		}
		return sum;
	}

	private HashMap<Character, Integer> getMap(String keyboard) {
		HashMap<Character, Integer> map = new HashMap<Character, Integer>();
		for (int i = 0; i < keyboard.length(); ++i) {
			char c = keyboard.charAt(i);
			map.put(c, i);
		}
		return map;
	}
发布了272 篇原创文章 · 获赞 7 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/m0_37302219/article/details/105466983